Configuration of MongoTamplate in SpringBoot+Mongodb Development

  mongodb, question

Background:

The project is managed using maven, and the following is the dependent jar tree structure:

[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

The following is the spring configuration file:

<?  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>

Question:

After executing the typed jar package, report the following error:
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.

My idea is to prove that I have relied on the spring-data-mongodb package through the jar tree and configuration files above.
Then why do you still remind me that MongoTemplate has no definition?


Update:

Thanks to @Kavlez for reminding and @XFHNever for helping, it was not loaded into the configuration file and @ importresource (“applicationcontext.xml”) was added to the entry class.

After successful loading, a nouniquebeandefinitionexception exception is reported, which is described as
No qualifying bean of type [org.springframework.data.mongodb.core.MongoTemplate] is defined: expected single matching bean but found 2: sequenceTemplate,grouponTemplate

But at this time, I injected @ autowire and @ qualifier (“grouptemplate”) (this is no problem in other spring projects), but why still report nouniquebeandefinitionexception?

Well, the cause of the problem was not found, but I was lucky to solve it:
A new bean has been added to the spring configuration file:

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

The project ran ~

Finally, if I am lucky enough to know the cause of this problem, I will update it below. Thank you again.

Judging from the information provided by the subject, neither MongoTemplate declared in the Spring configuration file has been detect.
If you press@XFHNeverThe classmate said that because two Bean of the same type are configured at the same time, it is not possible to identify the targetAutowireWhich is, in this case, the abnormal information should be:

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

I guess I didn’t read the configuration file.