为了账号安全,请及时绑定邮箱和手机立即绑定

SpringBoot + Mongodb 开发中关于MongoTamplate的配置问题

SpringBoot + Mongodb 开发中关于MongoTamplate的配置问题

侃侃无极 2019-04-16 20:27:09
背景:项目使用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配置文件:问题:执行打好的jar包后,报如下错误:Noqualifyingbeanoftype[org.springframework.data.mongodb.core.MongoTemplate]foundfordependency:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.我的想法是通过上面的jar树和配置文件,证明我已经依赖了spring-data-mongodb包。那为什么仍然会提示我MongoTemplate没有定义?更新:感谢@Kavlez同学提醒和@XFHNever同学帮忙,的确是没有加载到配置文件,给入口类添加@ImportResource("applicationContext.xml")加载成功后,又报NoUniqueBeanDefinitionException异常,描述为Noqualifyingbeanoftype[org.springframework.data.mongodb.core.MongoTemplate]isdefined:expectedsinglematchingbeanbutfound2:sequenceTemplate,grouponTemplate但此时我注入的方式是@Autowired和@Qualifier("grouponTemplate")配合(在其他spring项目中这样做没有问题的)但为什么仍然会报NoUniqueBeanDefinitionException呢?好吧,问题的原因没有找到,倒是让我侥幸解决了:在spring配置文件中新添加了一个bean:项目跑了起来~最后,如果有幸得知该问题的原因,我更新在这个问题下面。再次感谢。
查看完整描述

2 回答

?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

从题主提供的信息来看,在Spring配置文件中声明的两个MongoTemplate都没有detect到。
如果按@XFHNever同学所说,是因为同时配置了两个相同类型的Bean,而无法识别要Autowire的是哪一个,这种情况下异常信息应该是:
org.springframework.beans.factory.NoUniqueBeanDefinitionException:Noqualifyingbeanoftype
[org.springframework.data.mongodb.core.MongoTemplate]isdefined:
expectedsinglematchingbeanbutfound2
我猜测可能是没有读到该配置文件。
                            
查看完整回答
反对 回复 2019-04-16
?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

这个应该是因为你在配置文件中配置了两个MongoTemplate,然后Spring不知道应该加载哪个,可以考虑同时使用@Autowired和@Qualifier来指定具体加载哪个MongoTemplate。
PS:
@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入;@Resource(name="studentDao")
@Autowired默认是按照类型装配注入的,如果想按照名称来转配注入,则需要结合@Qualifier一起使用;@Autowired@Qualifier("studentDao")
@Resource注解是由J2EE提供,而@Autowired是由Spring提供,故减少系统对spring的依赖建议使用@Resource的方式;
                            
查看完整回答
反对 回复 2019-04-16
  • 2 回答
  • 0 关注
  • 1644 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信