Java @PropertySource是否可以用于具有@Value的属性?

Java @PropertySource是否可以用于具有@Value的属性?,java,spring,Java,Spring,我正在创建一个新的项目,并尝试在以前使用XML配置的地方使用Java配置。我的应用程序需要类似于的配置。我有如下配置: @Configuration @PropertySources({ @PropertySource(ignoreResourceNotFound = true, value = "classpath:app.config.properties"), @PropertySource(ignoreResourceNotFoun

我正在创建一个新的项目,并尝试在以前使用XML配置的地方使用Java配置。我的应用程序需要类似于
的配置。我有如下配置:

@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
        };
    }
公共类AppInitializer
扩展AbstractAnnotationConfigDispatcherServletInitializer{
@凌驾
受保护类[]getRootConfigClasses(){
返回新类[]{
RootConfig.class,
TestConfig.class,
SecurityConfig.class
};
}
在此之前,我将使用以下XML配置实现这一点:

@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");
    }

关于
@PropertySource
的文档说明,属性源已添加到
环境中

* 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.
设法解决了这个问题。我将
PropertySoure
s
)移动到
RootConfig
,它公开了一个
propertysourcesplaceconfigurer
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();
    }

}

很抱歉没有看到这个!我想我错过了它,因为我在同一时间添加了我自己的答案。