I am in serious trouble due to a situation. Here's my problem.
PROBLEM: Need to inject a simple static string field /property in an enum class FROM spring config XML. Here's some similar code I tested :
public enum AppProperty {
NUMBER_OF_ACCOUNTS("accounts"), NUMBER_OF_USERS("users");
private AppProperty(String cuser) {
this.current_user = cuser;
}
private final String current_user;
private static final String PROPERTY_FILE = "application.properties";
static {
init();
}
public static String appContext; // **PROBLEM** : need to populate this ONLY
// from spring config XML via property
// injection.
public static void init() {
if (appContext.equals("statement")) {
// do something..... generate statement et...
} else {
// do some other process... generate balance etc
}
}
.......... few more static methods...
..... few more non-static methods
}// end of class
I tried mostly everything for this simple problem but strangely nothing seems working for Spring 3.1 :(
Injecting a property via property setter etc not working at all.. String appContext always turns out NULL... tried ..
@Autowired(required = true)
setAppConfig( String context){
AppProperty.appContext = context
)
}
and tried every wiring and autowiring.... mostly either returns null or exception "AppProperties.init() cannot be called as no default constructor ...." (even tried a default constructor for god's sake but don't work...!!!
So thought lets get this single property from a property file and get it done but that also don't work and same problem.
<context:component-scan base-package="com.my.package" />
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
</list>
</property>
</bean>
and used this in java class over propert .. @Value("${appContext}")
This also demands to instantiate AppProperty.init() which is not achievable by me.
I think the ONLY way to inject a property from config xml seems using MethodInvokingFactoryBean (How to make spring inject value into a static field)
I tried this also but my hard luck it's also not working!!!! Only thing i think I may do wrong is not using the @value tag properly
In config file I used :
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="com.my.package.setAppContext"/>
<property name="arguments" value ="testing"/>
</bean>
In java file AppProperty I used
public static String appContext;
public void setAppContext(String appContext) {
AppProperty.appContext = appContext;
}
Request : Please provide your answer with exact code which I can use as I may be using right solution but Syntex and configuration are missing .. Shall I use @Component over my AppProperty class!! etc... please provide details and get my blessings...
Note: I NEED to Inject this property via Spring Config XML so that I can change the context at runtime.. As I am not allowed to change existing implementation of this class. So please don't suggest to remove enum class with something else... Thanks in advance.