公司的项目需要重构---把原来的XML里边的bean配置全都换成注入注解,现在报以下错误:
(MSC service thread 1-2) Context initialization failed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'iataSectionService' defined in "/D:/cmrm/CMRM/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final/standalone/deployments/cmrm-web.war/WEB-INF/lib/cmrm-master-1.0.0-SNAPSHOT.jar/applicationContext-master.xml": 
Cannot resolve reference to bean 'countryService' while setting bean property 'countryService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countryService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private cn.com.acca.cmrm.master.app.service.RegionService cn.com.acca.cmrm.master.app.service.impl.CountryServiceImpl.regionService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'regionService' defined in "/D:/cmrm/CMRM/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final/standalone/deployments/cmrm-web.war/WEB-INF/lib/cmrm-master-1.0.0-SNAPSHOT.jar/applicationContext-master.xml": Cannot resolve reference to bean 'provinceService' while setting bean property 'provinceService'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'provinceService' defined in "/D:/cmrm/CMRM/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final/standalone/deployments/cmrm-web.war/WEB-INF/lib/cmrm-master-1.0.0-SNAPSHOT.jar/applicationContext-master.xml": Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'countryService' of bean class [cn.com.acca.cmrm.master.app.service.impl.ProvinceServiceImpl]: Bean property 'countryService' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter? at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1134) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) [spring-beans-3.2.2.RELEASE.jar:3.2.2.RELEASE]关于CountryServiceImpl和RegionServiceImpl,出现了循环引用:@Service("countryService")
public class CountryServiceImpl extends BaseService<Country> implements CountryService {
    @Autowired
    private RegionService regionService;
    @Autowired
    private AreaService areaService;    /**
     * @param regionService the regionService to set
     */
    public void setRegionService(RegionService regionService) {
        this.regionService = regionService;
    }    /**
     * @param areaService the areaService to set
     */
    public void setAreaService(AreaService areaService) {
        this.areaService = areaService;
    }
@Service("regionService")
public class RegionServiceImpl extends BaseService<Region> implements RegionService {
   @Autowired
    private CountryService countryService;    /**
     * @param countryService the countryService to set
     */
    public void setCountryService(CountryService countryService) {
        this.countryService = countryService;
    }
是循环引用的问题?还是其他问题?欢迎高手解答!SpringBean异常Java注入注解

解决方案 »

  1.   

    把你provinceService贴出来看看,应该是命名或者set方法搞错了。
      

  2.   

    现在我把@autowired放到set上面了:
    接口/*
     * Copyright (c) 2012-2032 Accounting Center of China Aviation(ACCA).
     * All Rights Reserved.
     */
    package cn.com.acca.cmrm.master.app.service;import cn.com.acca.ajaf.core.exception.BusinessException;
    import cn.com.acca.ajaf.core.service.IBaseService;
    import cn.com.acca.cmrm.master.app.entity.Province;/**
     * 省份Service接口层.
     * 
     * @version cmrm-web v1.0
     * @author Wu Yang, 2013-5-27
     */
    public interface ProvinceService extends IBaseService<Province> {    /**
         * 判断省份代码是否存在,存在为true,不存在为false.
         * @param provinceCode 省份代码.
         * @return true or false.
         * @throws BusinessException BusinessException.
         */
        boolean isExisted(String provinceCode) throws BusinessException;
        
        /**
         * 按照地理地区代码查询,是否存在省份信息.
         * @param regionId 地理地区Id.
         * @return List.
         * @throws Exception Exception.
         */
        boolean isExisteProvinceByRegion(String regionId) throws Exception;
        
        /**
         * 添加单条数据.
         * @param province .
         * @param regionCode .
         * @return boolean.
         * @throws Exception .
         */
        boolean saveProvince(Province province, String regionCode) throws BusinessException;
        
        /**
         * 编辑单条数据.
         * @param province .
         * @param regionCode .
         * @throws Exception .
         */
        void updateProvince(Province province, String regionCode) throws BusinessException;
        
        /**
         * 删除单条数据.
         * @param clazz .
         * @param provinceId .
         * @return boolean.
         * @throws Exception .
         */
        boolean deleteProvince(Class<Province> clazz, String provinceId) throws BusinessException;
        
    }实现类:@Service("provinceService")
    public class ProvinceServiceImpl extends BaseService<Province> implements ProvinceService {
       
        private RegionService regionService;    /**
         * @param regionService the regionService to set
         */
        @Autowired
        public void setRegionService(RegionService regionService) {
            this.regionService = regionService;
        }
    //其他实现方法
    }
      

  3.   

    你这业务层有点乱啊,只保留@Service,后面知道的接口都删掉。业务接口应该注入到action层中。
      

  4.   

    我也这么觉得,这是新接手的项目,有很多的serivice互相调用的情况,如果全部重构的话,可能需要花一定的时间。
      

  5.   


    @Controller
    public class CountryAction extends CommonAction {    /** serialVersionUID. */
        private static final long serialVersionUID = 557943713629501919L;    private Country country;
       
        private CountryService countryService;
        /**
         * @param countryService the countryService to set
         */
        @Autowired
        public void setCountryService(CountryService countryService) {
            this.countryService = countryService;
        }
    }@Controller
    public class RegionAction extends CommonAction {
        /** serialVersionUID. */
        private static final long serialVersionUID = 557943713629501919L;    private Region region;
        private Country country;
       
        private RegionService regionService;    /* private CountryProvideImpl countryProvideImpl; */    /**
         * @param regionService the regionService to set
         */
        @Autowired
        public void setRegionService(RegionService regionService) {
            this.regionService = regionService;
        }
    }
      

  6.   

    看看set 和 get方法类型是否一致吧。
      

  7.   

    加了@Autowired注解不需要get,set方法啊