弹簧自动装配注释出错

时间:2013-12-07 20:27:28

标签: java spring spring-mvc

您好我在我的应用程序中收到以下错误,我无法解决它:

ERROR localhost-startStop-1 org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'journeyHistoryCSVServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: 
    com.common.application_service.impl.JourneyHistoryPrepareCSVServiceImpl com..common.application_service.impl.JourneyHistoryCSVServiceImpl.journeyHistoryPrepareCSVServiceImpl; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'journeyHistoryPrepareCSVServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.common.application_service.JourneyHistoryService com.common.application_service.impl.JourneyHistoryPrepareCSVServiceImpl.journeyHistoryService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'journeyHistoryServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.common.application_service.JourneyHistoryService com.common.application_service.impl.JourneyHistoryServiceImpl.journeyHistoryService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.common.application_service.JourneyHistoryService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)

我的journeyHistoryCSVServiceImpl是:

@Service("journeyHistoryCSVServiceImpl")
public class JourneyHistoryCSVServiceImpl implements JourneyHistoryCSVService {
    static final Logger logger = LoggerFactory.getLogger(JourneyHistoryCSVServiceImpl.class);

    @Autowired
    protected JourneyHistoryPrepareCSVServiceImpl journeyHistoryPrepareCSVServiceImpl;
    @Autowired
    protected ApplicationContext applicationContext;    
    @Autowired
    protected JourneyHistoryServiceImpl journeyHistoryServiceImpl;

我的JourneyHistoryPrepareCSVServiceImpl是:

@Service("journeyHistoryPrepareCSVServiceImpl")
public class JourneyHistoryPrepareCSVServiceImpl extends AbstractBaseService implements JourneyHistoryPrepareCSVService {
    static final Logger logger = LoggerFactory.getLogger(JourneyHistoryPrepareCSVServiceImpl.class);

    @Autowired
    protected JourneyHistoryService journeyHistoryService;
    @Autowired
    protected SystemParameterService systemParameterService;

更新

你仍然得到同样的错误:

@Service("journeyHistoryCSVServiceImpl")
public class JourneyHistoryCSVServiceImpl implements JourneyHistoryCSVService {
    static final Logger logger = LoggerFactory.getLogger(JourneyHistoryCSVServiceImpl.class);

    @Autowired
    protected JourneyHistoryPrepareCSVServiceImpl journeyHistoryPrepareCSVServiceImpl;
    @Autowired
    protected ApplicationContext applicationContext;    
    @Autowired
    protected JourneyHistoryService journeyHistoryService;

2 个答案:

答案 0 :(得分:0)

问题是,你尝试将bean连接到自身:

//remove those 2 lines
@Autowired
protected JourneyHistoryPrepareCSVServiceImpl journeyHistoryPrepareCSVServiceImpl;

你不需要这个

答案 1 :(得分:0)

您在哪里定义了com.novacroft.nemo.tfl.common.application_service.JourneyHistoryService类型的bean?

仔细查看堆栈跟踪:)

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.novacroft.nemo.tfl.common.application_service.JourneyHistoryService] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency. 
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:626)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)