0

Spring 2.5を使用していたので、プロパティが環境から提供されるか、config_path=C:/application.propertiesクラスパスであるデフォルトの場所からオーバーライドされるようにする必要があります。

したがって、以下のように適用しましたapplicationcontext.xml

<bean class="com.test.utils.ExtendedPropertySourcesPlaceholderConfigurer">
    <property name="overridingSource" value="file:${config_path}/application.properties"/>
    <property name="locations" value="classpath*:META-INF/*-config.properties" />
</bean>

ExtendedPropertySourcesPlaceholderConfigurerコード

public class ExtendedPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

    private Resource overridingSource;

    public void setOverridingSource(Resource overridingSource) {
        this.overridingSource = overridingSource;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        MutablePropertySources sources = ((ConfigurableApplicationContext) applicationContext).getEnvironment().getPropertySources();
        if (overridingSource == null) {
            return;
        }
        sources.addFirst(new ResourcePropertySource(overridingSource));
    }
}

今、これを春の3.1.2に移行しています。天気の春は、より効率的な方法でそれを行うための新しいAPIを提供していますか?

4

1 に答える 1

1

Spring 3.1では、新しい環境抽象化であるPropertySource Abstractionが導入されました (どちらのリンクもSpringSourceブログの記事を示しています)

しかし、Spring3.0または3.1ではオーバーライドする必要はないと思います

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:application-config.properties</value>
            <value>file:${user_config_path}/application-config.properties</value>
        </list>
    </property>
    <property name="localOverride" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
</bean>

ところで。Spring 3.0では、Beanクラスは次のとおりです。PropertyPlaceholderConfigurer(このブログhttp://www.baeldung.com/2012/02/06/properties-with-spring/を参照)

于 2013-01-18T07:07:44.910 に答える