簡體   English   中英

@PropertySource是否可以用於具有@Value的屬性?

[英]Is it possible for @PropertySource to be used for properties with @Value?

我正在創建一個新項目,並嘗試使用Java config,而以前使用XML config。 我的應用程序需要類似<property-placeholder>配置。 我有如下配置:

@Configuration
@PropertySources({
    @PropertySource(ignoreResourceNotFound = true,
                    value = "classpath:app.config.properties"),
    @PropertySource(ignoreResourceNotFound = true,
                    value = "classpath:app.user.config.properties"),
    @PropertySource(ignoreResourceNotFound = true,
                    value = "file:///${app.config}")
})
public class TestConfig {

    @Autowired
    Environment env;

    @Value("${prop1}")
    String prop1;

    @Bean
    public BasicData bd1() {
        System.out.println(env.getProperty("prop1"));
        System.out.println(prop1);
        return new BasicData("Test");
    }

env.getProperty(String)可以正常工作,盡管我雖然@Value注釋也可能起作用。

該配置通過MVC初始化程序實例化,如下所示:

public class AppInitializer
        extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[]{
            RootConfig.class,
            TestConfig.class,
            SecurityConfig.class
        };
    }

以前,我將使用以下內容通過XML配置實現此目的:

<context:property-placeholder
    location="classpath:app.config.properties,classpath:app.user.config.properties,file:///${app.config}"
    ignore-resource-not-found="true"/>

@PropertySource的文檔說,屬性源已添加到Environment

* Annotation providing a convenient and declarative mechanism for adding a
 * {@link org.springframework.core.env.PropertySource PropertySource} to Spring's
 * {@link org.springframework.core.env.Environment Environment}. To be used in
 * conjunction with @{@link Configuration} classes.

占位符的替換由BeanFactoryPostProcessor完成。 這曾經是PropertyPlaceholderConfigurer ,但是從Spring 3.1開始就有PropertySourcesPlaceholderConfigurer 我認為這是向上下文添加這種類型的bean的問題。 它的文檔表明它針對Environment解析了占位符:

* Specialization of {@link org.springframework.beans.factory.config.PlaceholderConfigurerSupport
 * PlaceholderConfigurerSupport} that resolves ${...} placeholders within bean definition
 * property values and {@code @Value} annotations against the current Spring {@link
 * Environment} and its set of {@link PropertySources}.

設法解決解決方案。 我將PropertySoures )移到了RootConfig ,該RootConfig公開了PropertySourcesPlaceholderConfigurer bean。

@PropertySources({
    @PropertySource(ignoreResourceNotFound = true,
                    value = "classpath:app.config.properties"),
    @PropertySource(ignoreResourceNotFound = true,
                    value = "classpath:app.user.config.properties"),
    @PropertySource(ignoreResourceNotFound = true,
                    value = "file:///${app.config}")
})
public class RootConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM