搜尋

首頁  >  問答  >  主體

SpringBoot + Mongodb 開發中關於MongoTamplate的設定問題

背景:

專案使用maven管理,如下是依賴jar樹結構:

[INFO] xxx:groupon-boot:jar:1.0
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:1.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:1.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot:jar:1.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:1.2.0.RELEASE:compile
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.7:compile
[INFO] |  |  |  +- org.slf4j:log4j-over-slf4j:jar:1.7.7:compile
[INFO] |  |  |  \- ch.qos.logback:logback-classic:jar:1.1.2:compile
[INFO] |  |  |     \- ch.qos.logback:logback-core:jar:1.1.2:compile
[INFO] |  |  \- org.yaml:snakeyaml:jar:1.14:runtime
[INFO] |  +- org.springframework.boot:spring-boot-starter-tomcat:jar:1.2.0.RELEASE:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.0.15:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.0.15:compile
[INFO] |  |  +- org.apache.tomcat.embed:tomcat-embed-logging-juli:jar:8.0.15:compile
[INFO] |  |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.0.15:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.4.4:compile
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.4.4:compile
[INFO] |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.4.4:compile
[INFO] |  +- org.hibernate:hibernate-validator:jar:5.1.3.Final:compile
[INFO] |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.1.3.GA:compile
[INFO] |  |  \- com.fasterxml:classmate:jar:1.0.0:compile
[INFO] |  +- org.springframework:spring-core:jar:4.1.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-web:jar:4.1.3.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-aop:jar:4.1.3.RELEASE:compile
[INFO] |  |  |  \- aopalliance:aopalliance:jar:1.0:compile
[INFO] |  |  +- org.springframework:spring-beans:jar:4.1.3.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:4.1.3.RELEASE:compile
[INFO] |  \- org.springframework:spring-webmvc:jar:4.1.3.RELEASE:compile
[INFO] |     \- org.springframework:spring-expression:jar:4.1.3.RELEASE:compile
[INFO] +- org.springframework.boot:spring-boot-starter-redis:jar:1.2.0.RELEASE:compile
[INFO] |  +- org.springframework:spring-context-support:jar:4.1.3.RELEASE:compile
[INFO] |  +- org.springframework:spring-tx:jar:4.1.3.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-redis:jar:1.4.1.RELEASE:compile
[INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.7:compile
[INFO] |  \- redis.clients:jedis:jar:2.5.2:compile
[INFO] |     \- org.apache.commons:commons-pool2:jar:2.2:compile
[INFO] +- org.springframework.boot:spring-boot-starter-data-jpa:jar:1.2.0.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-aop:jar:1.2.0.RELEASE:compile
[INFO] |  |  +- org.aspectj:aspectjrt:jar:1.8.4:compile
[INFO] |  |  \- org.aspectj:aspectjweaver:jar:1.8.4:compile
[INFO] |  +- org.springframework.boot:spring-boot-starter-jdbc:jar:1.2.0.RELEASE:compile
[INFO] |  |  +- org.springframework:spring-jdbc:jar:4.1.3.RELEASE:compile
[INFO] |  |  \- org.apache.tomcat:tomcat-jdbc:jar:8.0.15:compile
[INFO] |  |     \- org.apache.tomcat:tomcat-juli:jar:8.0.15:compile
[INFO] |  +- org.hibernate:hibernate-entitymanager:jar:4.3.7.Final:compile
[INFO] |  |  +- org.jboss.logging:jboss-logging-annotations:jar:1.2.0.Beta1:compile
[INFO] |  |  +- org.hibernate:hibernate-core:jar:4.3.7.Final:compile
[INFO] |  |  |  +- antlr:antlr:jar:2.7.7:compile
[INFO] |  |  |  \- org.jboss:jandex:jar:1.1.0.Final:compile
[INFO] |  |  +- org.hibernate.common:hibernate-commons-annotations:jar:4.0.5.Final:compile
[INFO] |  |  +- org.hibernate.javax.persistence:hibernate-jpa-2.1-api:jar:1.0.0.Final:compile
[INFO] |  |  +- org.jboss.spec.javax.transaction:jboss-transaction-api_1.2_spec:jar:1.0.0.Final:compile
[INFO] |  |  \- org.javassist:javassist:jar:3.18.1-GA:compile
[INFO] |  +- org.springframework:spring-orm:jar:4.1.3.RELEASE:compile
[INFO] |  +- org.springframework.data:spring-data-jpa:jar:1.7.1.RELEASE:compile
[INFO] |  |  +- org.springframework.data:spring-data-commons:jar:1.9.1.RELEASE:compile
[INFO] |  |  \- org.slf4j:jcl-over-slf4j:jar:1.7.7:compile
[INFO] |  \- org.springframework:spring-aspects:jar:4.1.3.RELEASE:compile
[INFO] +- org.hsqldb:hsqldb:jar:2.3.2:runtime
[INFO] \- witcool:groupon-mongodb:jar:1.0:compile
[INFO]    +- witcool:yqsp-mongo-common:jar:1.0:compile
[INFO]    |  +- witcool:yqsp-common:jar:1.0:compile
[INFO]    |  |  +- org.apache.httpcomponents:httpcore:jar:4.3.2:compile
[INFO]    |  |  +- org.apache.httpcomponents:httpclient:jar:4.3.6:compile
[INFO]    |  |  +- org.apache.httpcomponents:httpmime:jar:4.3.6:compile
[INFO]    |  |  \- commons-configuration:commons-configuration:jar:1.6:compile
[INFO]    |  |     +- commons-collections:commons-collections:jar:3.2.1:compile
[INFO]    |  |     +- commons-digester:commons-digester:jar:2.1:compile
[INFO]    |  |     |  \- commons-beanutils:commons-beanutils:jar:1.9.2:compile
[INFO]    |  |     \- commons-beanutils:commons-beanutils-core:jar:1.8.0:compile
[INFO]    |  +- org.springframework:spring-test:jar:4.1.3.RELEASE:compile
[INFO]    |  +- org.springframework.security:spring-security-core:jar:3.2.5.RELEASE:compile
[INFO]    |  +- org.springframework.security:spring-security-web:jar:3.2.5.RELEASE:compile
[INFO]    |  +- org.springframework.security:spring-security-config:jar:3.2.5.RELEASE:compile
[INFO]    |  +- org.springframework.security:spring-security-taglibs:jar:3.2.5.RELEASE:compile
[INFO]    |  |  \- org.springframework.security:spring-security-acl:jar:3.2.5.RELEASE:compile
[INFO]    |  +- org.springframework.security.oauth:spring-security-oauth2:jar:1.0.5.RELEASE:compile
[INFO]    |  +- javax.annotation:com.springsource.javax.annotation:jar:1.0.0:compile
[INFO]    |  +- javax.inject:javax.inject:jar:1:compile
[INFO]    |  +- mysql:mysql-connector-java:jar:5.1.34:compile
[INFO]    |  +- commons-dbcp:commons-dbcp:jar:1.4:compile
[INFO]    |  +- org.slf4j:slf4j-log4j12:jar:1.7.7:runtime
[INFO]    |  +- log4j:log4j:jar:1.2.17:compile
[INFO]    |  +- org.glassfish.jersey.media:jersey-media-json-jackson:jar:2.13:compile
[INFO]    |  |  \- org.glassfish.jersey.core:jersey-common:jar:2.13:compile
[INFO]    |  |     +- javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile
[INFO]    |  |     +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO]    |  |     +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.13:compile
[INFO]    |  |     +- org.glassfish.hk2:hk2-api:jar:2.3.0-b10:compile
[INFO]    |  |     |  +- org.glassfish.hk2:hk2-utils:jar:2.3.0-b10:compile
[INFO]    |  |     |  \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.3.0-b10:compile
[INFO]    |  |     +- org.glassfish.hk2.external:javax.inject:jar:2.3.0-b10:compile
[INFO]    |  |     +- org.glassfish.hk2:hk2-locator:jar:2.3.0-b10:compile
[INFO]    |  |     \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile
[INFO]    |  +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.3.2:compile
[INFO]    |  +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.3.2:compile
[INFO]    |  +- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.3.2:compile
[INFO]    |  \- cglib:cglib:jar:2.2.2:compile
[INFO]    |     \- asm:asm:jar:3.3.1:compile
[INFO]    +- org.springframework.data:spring-data-mongodb:jar:1.6.1.RELEASE:compile
[INFO]    |  \- org.mongodb:mongo-java-driver:jar:2.12.4:compile
[INFO]    +- commons-httpclient:commons-httpclient:jar:3.1:compile
[INFO]    |  \- commons-logging:commons-logging:jar:1.0.4:compile
[INFO]    +- commons-lang:commons-lang:jar:2.6:compile
[INFO]    +- commons-codec:commons-codec:jar:1.6:compile
[INFO]    +- org.codehaus.jackson:jackson-core-asl:jar:1.8.4:compile
[INFO]    +- org.codehaus.jackson:jackson-mapper-asl:jar:1.8.4:compile
[INFO]    +- dom4j:dom4j:jar:1.6.1:compile
[INFO]    |  \- xml-apis:xml-apis:jar:1.0.b2:compile
[INFO]    \- javax.servlet:javax.servlet-api:jar:3.1.0:compile

