【问题标题】:Spring @RefreshScope does not work on final class ComboPooledDataSourceSpring @RefreshScope 不适用于最终类 ComboPooledDataSource
【发布时间】:2017-04-24 17:34:50
【问题描述】:

我正在使用 CGLib (AOP) 代理。当 ComboPooledDataSource 是 final 类因为 @RefreshScope 不适用于 final 类时,是否有任何解决方法?

@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>

【问题讨论】:

    标签: spring c3p0


    【解决方案1】:

    第 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-03-06
      • 2020-05-15
      • 2017-12-21
      • 2017-04-22
      • 2015-11-03
      • 2021-12-27
      • 2012-01-02
      • 2013-06-12
      相关资源
      最近更新 更多