如何解决循环依赖?

时间:2018-06-19 10:44:17

标签: java spring spring-mvc circular-dependency circular-reference

我无法在以下代码段中解析循环依赖:

错误

Jun 19, 2018 3:50:40 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet [supportCenter] in web application [/SupportCenter] threw load() exception
org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'userEmployeeDetailByUserId': Requested bean is currently in creation: Is there an unresolvable circular reference?

代码

@Configuration
//@Import(MasterService.class)
public class ApplicationConfiguration {

    private MasterService masterService;

    public ApplicationConfiguration() {}

    @Autowired
    public void setMasterService(MasterService masterService) {
        this.masterService = masterService;
    }

    @Bean(name="userEmployeeDetailByUserId")
    public Map<Integer, UserEmployeeDetail> userEmployeeDetailByUserId() {
        final Map<Integer, UserEmployeeDetail> userEmployeeDetailByUserId = new HashMap<Integer, UserEmployeeDetail>();
        List<UserEmployeeDetail> userEmployeeDetails = this.masterService.getUserEmployeeDetails();
        for ( UserEmployeeDetail userEmployeeDetail : userEmployeeDetails ) {
            userEmployeeDetailByUserId.put(userEmployeeDetail.getUserId(), userEmployeeDetail);
        }
        return userEmployeeDetailByUserId;
    }
}

MasterServiceImpl

@Service
public class MasterServiceImpl implements MasterService {//, ApplicationContextAware, InitializingBean 

    private static final Logger LOGGER = LogManager.getLogger(MasterServiceImpl.class);

    @Autowired
    private MasterDao masterDao;

    @Resource(name="userEmployeeDetailByUserId")
//  @Autowired
//  @Qualifier(value="userEmployeeDetailByUserId")
    private Map<Integer, UserEmployeeDetail> userEmployeeDetailByUserId;

//  private ApplicationContext applicationContext;
//      

    public MasterServiceImpl() {}
//  public MasterServiceImpl(@Lazy Map<Integer, UserEmployeeDetail> userEmployeeDetailByUserId) {
//      this.userEmployeeDetailByUserId = userEmployeeDetailByUserId;
//  }



//  @Override
//  public void setApplicationContext(ApplicationContext applicationContext)
//          throws BeansException {
//      this.applicationContext = applicationContext;
//      
//  }
//
//  @SuppressWarnings("unchecked")
//  @Override
//  public void afterPropertiesSet() throws Exception {
//      this.userEmployeeDetailByUserId = (Map<Integer, UserEmployeeDetail>) 
//                          applicationContext.getBean("userEmployeeDetailByUserId");
//  }

    public void setMasterDao(MasterDao masterDao) {
        this.masterDao = masterDao;
    }


//  public void setUserEmployeeDetailByUserId(Map<Integer, UserEmployeeDetail> userEmployeeDetailByUserId) {
//      this.userEmployeeDetailByUserId = userEmployeeDetailByUserId;
//  }


    @Override
    public List<UserEmployeeDetail> getUserEmployeeDetails() {
        try {
            return this.masterDao.getUserEmployeeDetails();
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("Message     :- "+e.getMessage());
        LOGGER.error("Root Cause  :- "+e.getCause());
        LOGGER.error("                 ************************************************************");
        return new ArrayList<>();
        }
    }
}

代码有什么问题?
我应该重新设计我的组件吗?

1 个答案:

答案 0 :(得分:1)

getUserEmployeeDetails移至新的Service,并使MasterServiceImpl和Bean userEmployeeDetailByUserId使用此新服务:

@Service
class NewService {

    @Autowired
    private MasterDao masterDao;

    @Override
    public List<UserEmployeeDetail> getUserEmployeeDetails() {
        try {
            return this.masterDao.getUserEmployeeDetails();
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("Message     :- "+e.getMessage());
        LOGGER.error("Root Cause  :- "+e.getCause());
        LOGGER.error("                 ************************************************************");
        return new ArrayList<>();
        }
    }
}

MasterServiceImpl类:

@Service
public class MasterServiceImpl implements MasterService {//, ApplicationContextAware, InitializingBean 

    @Resource(name="userEmployeeDetailByUserId")
    private Map<Integer, UserEmployeeDetail> userEmployeeDetailByUserId;

还有ApplicationConfiguration

@Configuration
//@Import(MasterService.class)
public class ApplicationConfiguration {

    private NewService newService;

    public ApplicationConfiguration() {}

    @Autowired
    public void setNewService(NewService newService) {
        this.newService= newService;
    }

    @Bean(name="userEmployeeDetailByUserId")
    public Map<Integer, UserEmployeeDetail> userEmployeeDetailByUserId() {
        final Map<Integer, UserEmployeeDetail> userEmployeeDetailByUserId = new HashMap<Integer, UserEmployeeDetail>();
        List<UserEmployeeDetail> userEmployeeDetails = this.newService.getUserEmployeeDetails();
        for ( UserEmployeeDetail userEmployeeDetail : userEmployeeDetails ) {
            userEmployeeDetailByUserId.put(userEmployeeDetail.getUserId(), userEmployeeDetail);
        }
        return userEmployeeDetailByUserId;
    }
}