This question is the follow-up of this one.
The problem is I want to inject a value in .properties file and always return null. The reason is that myClassContext is calling new myclass1() and new myclass2() so both myclass1 and myclass2 are not manage by Spring.
So my question now is how can I refactor the code to make the @Value("${path}") useful so I can get right result of myClassContext.getPath(params) based on the params.
I think I should use static factory-method to do this, but I can't think of a solution.
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
my classes:
package com.mypackage
@Component
public class myclass1 implements Imyclass {
@Value("${path}")
private String path; //this always is null
...
public String getMyPath() {
return path+"myclass1";
}
}
package com.mypackage
@Component
public class myclass2 implements Imyclass {
@Value("${path}")
private String path; //this always is null
...
public String getMyPath() {
return path+"myclass2";
}
}
myClassContext :
public class myClassContext {
public static String getPath(params){
return getImyclass(params).getMyPath();
}
private static Imyclass getImyclass(params){
Imyclass mc=null;
if (logic(params)) {
mc=new myclass();
}
else{
mc=new myclass2();
}
return mc;
}
}
What's the better way to manage instances by Spring so the @value will inject correctly?