[英]Spring autowired is not working
我有以下類,並且只有在使用ctx.getBean()
時,我才能使用將其注釋為@Component
。 但我想在下面知道。
為什么注釋@autowired
無法正常工作。
我在PathVerifier
遇到錯誤,因為它是界面並遵循工廠模式。 實際的實現對象將僅知道運行時權限。 因此,我為PathVerifier
接口的所有實現添加了@Component
。
我的上下文文件中包含以下內容:
<context:annotation-config />
<context:component-scan base-package="com.xxx.xxx.process">
</context:component-scan>
我的主要課程如下:
@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;
}
}
靜態字段不能自動接線。
您不能在Spring中自動連線或手動連線靜態字段。 您必須編寫自己的邏輯來執行此操作
。 嘗試從變量中刪除靜態修飾符。
更新
您可以按照以下步驟自動裝配實現對象列表。
<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>
因此,將實現注入工廠以避免使用N個 @Autowired批注
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.