0

我有两个弹簧配置文件:开发和生产。应该有一个通用属性文件(classpath:properties/common/*.properties放在classpath:properties/development/*.properties.

这是我的上下文配置片段,以阐明我的意图:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.example.entities.*" />
    <property name="hibernateProperties" ref="hibernateProperties" />
</bean>

<beans profile="development">

    <context:property-placeholder location="classpath:properties/development/jdbc.properties" />

    <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="ignoreResourceNotFound" value="true" />
        <property name="location" value="classpath:properties/development/hibernate.properties" />
    </bean>

</beans>

<beans profile="production">

    <context:property-placeholder location="classpath:properties/production/jdbc.properties" />

    <bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="ignoreResourceNotFound" value="true" />
        <property name="location" value="classpath:properties/production/hibernate.properties" />
    </bean>

</beans>

Currently there's no common properties somewhere. How to merge a common properties file with the one in each profile for both jdbc.properties and hibernate.properties?

4

2 回答 2

1

我为此使用了 JavaConfig:

@Configuration
@Profile("development")
public class DevelopmentConfig {
    public @Bean String profile() {
        return "development";
    }
}

@Configuration
@Profile("production")
public class ProductionConfig {
    public @Bean String profile() {
        return "production";
    }
}

public class PropertyUtils {
    public static Properties getProperties(String profile, String filename) throws IOException {
        Properties ret = new Properties();
        ClassPathResource resource;

        resource = new ClassPathResource("properties/common/" + filename + ".properties");
        ret.putAll(PropertiesLoaderUtils.loadProperties(resource));

        resource = new ClassPathResource("properties/" + profile + "/" + filename + ".properties");
        if (resource.exists()) {
            ret.putAll(PropertiesLoaderUtils.loadProperties(resource));
        }

        return ret;
    }
}

@Configuration
public class MainConfig {
    private @Autowired String profile;
    // Here you can use: PropertyUtils.getProperties(profile, "jdbc"))
}
于 2013-08-11T12:59:02.610 回答
0

这应该让你开始

<bean id="allProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <property name="singleton" value="true"/>
  <property name="ignoreResourceNotFound" value="true"/>
  <property name="locations">
    <list>
      <value>classpath*:default.properties</value>
      <value>classpath*:overrides.properties</value>
      <value>file:${APP_HOME}/**/*.properties</value>
    </list>
  </property>
</bean>
于 2013-07-23T21:29:00.807 回答