繁体   English   中英

在Spring中在运行时更改数据源URL

[英]change datasource url at runtime in spring

我通过注释配置了spring应用程序。 这是我配置的一部分

@Configuration
@EnableTransactionManagement
public class JpaSpringConfiguration {

    @Bean(destroyMethod = "close")
    @Lazy
    @Primary
    public BasicDataSource dataSource(@Value("${statistics.hostname}") String statisticsHostname) { 
        final BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("org.postgresql.Driver");
        String url = String.format("jdbc:postgresql://%s:5432/statistics-db", statisticsHostname);
        dataSource.setUrl(url);
        ....
        return dataSource;
    }

    @Bean
    public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
        final PropertyPlaceholderConfigurer placeholderConfigurer = new PropertyPlaceholderConfigurer();
        placeholderConfigurer.setSystemPropertiesMode(SYSTEM_PROPERTIES_MODE_OVERRIDE);
        Properties properties = new Properties();
        properties.setProperty("statistics.hostname", "localhost");

        placeholderConfigurer.setProperties(properties);
        return placeholderConfigurer;
    }

直到最近我们才有了xml配置

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="properties">
        <props>
            <prop key="statistics.hostname">localhost</prop>
        </props>
    </property>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" lazy-init="true" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver" />
    <property name="url" value="jdbc:postgresql://${statistics.hostname}:5432/statistics-db" />
    <property name="username" value="user" />
    <property name="password" value="password" />
</bean>

当用户选择不同的服务器连接时,我们设置系统属性和关闭的应用程序上下文并刷新

System.setProperty("statistics.hostname", hostname)
applicationContext.close()
applicationContext.refresh()

当我使用注释配置时,这不起作用。 我的问题是:

  1. 为什么它现在不起作用?
  2. 如何通过系统属性完全摆脱设置主机名?

编辑:我刚刚发现我在方法dataSource()中的参数名称周围忘了$ {}。 所以它现在有效,但问题2仍然存在。

不知道为什么它不起作用,但你可能会尝试做更多的事情:

  1. 是否需要在刷新之前关闭上下文? 尽量只刷新它。
  2. 您可以将bean标记为@RefreshScope(但它需要spring cloud)并使用/refresh endpoint刷新它。 这需要另一个端点在调用刷新之前实际更新bean上的主机。

“如何完全摆脱通过系统属性设置主机名?”

将其传递给属性文件,这是通常配置的方式。 如果您使用的是spring boot,那么您只需要配置:

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
...

属性。 将使用这些值为您创建数据源bean。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM