我无法在以下代码片段中解决循环依赖关系:
错误
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<>();
}
}
}
代码有什么问题?
我应该重新设计我的组件吗?
最佳答案
将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;
}
}
关于java - 如何解决循环依赖?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50926481/