繁体   English   中英

尝试自动连接使用MongoDB使用Morphia的类时出现Spring bean配置错误

[英]Error with spring bean configuraion while trying to autowire class that usind Morphia with MongoDB

我有一个使用外观的控制器,该外观使用DAO以便将一些值保存到DB中,这是结构:

控制器:

@Controller
@RequestMapping("stores/Items")
@ContextConfiguration("classpath:application-context-core-production.xml")
public class ItemsController {

    @Autowired
    IItemsFacade itemsFacade;   
}

正面:

@Service
public class ItemsFacade implements IItemsFacade {

    @Autowired
    ItemDAO itemDAO;
}

DAO:

@Repository
public class ItemDAO extends BasicDAO<Item, ObjectId> implements IItemDAO{


    @Autowired
    public ItemDAO(MongoClient mongoClient, Morphia morphia, String mongoDB) {
        super(mongoClient, morphia, mongoDB);
    }
}

应用程序上下文核心production.xml:

<context:component-scan base-package="com.salegroup.*" />
<!-- Setup Mongo and Morphia -->
<mongo:mongo host="localhost" port="27017" />

<bean class="java.lang.String" id="mongoDB">
    <constructor-arg value="sale" />
</bean>

<bean class="com.mongodb.MongoClient" id="mongo" />
<bean class="org.mongodb.morphia.Morphia" id="morphia" />


<bean class="com.salegroup.persistence.dao.item.impl.ItemDAO" id="itemDAO">
    <constructor-arg ref="mongo" index="0" />
    <constructor-arg ref="morphia" index="1" />
    <constructor-arg ref="mongoDB" index="2" />
</bean>

DAO应该连接到mongoDB,我正在使用xml配置以便创建MongoClient和Morfia。

尝试在嵌入式tomcat服务器中运行时,出现以下错误:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemDAO' defined in file 
[...\ItemDAO.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [org.mongodb.morphia.Morphia]: : No qualifying bean of type [org.mongodb.morphia.Morphia] 
found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. 
Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.mongodb.morphia.Morphia] found for dependency:
 expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]

我错过了什么? 必须说,以防万一我要从外观类中删除一切正常(... dahhh ..)。

除此以外,这些测试也可以正常工作,而且我正在将有效的连接加入数据库。

任何想法?

您的代码中有两个问题

  1. 注释@ContextConfiguration不专用于导入xml配置文件,除非是集成测试

@ContextConfiguration定义了类级别的元数据,用于确定如何加载和配置用于集成测试的ApplicationContext。 API

  1. 永远不会解析xml配置文件,这就是为什么未发现Morphia实例自动装配的原因,即使发生这种情况并且解析了xml配置文件,您也可能会在容器中获得2个ItemDAO实例,这可能是您不想要的

如果您在春季使用基于附件的配置,那么您将要做的是

第一种方法

  • 在您的配置类(用@Configuration注释的类)中,您可以像这样注释它

     @Configuration @ImportResource("classpath:application-context-core-production.xml") public class AppConfig{...} 
  • 您从类ItemDAO中删除了注释@Repository和@Autowired

第二种方式

完全忘记xml配置,并在这样的注释中完成所有操作

@Configuration 
@PropertySource("classpath:mongo.properties")
public class AppConfig{
   // some methods ...
   @Bean
   public Mongo mongo(@Value("${mongo.host.addr}")String host,@Value("${mongo.host.port}")int port){
       return new Mongo(host,port);
   }
   @Bean
   public Morphia morphia(){
      return new Morphia();
   } 
}

在您的存储库类中

    @Repository
public class ItemDAO extends BasicDAO<Item, ObjectId> implements IItemDAO{


    @Autowired
    public ItemDAO(MongoClient mongoClient, Morphia morphia,@Value("${mongo.mongoDB}") String mongoDB) {
        super(mongoClient, morphia, mongoDB);
    }
}

在您的类路径mongo.properties中

mongo.DB=sale
mongo.host.addr=localhost
mongo.host.port=27017

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM