Reputation: 1633
I have the following class and I am able to use which are annotated as @Component
only when use ctx.getBean()
. But I want to know below.
Why annotation @autowired
is not working.
I am getting error at PathVerifier
because it is interface and following factory patter. The actual implementation object will only know runtime right. So I added @Component
for all implementations of my PathVerifier
interface.
I have below in my context file:
<context:annotation-config />
<context:component-scan base-package="com.xxx.xxx.process">
</context:component-scan>
My main classes is as follows:
@Component("verifier")
public class Verifier {
private static final Logger log = LoggerFactory.getLogger(Verifier.class);
@Autowired
private static PathVerifierFactory pathVerifierFactory;
private static PathVerifer pathVerifier;
public AutogenPathInfo getXMLPathData() throws Exception {
ApplicationContext ctx = new ClassPathXmlApplicationContext("context.xml");
FileInputParser parser = (FileInputParser) ctx.getBean("fileParser");
pathVerifierFactory=(PathVerifierFactory) ctx.getBean("pathVerifierFactory");
//pathVerifier=(PathVerifer) ctx.getBean("pathVerifer");
return parser.process();
}
public List<Paths> splitXMLData(AutogenPathInfo pathInfo, String pathTypeId) {
List<Paths> pathsList = new ArrayList<Paths>();
List<Paths> pathTypes = pathInfo.getPaths();
System.out.println("Size of 'Paths' :" + pathTypes.size());
Iterator<Paths> pathsItr = pathTypes.iterator();
int typeICnt = 0;
while (pathsItr != null && pathsItr.hasNext()) {
Paths paths = pathsItr.next();
if (paths.getTypeid().equalsIgnoreCase(pathTypeId)) {
pathsList.add(paths);
}
continue;
}
System.out.println("total Path types " + typeICnt);
return pathsList;
}
public void verifyPathInfo() throws Exception {
AutogenPathInfo autogenPathInfo=null;
autogenPathInfo = getXMLPathData();
List<String> pathIdList = getPathTypeIds(autogenPathInfo);
if(pathIdList!=null)
System.out.println("Size of Paths Element in XML : "+pathIdList.size());
if(pathVerifierFactory==null){
log.debug("pathVerifierFactory is null");
}
for(String pathId: pathIdList) {
List<Paths> pathList = splitXMLData(autogenPathInfo, pathId);
PathVerifer pathVerifierObj = pathVerifierFactory.getPathVerifier(pathId);
if(pathVerifierObj==null){
log.debug("pathVerifierObj is null");
}
pathVerifierObj.verifyPaths(pathList);
}
}
private List<String> getPathTypeIds(AutogenPathInfo autogenPathInfo) {
List<String> typeIdList=new ArrayList<String>();
List<Paths> pathsList = autogenPathInfo.getPaths();
Iterator<Paths> pathListIterator = pathsList.iterator();
while(pathListIterator!=null && pathListIterator.hasNext()){
Paths paths = pathListIterator.next();
if(!StringUtils.isBlank(paths.getTypeid())){
typeIdList.add(paths.getTypeid().trim());
}
}
List<String> distinctPathIdList = new ArrayList<String>(new HashSet<String>(typeIdList));
return distinctPathIdList;
}
}
Upvotes: 0
Views: 402
Reputation: 19533
Static fields can not autowired.
You cannot autowire or manually wire static fields in Spring. You'll have to write your own logic to do this
. Try to remove static modifier from your variables.
UPDATE
You could autowire the list of implementation objects as it follows.
<bean id="stage1" class="Stageclass"/>
<bean id="stage2" class="Stageclass"/>
<bean id="stages" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="stage1" />
<ref bean="stage2" />
</list>
</constructor-arg>
</bean>
So inject the implementations to the factory to avoid have N @Autowired annotations
Upvotes: 3