Java 弹簧&x27;s@限定符不起作用
有两个数据源配置类:Java 弹簧&x27;s@限定符不起作用,java,spring,Java,Spring,有两个数据源配置类: @Configuration @EnableJpaRepositories( basePackages = {"com.proj.killbill.repository.kb", "com.proj.service.base.repository"}, entityManagerFactoryRef = "kbEntityManagerFactory", transactionManagerRef = "kbTransacti
@Configuration
@EnableJpaRepositories(
basePackages = {"com.proj.killbill.repository.kb", "com.proj.service.base.repository"},
entityManagerFactoryRef = "kbEntityManagerFactory",
transactionManagerRef = "kbTransactionManager")
@EnableJpaAuditing
@Profile({"dev", "prod"})
public class PersistenceKbConfig {
@Value("${db.user:default}")
private String user;
@Value("${db.password:default}")
private String password;
@Value("${db.url:default}")
private String jdbcUrl;
@Value("${app.kb.ds.jndi.name}")
private String kbDsJndiName;
@Bean(name = "kbDataSource")
@Profile("dev")
public DataSource devKbDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl(jdbcUrl);
dataSource.setUsername(user);
dataSource.setPassword(password);
return dataSource;
}
@Bean(name = "kbDataSource")
@Profile("prod")
public DataSource prodKbDataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
return dsLookup.getDataSource(kbDsJndiName);
}
@SuppressWarnings("Duplicates")
@Bean(name = "kbEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
@Qualifier("kbDataSource") DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.proj.service.base.domain");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
return properties;
}
@Bean(name = "kbTransactionManager")
public PlatformTransactionManager kbTransactionManager(
@Qualifier("kbEntityManagerFactory") EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
@Configuration
@EnableJpaRepositories(
basePackages = {"com.proj.killbill.repository.cm", "com.proj.service.base.repository"},
entityManagerFactoryRef = "cmEntityManagerFactory",
transactionManagerRef = "cmTransactionManager")
@EnableJpaAuditing
@Profile({"dev", "prod"})
public class PersistenceCmConfig {
@Value("${cm.db.user:default}")
private String user;
@Value("${cm.db.password:default}")
private String password;
@Value("${cm.db.url:default}")
private String jdbcUrl;
@Value("${app.cm.ds.jdni.name}")
private String cmJndiName;
private static final Logger LOGGER = LogManager.getLogger(PersistenceCmConfig.class);
@Bean(name = "cmDataSource")
@Profile("dev")
public DataSource devCmDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
dataSource.setUrl(jdbcUrl);
dataSource.setUsername(user);
dataSource.setPassword(password);
LOGGER.debug("CM JDBC URL USER/PASSWORD" + jdbcUrl + " " + user + "/" + password);
return dataSource;
}
@Bean(name = "cmDataSource")
@Profile("prod")
public DataSource prodCmDataSource() {
final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
dsLookup.setResourceRef(true);
return dsLookup.getDataSource(cmJndiName);
}
@SuppressWarnings("Duplicates")
@Bean(name = "cmEntityManagerFactory")
public LocalContainerEntityManagerFactoryBean cmEntityManagerFactory(
@Qualifier("cmDataSource") DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.proj.service.base.domain");
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(additionalProperties());
return em;
}
private Properties additionalProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
return properties;
}
@Bean(name = "cmTransactionManager")
public PlatformTransactionManager cmTransactionManager(
@Qualifier("cmEntityManagerFactory") EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
}
实体管理器使用@Qualifier注释注入适当的数据源。但当我启动应用程序时,我得到了一个例外:
==========
com.proj.killbill.config.PersistenceCmConfig中方法cmEntityManagerFactory的参数0需要一个bean,但找到了2:
-cmDataSource:由类路径资源[com/proj/killbill/config/PersistenceCmConfig.class]中的方法“devCmDataSource”定义
-kbDataSource:由类路径资源[com/proj/killbill/config/PersistenceKbConfig.class]中的方法“devKbDataSource”定义
行动:
考虑将其中一个bean标记为@Primary,更新使用者以接受多个bean,或者使用@Qualifier来标识应该使用的bean
============
@由于未知原因,限定符(“cmDataSource”)被忽略。为什么可能呢?这一点不容忽视。只需将其中一个源声明为
@Primary
@限定符
将插入正确的限定符。查看此项了解更多详细信息@pvpkiran好的,谢谢。我只是被spring的信息误导了,用Primary或Qualifier来解决这个问题。