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

Mar*_*nas 5 java spring spring-annotations

我通过注释配置了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;
    }
Run Code Online (Sandbox Code Playgroud)

直到最近我们才有了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>
Run Code Online (Sandbox Code Playgroud)

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

System.setProperty("statistics.hostname", hostname)
applicationContext.close()
applicationContext.refresh()
Run Code Online (Sandbox Code Playgroud)

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

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

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

fre*_*man 0

不确定为什么它不起作用,但您可以尝试做更多事情:

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

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

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

spring.datasource.url=
spring.datasource.username=
spring.datasource.password=
...
Run Code Online (Sandbox Code Playgroud)

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