SpringBoot + Mongodb 开发中关于MongoTamplate的配置问题
黄舟
黄舟 2017-04-24 09:13:28
0
3
572

背景:

项目使用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>

项目跑了起来~

最后,如果有幸得知该问题的原因,我更新在这个问题下面。再次感谢。

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

모든 응답(3)
伊谢尔伦

제보자가 제공한 정보에 따르면 Spring 구성 파일에 선언된 두 개의 MongoTemplate이 모두 감지되지 않았습니다.
@XFHNever가 말했듯이 동일한 유형의 두 Bean이 동시에 구성되어 어느 것이 Autowire인지 식별할 수 없기 때문인 경우 이 경우 예외 메시지는

이어야 합니다.

org.springframework.beans.factory.NoUniqueBeanDefinitionException: 해당 유형의 Bean이 없습니다
[org.springframework.data.mongodb.core.MongoTemplate]이 정의되었습니다:
하나의 일치하는 빈을 예상했지만 2개를 찾았습니다

제 생각에는 구성 파일을 읽지 않았을 수도 있습니다.

黄舟

구성 파일에 두 개의 MongoTemplate을 구성했는데 Spring이 어떤 것을 로드해야 할지 모르기 때문일 수 있습니다. 로드할 MongoTemplate을 지정하기 위해 @Autowired와 @Qualifier를 동시에 사용하는 것을 고려할 수 있습니다.

PS:

  1. @Resource는 기본적으로 이름에 따라 조립되어 주입됩니다. 이름과 일치하는 Bean을 찾을 수 없는 경우에만 @Resource(name="studentDao")
  2. @Autowired는 기본적으로 타입에 따라 구성되어 주입되는데, 이름에 따라 할당하고 주입하려면 @Qualifier와 함께 사용해야 합니다.("studentDao")
  3. @Resource 주석은 J2EE에서 제공되며 @Autowired는 Spring에서 제공되므로 스프링에 대한 시스템 의존성을 줄이기 위해 @Resource를 사용하는 것이 좋습니다.
洪涛

http://www.jianshu.com/p/6c82e6e21e5d
제가 전에 정리한 글을 읽어보실 수 있습니다

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!