gpt4 book ai didi

java - 添加多个 mongoTemplates 到 servlet-context

转载 作者:太空宇宙 更新时间:2023-11-04 15:18:55 25 4
gpt4 key购买 nike

我正在尝试连接同一台机器上的两个不同数据库,我已引用 here

但是我遇到了以下异常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'locationServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.data.mongodb.core.MongoTemplate com.cheasyy.cofinding.service.profile.LocationServiceImpl.mt; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [org.springframework.data.mongodb.core.MongoTemplate] is defined: expected single matching bean but found 2: [mongoTemplate, readTemplate]
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)

我的 servlet-context.xml 看起来像

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

<!-- DispatcherServlet Context: defines this servlet's request-processing
infrastructure -->

<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven />
<beans:bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

<!-- Handles HTTP GET requests for /resources/** by efficiently serving
up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />

<!-- Resolves views selected for rendering by @Controllers to .jsp resources
in the /WEB-INF/views directory -->
<beans:bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>

<context:component-scan
base-package="com.cheasyy.cofinding,com.cheasyy.cofinding.controller,com.cheasyy.cofinding.model.profile,com.cheasyy.cofinding.service.profile" />


<!-- Mongo settings -->
<mongo:mongo id="mongo" host="192.168.1.3" port="27017" />

<beans:bean id="mongoTemplate"
class="org.springframework.data.mongodb.core.MongoTemplate">
<beans:constructor-arg ref="mongo" />
<beans:constructor-arg name="databaseName"
value="cofinding" />
</beans:bean>
<mongo:repositories
base-package="com.cheasyy.cofinding,com.cheasyy.cofinding.controller,com.cheasyy.cofinding.model.profile,com.cheasyy.cofinding.service.profile" />

<!-- adding another mongo template -->
<!-- Mongo settings -->
<mongo:mongo id="read" host="192.168.1.3" port="27017" />

<beans:bean id="readTemplate"
class="org.springframework.data.mongodb.core.MongoTemplate">
<beans:constructor-arg ref="read" />
<beans:constructor-arg name="databaseName"
value="readDB" />
</beans:bean>
<mongo:repositories
base-package="com.cheasyy.cofinding,com.cheasyy.cofinding.controller,com.cheasyy.cofinding.model.profile,com.cheasyy.cofinding.service.profile" />




</beans:beans>

我已经尝试过

@Autowired
@Qualifier("readTemplate")
private MongoTemplate mt1;

@Autowired
@Qualifier("mongoTemplate")
private MongoTemplate mt;

最佳答案

当有多个可以 Autowiring 的bean时,需要添加@Qualifier注解指定要注入(inject)的实例的名称:

@Autowired
@Qualifier("someBeanName")
private SomeType someType;

文档的相关部分:Fine-tuning annotation-based autowiring with qualifiers

关于java - 添加多个 mongoTemplates 到 servlet-context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20627583/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com