小编Mar*_*nas的帖子

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

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

java spring spring-annotations

5
推荐指数
1
解决办法
3180
查看次数

标签 统计

java ×1

spring ×1

spring-annotations ×1