Spring自动装配不起作用

时间:2014-02-04 19:15:19

标签: java spring

我有以下课程,我只能在使用@Component时使用注释为ctx.getBean()的课程。但我想在下面知道。

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

  2. 我在PathVerifier收到错误,因为它是界面并跟随工厂模式。实际的实现对象只知道运行时权限。所以我为@Component界面的所有实现添加了PathVerifier

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

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

1 个答案:

答案 0 :(得分:3)

静态字段无法自动装配。

  

您无法在Spring中自动装配或手动连​​接静态字段。你会   必须编写自己的逻辑来执行此操作

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

check this out

<强>更新

您可以按照以下步骤自动装配实施对象列表。

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

相关问题