gpt4 book ai didi

java - Spring Autowiring 不起作用

转载 作者:行者123 更新时间:2023-12-01 23:13:03 26 4
gpt4 key购买 nike

我有以下类,并且仅在使用 ctx.getBean() 时才可以使用注释为 @Component 的类。但我想知道下面的情况。

  1. 为什么注释@autowired不起作用。

  2. 我在 PathVerifier 处收到错误,因为它是接口(interface)并遵循工厂模式。实际的实现对象只会知道运行时的正确性。因此,我为 PathVerifier 接口(interface)的所有实现添加了 @Component

我的上下文文件中有以下内容:

 <context:annotation-config />  
<context:component-scan base-package="com.xxx.xxx.process">
</context:component-scan>

我的主要类(class)如下:

@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;

}

}

最佳答案

静态字段无法 Autowiring 。

You cannot autowire or manually wire static fields in Spring. You'll have to write your own logic to do this

。尝试从变量中删除 static 修饰符。

check this out

更新

您可以 Autowiring 实现对象列表,如下所示。

<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>

因此将实现注入(inject)工厂以避免有 N @Autowired 注释

关于java - Spring Autowiring 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21561344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com