簡體   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