2017-04-24 771 views
0

我正在使用CGLib(AOP)代理。當ComboPooledDataSource是最終類時,是否有任何解決方法,因爲@RefreshScope在最終類上不起作用?Spring @RefreshScope在最終的類上不起作用ComboPooledDataSource

@Bean(name = "portalDataSource", destroyMethod = "close") 
@RefreshScope 
public DataSource dataSource() Integer iMaxConTimeout) throws Exception { 
    ComboPooledDataSource cpds = new ComboPooledDataSource(); 
    cpds.setDriverClass("com.mysql.jdbc.Driver"); //loads the jdbc driver 
    cpds.setJdbcUrl("...."); 
    cpds.setUser("..."); 
    cpds.setPassword("..."); 


    // the settings below are optional -- c3p0 can work with defaults 
    cpds.setMinPoolSize(iMinDBCons); 
    cpds.setMaxPoolSize(iMaxDBCons); 
    cpds.setMaxIdleTime(iMaxConTimeout);  
    return cpds; 
} 

最終的類ComboPooledDataSource是c3p0連接池的一部分。

<!-- Hibernate c3p0 connection pool --> 
<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-c3p0</artifactId> 
    <version>5.0.4.Final</version> 
</dependency> 

回答

0

步驟1:創建名爲SigComboPooledDataSource

public class SigComboPooledDataSource extends TransactionAwareDataSourceProxy { 

    @Autowired 
    // Inject your class by constructor 
    SigComboPooledDataSource(ComboPooledDataSource dataSource) { 
     super.setTargetDataSource(dataSource); 
    } 

    public void close() { 
     ((ComboPooledDataSource) super.getTargetDataSource()).close(); 
    } 

} 

步驟2

@Bean(name = "portalDataSource", destroyMethod = "close") 
@RefreshScope 
public DataSource dataSource() Integer iMaxConTimeout) throws Exception { 

    ComboPooledDataSource cpds = new ComboPooledDataSource();  
    cpds.setDriverClass("com.mysql.jdbc.Driver"); //loads the jdbc driver  
    cpds.setJdbcUrl("....");  
    cpds.setUser("..."); 
    cpds.setPassword("..."); 
    cpds.setPassword("..."); 

    // the settings below are optional -- c3p0 can work with defaults 
    cpds.setMinPoolSize(iMinDBCons); 
    cpds.setMaxPoolSize(iMaxDBCons); 
    cpds.setMaxIdleTime(iMaxConTimeout);  
    return new SigComboPooledDataSource(cpds); 
}