활성 스프링 웹 앱을 스프링 부트 (1.4.2)로 마이그레이션 중입니다.스프링 부트가 @Qualifier 주석을 무시합니다.
빈은 @ImportResource로로드 될 때 XML로 정의됩니다.
내가 시작하는 콩의 4 개가 동일한 객체 BasicDataSource의 인스턴스입니다.
로드 할 스프링을 지정하려면 각 ID를 설정하고 @Qualifier를 사용하여 올바른 bean을 변수에 바인드하십시오.
그러나 그 봄이 내 @Qualifier를 무시하고 던지고 것 같다
" '는 javax.sql.DataSource'가능한 유형의 어떠한 자격 콩 : DataSource1, DataSource2, DataSource3, DataSource4 : 4 예상 하나의 일치 콩을하지만 발견"을PS - @Qualifier가있는 클래스는 추상 클래스이고 인스턴스화에 실패한 클래스는 확장 클래스이지만 @Qualifier는 @Inherited를 가지고 있습니다.
주요 XML
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:task="http://www.springframework.org/schema/task"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring
http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">
<context:load-time-weaver aspectj-weaving="on"/>
<cache:annotation-driven mode="aspectj"/>
<context:property-override location="file:/app/config/dataSourceOverride.cfg"/>
<context:annotation-config />
<bean id="DataSource1" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="userId" value="4" />
</bean>
<bean id="DataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="userId" value="3" />
</bean>
<bean id="DataSource3" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="userId" value="2" />
</bean>
<bean id="DataSource4" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="userId" value="1" />
</bean>
봄 부팅 응용 프로그램
package com.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan("com.app")
@ImportResource("com/app/startup/spring.xml")
public class SpringBootServer {
public static void main(String[] args) {
SpringApplication.run(SpringBootServer.class, args);
}
}
추상 클래스
public abstract class GenericDao {
public GenericDao() {
}
private Logger logger = LoggerFactory.getLogger(GenericDao.class);
@Autowired
@Qualifier("DataSource1")
protected BasicDataSource dataSource1Impl;
@Autowired
@Qualifier("DataSource2")
protected BasicDataSource dataSource2Impl;
@Autowired
@Qualifier("DataSource3")
protected BasicDataSource dataSource3Impl;
@Autowired
@Qualifier("DataSource4")
protected BasicDataSource dataSource4Impl;
}
솔리드 CALSS
@Component("widgetsDao")
public class WidgetsDao extends GenericDao {
##Some methods##
}
예외 아마도 봄 내 @Qualifier 주석을 무시하도록 만들 수있는 어떤
***************************
APPLICATION FAILED TO START
***************************
Description:
Field dataSource1Impl in com.app.dal.GenericDao required a single bean, but 4 were found:
- DataSource1: defined in class path resource [com/app/startup/app-spring.xml]
- DataSource2: defined in class path resource [com/app/startup/app-spring.xml]
- DataSource3: defined in class path resource [com/app/startup/app-spring.xml]
- DataSource4: defined in class path resource [com/app/startup/app-spring.xml]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
?
고맙습니다.
XML 구성에 한정자가 없습니다. –
id가 한정자 이름 역할을합니다 – Ronco
이것이 왜 작동하지 않는 지 모르겠지만 bean 이름이 변수 이름과 일치하면 한정자가 필요하지 않습니다. '@Autowired BasicDataSource datasource4'는'datasource4'라는 이름의 bean으로 연결될 것입니다. – slim