使用PropertySourcesPlaceholderConfigurer @Bean时无法找到属性

时间:2015-07-24 19:20:18

标签: java spring properties

我们正在配置新的Spring应用程序,我们正试图确定如何使每个环境的应用程序属性保持不同。我们使用PropertySourcesPlaceholderConfigurer @Beans和@Profile来规定要使用的属性文件:

public abstract class BaseConfig implements InitializingBean, DisposableBean {

    private static final String LOCAL_CONFIG = "local/application.properties";
    private static final String DEV_CONFIG = "development/application.properties";
    private static final String STAGING_CONFIG = "staging/application.properties";
    private static final String PRODUCTION_CONFIG = "production/application.properties";

    @Bean
    @Profile("development")
    public static PropertySourcesPlaceholderConfigurer developmentPropertyPlaceholderConfigurer() {
        System.out.println("config'ing dev");
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setLocation(new ClassPathResource(DEV_CONFIG));
        return configurer;
    }

    @Bean
    @Profile("staging")
    public static PropertySourcesPlaceholderConfigurer stagingPropertyPlaceholderConfigurer() {
        System.out.println("config'ing staging");
        PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
        configurer.setLocation(new ClassPathResource(STAGING_CONFIG));
        return configurer;
    }

    // and so on...

}

我们在命令行中添加了一个环境属性,它似乎正确地选择了这些配置文件:

-Dspring.profiles.active="development"

我们会在扫描BaseConfig时在日志中看到这一点:

config'ing dev
2015-07-24T15:19:09,529 {} [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  o.s.c.s.PropertySourcesPlaceholderConfigurer - Loading properties file from class path resource [development/application.properties]

当我们尝试从其中一个文件中获取属性时,它不在@Autowire环境中或ApplicationContext环境中:

@Configuration
@EnableJpaRepositories(basePackages = { 
        "com.myapp.jpa.domain", "com.myapp.jpa.repositories" })
@EnableTransactionManagement
@PropertySource(value = "file:/my/home/dir/db.properties", ignoreResourceNotFound = true)
public class JpaConfig extends BaseConfig {

    @Autowired
    Environment environment;

    @Bean
    public DataSource dataSource() throws SQLException {
        String testProperty = environment.getProperty("test.property");
        logger.info("test.property from autowired environment: " + testProperty);

        ApplicationContext ctx = new GenericApplicationContext();
        Environment env = ctx.getEnvironment();
        boolean contains = env.containsProperty("test.property");
        logger.info("Does my environment contain the 'test.property' property? " + contains);
        logger.info("test.property from context: " + env.getProperty("test.property"));

        // ...
    }
}

这两个语句都打印一个null属性。总之,看起来我想要阅读的application.properties文件实际上正在被阅读,但这些属性在Environment中不可用。我究竟做错了什么?我怀疑在创建PropertySourcesPlaceholderConfigurer Bean之后我还缺少一些东西。

2015-07-24T15:19:09,730 {} [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  x.y.z.JpaConfig - test.property from autowired environment: null
2015-07-24T15:19:09,731 {} [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  x.y.z.JpaConfig - Does my environment contain the 'test.property' property? false
2015-07-24T15:19:09,731 {} [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  x.y.z.JpaConfig - test.property from context: null
编辑 - 哎呀。我忘记在这里粘贴development/application.properties的样子:

test.property=from_development

编辑 - 有人询问有效的个人资料。我使用Java命令的-D属性设置它们。如果没有设置,BaseConfig中的正确方法将无法运行但是确实如此。我添加了下面的代码,以证明活动配置文件实际上已设置:

for ( String activeProfile : environment.getActiveProfiles() ) {
    logger.info("active profile: " + activeProfile);
}

产生:

2015-07-24T16:02:48,179 {} [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO  c.d.c.c.j.JpaConfig - active profile: development

1 个答案:

答案 0 :(得分:0)

所以看起来我做错了。我从正确的道路开始,但出于某种原因出轨了。正确的方法是为每个环境设置@Configuration个类,并使每个环境指向不同的application.properties个文件:

@Configuration
@Profile("development")
@PropertySource("classpath:development/application.properties")
public class DevelopmentConfig extends BaseConfig {
}

@Configuration
@Profile("production")
@PropertySource("classpath:production/application.properties")
public class ProductionConfig extends BaseConfig {
}

然后,在启动服务器时指定应在命令行上使用的配置文件:

-Dspring.profiles.active="development"