我有一个spring boot bean,它是动态数据源对象的代理对象。
@Bean("schemaSwappableDataSource")
public ProxyFactoryBean schemaSwappableDataSource(@Autowired @Qualifier("schemaSwappableDataSourceProxy") SchemaSwappableDataSourceProxy schemaSwappableDataSourceProxy){
try {
ProxyFactoryBean bean = new ProxyFactoryBean();
Class<?>[] classList = {javax.sql.DataSource.class};
bean.setProxyInterfaces(classList);
bean.setTargetSource(schemaSwappableDataSourceProxy);
//return (DataSource)bean.getObject();
return bean;
}catch(Exception e){
e.printStackTrace();
return null;
}
}
当我尝试使用@Autowired注入此bean时,我在运行时得到以下错误。
@Autowired
@Qualifier("schemaSwappableDataSource")
DataSource schemaSwappableDataSource;
Eroor消息
11:39:24.238 - WARN - [SpringApplicationRunListeners.java:91] - Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor' defined in class path resource [org/springframework/transaction/annotation/ProxyTransactionManagementConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor]: Factory method 'transactionAdvisor' threw exception; nested exception is java.lang.NullPointerException)
11:39:24.410 - ERROR - [LoggingFailureAnalysisReporter.java:42] -
***************************
APPLICATION FAILED TO START
***************************
Description:
Field schemaSwappableDataSource in com.siemens.rcs.dc.comment.dao.impl.CommentDAOImpl required a bean of type 'javax.sql.DataSource' that could not be found.
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.type) did not find property 'spring.datasource.type'
- Bean method 'dataSource' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
- Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'
Action:
Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
但如果我使用
@Resource(name="schemaSwappableDataSource")
DataSource schemaSwappableDataSource;
工作正常...... 任何人都可以告诉我我做错了什么以及在这种情况下使用ProxyFactory bean的更好方法是什么?我需要在运行时创建数据源对象,因为数据源动态地需要根据传递的参数连接到不同类型的数据库。它不需要每次都创建新的数据源,因为一旦创建它就存储在静态映射中并基于线程局部变量获取它们。 任何帮助,最佳实践请咨询。
答案 0 :(得分:0)
全部谢谢,它使用简单的cust。
@Bean("schemaSwappableDataSource")
public DataSource schemaSwappableDataSource(@Autowired @Qualifier("schemaSwappableDataSourceProxy") SchemaSwappableDataSourceProxy schemaSwappableDataSourceProxy) throws ClassNotFoundException{
ProxyFactoryBean bean = new ProxyFactoryBean();
Class<?>[] classList = {javax.sql.DataSource.class};
bean.setProxyInterfaces(classList);
bean.setTargetSource(schemaSwappableDataSourceProxy);
return javax.sql.DataSource.class.cast(bean.getObject());
}
@Bean("schemaSwappableJdbcTemplate")
public JdbcTemplate schemaSwappableJdbcTemplate(@Qualifier("schemaSwappableDataSource") DataSource schemaSwappableDataSource) {
return new JdbcTemplate(schemaSwappableDataSource);
}
@Bean("schemaSwappableTxManager")
public DataSourceTransactionManager schemaSwappableTxManager(@Qualifier("schemaSwappableDataSource") DataSource schemaSwappableDataSource){
DataSourceTransactionManager txMgr = new DataSourceTransactionManager();
txMgr.setDataSource(schemaSwappableDataSource);
return txMgr;
}
将两个注释组合成自定义的@SchemaSelector&lt; - custom&amp; @Transactional&lt; - 由春天提供。但是能够通过使用org.springframework.core.Ordered接口来解决这个问题。