0

開発と生産の 2 つのスプリング プロファイルがあります。プロファイル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>

現在、どこにも共通のプロパティはありません。jdbc.propertiesとの両方について、共通のプロパティ ファイルを各プロファイルのファイルとマージする方法は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 に答える