0

我有一个 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;

错误消息

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 的更好方法是什么?我需要在运行时创建数据源对象,因为数据源需要根据传递的参数动态连接到不同类型的数据库。它不需要每次都创建新的数据源,因为一旦创建它就存储在静态映射中并根据线程局部变量获取它们。任何帮助,最佳实践请指教。

4

1 回答 1

0

谢谢大家,它与一个简单的客户合作。

@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 <- custom & @Transactional <--由 spring 提供。但是能够通过使用 org.springframework.core.Ordered 接口来解决这个问题。

于 2017-04-24T22:39:13.193 回答