如下是spring設定檔:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c" 
xmlns:repository="http://www.springframework.org/schema/data/repository"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd  
http://www.springframework.org/schema/data/repository
http://www.springframework.org/schema/data/repository/spring-repository.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">


<context:component-scan base-package="com.xxx.groupon" />
<!-- Factory bean that creates the Mongo instance
 -->
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
    <property name="host" value="localhost" />
    <property name="port" value="27017" />
</bean>
<bean id="grouponTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo" />
    <constructor-arg name="databaseName" value="groupon3" />
</bean>
<bean id="sequenceTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg name="mongo" ref="mongo" />
    <constructor-arg name="databaseName" value="sequences3" />
</bean>

問題:

執行打好的jar包後,報如下錯誤:
No qualifying bean of type [org.springframework.data.mongodb.core.MongoTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.

我的想法是透過上面的jar樹和設定文件,證明我已經依賴了spring-data-mongodb套件。
那為什麼還是會提示我MongoTemplate沒有定義?


更新:

感謝@Kavlez同學提醒和@XFHNever同學幫忙,的確是沒有加載到配置文件,給入口類添加@ImportResource("applicationContext.xml")

載入成功後,又報NoUniqueBeanDefinitionException異常,描述為
No qualifying bean of type [org.springframework.data.mongodb.core.MongoTemplate] is defined: expected single matching bean but found 2: sequenceTemplate,grouponTemplate

但此時我注入的方式是@Autowired和@Qualifier("grouponTemplate")配合(在其他spring專案中這樣做沒有問題的)但為什麼仍然會報NoUniqueBeanDefinitionException呢?

好吧,問題的原因沒有找到,倒是讓我僥倖解決了:
在spring設定檔中新加入了一個bean:

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongo" ref="mongo" />
        <constructor-arg name="databaseName" value="test" />
    </bean>

專案跑了起來~

最後,如果有幸得知問題的原因,我更新在這個問題下面。再次感謝。

黄舟黄舟2802 天前639

全部回覆(3)我來回復

  • 伊谢尔伦

    伊谢尔伦2017-04-24 09:15:28

    從題主提供的資訊來看,在Spring設定檔中聲明的兩個MongoTemplate都沒有detect到。
    如果按 @XFHNever 同學所說,是因為同時配置了兩個相同類型的Bean,而無法辨識要Autowire的是哪一個,這種情況下異常訊息應該是:

    org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type
    [org.springframework.data.mongodb.core.MongoTemplate] is defined:
    expected single matching bean but found 2

    我猜可能是沒有讀到該設定檔。

    回覆
    0
  • 黄舟

    黄舟2017-04-24 09:15:28

    這個應該是因為你在設定檔中配置了兩個MongoTemplate,然後Spring不知道應該載入哪一個,可以考慮同時使用@Autowired和@Qualifier來指定具體載入哪個MongoTemplate。

    PS:

    1. @Resource預設是按照名稱來組裝注入的,只有當找不到與名稱匹配的bean才會按照類型來組裝注入;@Resource(name="studentDao")
    2. @Autowired預設是按照類型組裝注入的,如果想按照名稱來轉配注入,則需要結合 @Qualifier一起使用;@Autowired @Qualifier("studentDao")
    3. @Resource註解是由J2EE提供,而@Autowired是由Spring提供,故減少系統對spring的依賴建議使用 @Resource的方式;

    回覆
    0
  • PHP中文网

    PHP中文网2017-04-24 09:15:28

    http://www.jianshu.com/p/6c82e6e21e5d
    我之前總結的文章,你可以看看

    回覆
    0
  • 取消回覆