2017-04-24 136 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接口來解決這個問題。

相關問題