简体   繁体   English

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

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

I have a controller that is using a facade that using DAO in order to save some values into the DB here is the stracture: 我有一个使用外观的控制器,该外观使用DAO以便将一些值保存到DB中,这是结构:

Controller: 控制器:

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

    @Autowired
    IItemsFacade itemsFacade;   
}

Facade: 正面:

@Service
public class ItemsFacade implements IItemsFacade {

    @Autowired
    ItemDAO itemDAO;
}

DAO: DAO:

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


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

application-context-core-production.xml: 应用程序上下文核心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>

The DAO should connect into the mongoDB, I'm using the xml configuration inorder to create the MongoClient and Morfia. DAO应该连接到mongoDB,我正在使用xml配置以便创建MongoClient和Morfia。

while trying to run in in embeded tomcat server i'm getting the following error: 尝试在嵌入式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]

What did i miss? 我错过了什么? must say in case i'm removing in from the facade class everything is working (...dahhh..). 必须说,以防万一我要从外观类中删除一切正常(... dahhh ..)。

In addition to that tests are working as well and i'm getting a valid conneciton into the DB. 除此以外,这些测试也可以正常工作,而且我正在将有效的连接加入数据库。

Any idea? 任何想法?

there are two problems in your code 您的代码中有两个问题

  1. the annotation @ContextConfiguration is not dedicated for importing xml configuration files except in the case of integratointesting 注释@ContextConfiguration不专用于导入xml配置文件,除非是集成测试

@ContextConfiguration defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests. @ContextConfiguration定义了类级别的元数据,用于确定如何加载和配置用于集成测试的ApplicationContext。 API API

  1. The xml configuration file never gets parsed that is why Morphia instance is not found to be autowired and even if this happens and your xml configuration file gets parsed you will get 2 instances of ItemDAO in your container wich probably you don't want 永远不会解析xml配置文件,这就是为什么未发现Morphia实例自动装配的原因,即使发生这种情况并且解析了xml配置文件,您也可能会在容器中获得2个ItemDAO实例,这可能是您不想要的

if you are using Annptation based configuration for spring here is what you are going to do 如果您在春季使用基于附件的配置,那么您将要做的是

First Approach 第一种方法

  • in your Configuration class(the one annotated with @Configuration) you annotate it like this 在您的配置类(用@Configuration注释的类)中,您可以像这样注释它

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

Second Aproach 第二种方式

forget totally about xml config and do it all in annotations like this 完全忘记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();
   } 
}

in your repository class 在您的存储库类中

    @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);
    }
}

in your classpath mongo.properties 在您的类路径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