2017-04-24 137 views
0

我有一个Spring Boot Bean,它是动态数据源对象的代理对象。spring-boot 1.4.1 @Resource和@Autowired之间的依赖注入区别

@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

使用组件或服务,而不是@Bean注释....我也希望这个类是在春季组件基础包... – VelNaga

+0

谢谢,'@Bean'出现在'@Configuration'类中spring'@ComponentScan(basePackages = {“”}“path。 –

+0

不要捕捉异常并返回'null',因为这会导致难以调试的问题(正如您注意到的),您可能想要禁用默认的自动配置一个'DataSource'。 –

回答

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

相关问题