First of all, as said by @rAy, don't inject to static field. Honestly that's quite meaningless.
Back to your question (assume you changed the field to non-static). The whole problem is not (yet) related to Auto-wiring. As quite some answers already pointed out, in Spring, an instance of bean will be first created, and then field/setter injection happens afterwards. Therefore, your constructor run before you have injected the field it used.
There are a lot of ways to solve this issue. I am listing some of the most common options (I bet there are still more ways), try to understand them and choose one suit your design.
Constructor Injection
Instead of injecting to field, you can choose to inject through constructor. Have your AccountHelper look something like:
public Class AccountHelper {
private String configFilename;
public AccountHelper(String configFilename) {
this.configFilename = configFilename;
// do something base on configFilename
}
}
You can use auto-wiring for the constructor argument, or declare explicitly in XML (from your question, it seems to me you know how to do it)
Initialization method
In Spring, there are tons of way to have an initialization method after all properties are set. So, you can do something like:
public Class AccountHelper implements InitializingBean{
private String configFilename;
// corresponding setters etc
public AccountHelper() {
// don't do initialization in ctor
}
@Override
public void afterPropertiesSet() {
// called after property injections finished
// do something base on configFilename
}
}
or you can even have a method in any name you like, and tell Spring you want to use it as initialization method, by annotating with @PostConstruct
, or by init-method
in XML, or by initMethodName
in @Bean
, etc.
Factory Bean
If you don't have much control on the way your AccountHelper is created, and the creation process may be difficult to perform by plain Spring config, you can always write a Factory Bean to create the bean, with the construction logic hand-crafted in the Factory Bean.
After you make the thing work without auto-wiring, adding auto-wiring is just a piece of cake.