项目使用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>
项目跑了起来~
最后,如果有幸得知该问题的原因,我更新在这个问题下面。再次感谢。
伊谢尔伦2017-04-24 09:15:28
From the information provided by the subject, neither of the two MongoTemplates declared in the Spring configuration file were detected.
If @XFHNever said it is because two beans of the same type are configured at the same time and it is impossible to identify which one is needed, in this case the exception message should be: Autowire
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of typeMy guess is that the configuration file may not have been read.[org.springframework.data.mongodb.core.MongoTemplate] is defined:
expected single matching bean but found 2
黄舟2017-04-24 09:15:28
This is probably because you have configured two MongoTemplates in the configuration file, and Spring does not know which one should be loaded. You can consider using @Autowired and @Qualifier at the same time to specify which MongoTemplate to load.
PS:
PHP中文网2017-04-24 09:15:28
http://www.jianshu.com/p/6c82e6e21e5d
You can read the articles I summarized before