2 개의 bean을 작성하려는 Spring Boot 기반 응용 프로그램을 개발 중입니다. 하나는 'Oracle'데이터베이스를 가리키고, 다른 하나는 ' 다른 하나는 하이브를 가리킬 것입니다. 동일한 유형의 2 bean 사용 : Spring의 javax.sql.DataSource
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration':
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private javax.sql.DataSource
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.dataSource;
nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [javax.sql.DataSource] is defined: expected
single matching bean but found 2: metadataDataSource,hiveDataSource
은 주로 그들 모두는 javax.sql.DataSource에서 상속 때문에 :
public @Bean
BoneCPDataSource metadataDataSource() {
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
boneCPDataSource.setDriverClass(getDriver());
boneCPDataSource.setJdbcUrl(getJdbcUrl());
boneCPDataSource.setUser(getUser());
boneCPDataSource.setPassword(getPassword());
boneCPDataSource.setMaxConnectionsPerPartition(5);
boneCPDataSource.setPartitionCount(5);
return boneCPDataSource;
}
public @Bean
BasicDataSource hiveDataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
// Note: In a separate command window, use port forwarding like this:
//
// ssh -L 127.0.0.1:9996:<server>:<port> -l <userid> <server>
//
// and then login as the generic user.
basicDataSource.setDriverClassName("org.apache.hadoop.hive.jdbc.HiveDriver");
basicDataSource.setUrl("jdbc:hive://127.0.0.1:9996:10000/mytable");
return basicDataSource;
}
문제는 내가이 무엇입니까 시작에 있습니다 : 다음과 같이 내가 그들을 선언했습니다. 이 문제를 해결하는 가장 좋은 방법은 무엇입니까?
편집 :
는 지금은 다음과 같이 그들을 선언했습니다
public @Bean (name="metadataDataSource")
BoneCPDataSource metadataDataSource() {
BoneCPDataSource boneCPDataSource = new BoneCPDataSource();
boneCPDataSource.setDriverClass(getDriver());
boneCPDataSource.setJdbcUrl(getJdbcUrl());
boneCPDataSource.setUser(getUser());
boneCPDataSource.setPassword(getPassword());
boneCPDataSource.setMaxConnectionsPerPartition(5);
boneCPDataSource.setPartitionCount(5);
return boneCPDataSource;
}
public @Bean (name="hiveDataSource")
BasicDataSource hiveDataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
// Note: In a separate command window, use port forwarding like this:
//
// ssh -L 127.0.0.1:9996:<server>:<port> -l <userid> <server>
//
// and then login as the generic user.
basicDataSource.setDriverClassName("org.apache.hadoop.hive.jdbc.HiveDriver");
basicDataSource.setUrl("jdbc:hive://127.0.0.1:9996:10000/mytable");
return basicDataSource;
}
을 그리고이 예외를 가지고 :
Exception in thread "main"
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name
'org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration':
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private javax.sql.DataSource
org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration.dataSource;
nested exception is
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No
qualifying bean of type [javax.sql.DataSource] is defined: expected
single matching bean but found 2: metadataDataSource,hiveDataSource
다른 클래스는 이러한 콩 참조됩니다 다음과 같습니다 :
개 공용 클래스는 MetadataProcessorImpl는 MetadataProcessor를 구현 {@Bean
를 사용하는 경우
@Autowired
@Qualifier("metadataDataSource")
BoneCPDataSource metadataDataSource;
@Controller 공용 클래스 HiveController {
@Autowired
@Qualifier("hiveDataSource")
BasicDataSource hiveDataSource;
Luiggi - 난 당신이 제안하는지 모르겠습니다. 제 질문이 적절하지 않았습니까? 나는 이것을 묻기 전에 대답을 봤다. 그러나 내가 얻은 결과는 실제로 도움이되지 못했다. 그러나 나는 그들 모두를 통해 많은 시간을 할애하지 않았다고 고백해야한다. 그게 내가해야 할 일 이니? – DilTeam
귀하의 질문에 가장 유용한 게시물을 답변으로 받아 들여야한다는 의미입니다. 미안 해요. 내 말이 그걸 표현하지 않았다면. –
내 의견을 다시 말하게하십시오 : 귀하의 질문에 [답변으로 가장 많이 도움이 된 게시물 수락] (http://meta.stackexchange.com/a/118694/182862)해야합니다. 귀하의 프로필을 검토 한 후에는 아무 것도받지 않았으므로, 답변을 수락하는 데 시간이 좀 걸릴 것으로 예상됩니다 (있는 경우). 이 작동 방식을 설명하는 링크가 있습니다. –