使用 PropertySourcesPlaceholderConfigurer @Bean 时找不到 属性
Can't find property when using PropertySourcesPlaceholderConfigurer @Bean
我们正在配置一个新的 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
看来我做错了。我开始走上了正确的道路,但后来出于某种原因出轨了。正确的方法是为每个环境设置 @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"
我们正在配置一个新的 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
看来我做错了。我开始走上了正确的道路,但后来出于某种原因出轨了。正确的方法是为每个环境设置 @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"