@Configuration类提供给定限定符的bean,但不提供其他bean

时间:2013-11-25 11:49:42

标签: java spring mongodb

我有两个配置类,如下面的

@Configuration
public class EnvConfiguration {

    @Autowired
    public RAMTaskDAO   ramTaskDAO;

    @Autowired
    public MongoTaskDAO mongoTaskDAO;

    @Bean
    @Qualifier("ram")
    public TaskDAO ramTaskDAO() {
        return ramTaskDAO;
    }

    @Bean
    @Qualifier("mongo")
    public TaskDAO mongoTaskDAO() {
        return mongoTaskDAO;
    }
}

@Configuration
@Profile("dev")
public class DevEnvConfiguration extends EnvConfiguration {

  @Bean
  @Qualifier("taskDao")
  public TaskDAO taskDAO() {
    return ramTaskDAO;
  }

}

我的主要课程如下所示:

@Component
public class TaskDAOUsageDemonstrator {

  @Autowired
  @Qualifier("taskDao")
  public TaskDAO taskDao;

  @Autowired
  @Qualifier("ram")
  public TaskDAO ramTaskDao;

  @Autowired
  @Qualifier("mongo")
  public TaskDAO mongoTaskDao;

  @ForSpring
  private TaskDAOUsageDemonstrator() {
  }

  public static void main(String[] args) {
    GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    context.getEnvironment().setActiveProfiles("dev");
    context.load("applicationContext.xml");
    context.refresh();
  }

问题:
当Spring尝试实例化TaskDAOUsageDemonstrator时,它无法找到限定符的TaskDAO" mongo"由于某些原因。我可以通过其他两个限定符来获得Bean,但不仅仅是使用" mongo"。我试图将限定符名称重命名为mongo1,但事情并没有改变。

更新的 我试图在DevEnvConfiguration中返回mongoTaskDao而不是ramTaskDAO,用于限定符' taskDao',我能够得到一个mongoTaskDao bean。出于某种原因,键入:TaskDAO with qualifier" mongo"只会导致问题。

异常追踪:

 Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'taskDAOUsageDemonstrator': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.mb.taskmanager.TaskDAO com.mb.taskmanager.TaskDAOUsageDemonstrator.mongoTaskDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mb.taskmanager.TaskDAO] 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), @org.springframework.beans.factory.annotation.Qualifier(value=mongo)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:270)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1113)
    at com.mb.taskmanager.TaskDAOUsageDemonstrator.demoRamTaskDaoAutowired(TaskDAOUsageDemonstrator.java:84)
    at com.mb.taskmanager.TaskDAOUsageDemonstrator.main(TaskDAOUsageDemonstrator.java:53)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.mb.taskmanager.TaskDAO com.mb.taskmanager.TaskDAOUsageDemonstrator.mongoTaskDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mb.taskmanager.TaskDAO] 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), @org.springframework.beans.factory.annotation.Qualifier(value=mongo)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    ... 11 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.mb.taskmanager.TaskDAO] 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), @org.springframework.beans.factory.annotation.Qualifier(value=mongo)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:947)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:816)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
    ... 13 more

更不用说RAMTaskDAO和MongoTaskDAO完全相同:

@Component
@Scope("singleton")
public class RAMTaskDAO implements TaskDAO {

  private ConcurrentHashMap<String, BackendTask> taskMap;

  public RAMTaskDAO() {
    taskMap = new ConcurrentHashMap<String, BackendTask>();
  }

  ...
}

@Component
@Scope("singleton")
public class MongoTaskDAO implements TaskDAO {

  @Autowired
  public MongoOperations mongoOperations;

  public MongoTaskDAO() {
  }

  ...
}

更新:
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xmlns:rabbit="http://www.springframework.org/schema/rabbit"
    xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/rabbit
          http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <import resource="common-spring.xml" />
    <import resource="mongo-spring.xml" />
    <import resource="rabbit-spring.xml" />

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="test/app.properties" />
    </bean>

    <context:component-scan base-package="com.mb" />
    <context:annotation-config />

</beans>

mongo-spring.xml

    <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-3.0.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="prod/app.properties" />
    </bean>

    <context:component-scan base-package="com.mb.taskmanager.db" />
    <context:annotation-config />

    <mongo:mongo host="${mongo.host}" port="${mongo.port}" />
    <mongo:db-factory dbname="${mongo.dbname}" mongo-ref="mongo"
        username="${mongo.user}" password="${mongo.pwd}" />

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    </bean>
</beans>

app.properties

mongo.host=myhost.com
mongo.port=27017
mongo.dbname=framework-unittesting-db
mongo.user=xxxxxxx
mongo.pwd=xxxxx

rabbit.host=localhost
rabbit.user=xxxx
rabbit.pwd=xxxxxxxxxx

TaskManager
TaskManager就像这样开始

public class TaskManager {
  @Autowired
  @Qualifier("taskDao")
  protected TaskDAO         taskDAO;

  @Autowired
  private TaskBrokerFactory taskBrokerFactory;

  private TaskManager() {
  }

  ....

}

0 个答案:

没有答案