简体   繁体   English

弹簧自动接线不起作用

[英]Spring autowired is not working

I have the following class and I am able to use which are annotated as @Component only when use ctx.getBean() . 我有以下类,并且只有在使用ctx.getBean()时,我才能使用将其注释为@Component But I want to know below. 但我想在下面知道。

  1. Why annotation @autowired is not working. 为什么注释@autowired无法正常工作。

  2. I am getting error at PathVerifier because it is interface and following factory patter. 我在PathVerifier遇到错误,因为它是界面并遵循工厂模式。 The actual implementation object will only know runtime right. 实际的实现对象将仅知道运行时权限。 So I added @Component for all implementations of my PathVerifier interface. 因此,我为PathVerifier接口的所有实现添加了@Component

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;

    }

}

Static fields can not autowired. 静态字段不能自动接线。

You cannot autowire or manually wire static fields in Spring. 您不能在Spring中自动连线或手动连线静态字段。 You'll have to write your own logic to do this 您必须编写自己的逻辑来执行此操作

. Try to remove static modifier from your variables. 尝试从变量中删除静态修饰符。

check this out 看一下这个

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 因此,将实现注入工厂以避免使用N个 @Autowired批注

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM