Spring动态配置加载

时间:2015-03-09 13:48:08

标签: hibernate spring-mvc

我一直在使用spring / hibernate应用程序,我有@Configuration类从文件读取属性值,然后从db加载客户端名称并返回客户端的数据源。

我要做的是,从网址获取客户端参数,而不是在启动jetty时提供-DclientName。

这样它就可以从url获取参数并从db获取url并创建并将另一个工作数据源设置为当前应用程序。

我现在很困惑,如果你不理解我的问题,请接受我的道歉,你可以请求另一个解释。

任何回复/帮助将不胜感激。 感谢

@Configuration
@EnableTransactionManagement
@ImportResource("classpath:applicationContext.xml")
public class PersistenceConfig {

@Value("${primaryWebDomain}")
private String primaryWebDomain = null;

@Value("${db.driver}")
private String DB_DRIVER = null;

@Value("${db.url}")
private String DB_CONFIG_URL = null;

@Value("${db.user}")
private String DB_USER = null;

@Value("${db.password}")
private String DB_PASSWORD = null;

@Value("${db.client.base.url}")
private String DB_CLIENT_BASE_URL = null;

private String artworkFilePath = null;
private String dataBaseName = null;

@Bean
public ComboPooledDataSource dataSource() throws PropertyVetoException, SQLException {

    initConfigProps();

    ComboPooledDataSource dataSource = new ComboPooledDataSource();

    dataSource.setDriverClass(DB_DRIVER);
    dataSource.setJdbcUrl(DB_CLIENT_BASE_URL + dataBaseName);
    dataSource.setUser(DB_USER);
    dataSource.setPassword(DB_PASSWORD);

    dataSource.setAcquireIncrement(1);
    dataSource.setMinPoolSize(2);
    dataSource.setMaxPoolSize(5);
    dataSource.setMaxIdleTime(300);

    return dataSource;
}

@Bean
public String artworkFilePath() throws SQLException {
    if (artworkFilePath == null) 
        initConfigProps();

    if (System.getProperty("os.name").startsWith("Windows")) 
        return this.artworkFilePath + File.separatorChar + "originals";

    String assetPath = this.artworkFilePath.substring(1).replace("\\".charAt(0), File.separatorChar) + File.separatorChar + "originals";

    return assetPath;
}

@Bean
public String brandworkzApplicationURL() throws SQLException {
    if (primaryWebDomain == null) 
        initConfigProps();

    StringBuilder appUrl = new StringBuilder().append("http://").append(primaryWebDomain).append("/BMS/category/browse.cfm");

    return appUrl.toString();
}

private void initConfigProps() throws SQLException {

    Connection dbConnection = null;
    Statement preparedStatement = null;
    String selectStatement = "SELECT artworkfilepath, databasename FROM bms_applicationVariables WHERE primaryWebDomain ='" + primaryWebDomain + "'";

    try {

        Class.forName(DB_DRIVER);
        dbConnection = DriverManager.getConnection(DB_CONFIG_URL, DB_USER, DB_PASSWORD);
        preparedStatement = dbConnection.createStatement();
        ResultSet rs = preparedStatement.executeQuery(selectStatement);

        while (rs.next()) {
            this.artworkFilePath = rs.getString("artworkfilepath");
            this.dataBaseName = rs.getString("databasename");
        }

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        dbConnection.close();
    }

  }
}

1 个答案:

答案 0 :(得分:0)

因为你想根据一些动态动作使用不同的DataSource;你可能想用AbstractRoutingDataSource

Look at this thread for details