I'm using spring 3.2.2 and what to inject some string in my .properties file into a class. But always get null.
In my applicationContext.xml:
....
<context:component-scan base-package="com.mypackage">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/
</context:component-scan>
<context:property-placeholder location="classpath:/config/core/abc.properties"/>
....
in abc.properties:
path=testpath
in java code:
package com.mypackage
@Component
public class myclass implements Imyclass {
@Value("${path}")
private String path; //this always is null
...
public String getMyPath() {
return path+"myclass";
}
}
what I'm missing here?
[Edit]
the reason is in my code someone is calling new Imyclass(). Now there is another class impelement Imyclass:
package com.mypackage
@Component
public class myclass2 implements Imyclass {
@Value("${path}")
private String path; //this always is null
...
public String getMyPath() {
return path+"myclass2";
}
}
The function calling the Imyclass:
public class myClassContext {
public static String getPath(...){
return getImyclass(...).getMyPath();
}
private static Imyclass getImyclass(....){
Imyclass mc=null;
if (.....) {
mc=new myclass();
}
else{
mc=new myclass2();
}
return state;
}
}
What's the better way to manage the instance by Spring so the @value will inject correctly?
Do I have to inject both class into myClassContext as a property or use the @Configurable for both myclass and myclass2?