PropertySourcesPlaceholderConfigurer
主要步骤
切⼊位置
注解为向Spring的 Environment中添加PropertySource提供了一种方便的声明性机制。与@Configuration类一起使用。
给定一个包含键/值对testbean.name=myTestBean的文件app.properties,下面的@Configuration类使用@PropertySource将app.properties添加到Environment的propertysources集合。
@Configuration
@PropertySource("classpath:/com/myco/app.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
注意,Environment对象@Autowired到配置类中,然后在填充TestBean对象时使用。根据上面的配置,对testBean.getName()的调用将返回“myTestBean”。
为了解析出和@Value注解中的${…}占位符,以使用PropertySource中的配置,必须确保在ApplicationContext使用的BeanFactory中注册了适当的嵌入式值解析器(an appropriate embedded value resolver)。当在XML中使用context:property-placeholder时,会自动进行注册。
在使用@Configuration类时,可以通过静态@Bean方法显式注册PropertySourcesPlaceholderConfigurer来实现这一点。但是请注意,通过静态@Bean方法显式注册PropertySourcesPlaceholderConfigurer通常只有在需要定制配置(如占位符语法等)时才需要。可参见@Configuration的javadocs的“使用外部化值”部分和@Bean的javadocs的“关于beanfactorypostprocessor -返回@Bean方法的说明”了解详细信息和示例。
任何${…}占位符出现在@PropertySource资源位置中,将根据已经在环境中注册的property sources进行解析。例如:
@Configuration
@PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
public class AppConfig {
@Autowired
Environment env;
@Bean
public TestBean testBean() {
TestBean testBean = new TestBean();
testBean.setName(env.getProperty("testbean.name"));
return testBean;
}
}
假设“我的。”my.placeholder”出现在已经注册的property sources中——例如,系统属性或环境变量——占位符将被解析为相应的值。如果不是,那么"default/path"将被用作默认值。默认值(由冒号“:”分隔)是可选的。如果没有指定默认值且属性无法解析,则会抛出IllegalArgumentException异常。
在给定的属性键存在于多个.properties文件中的情况下,处理的最后一个@PropertySource注解将“胜出”并覆盖之前的同名键。
例如,给定两个属性文件a.properties和b.properties,考虑以下两个使用@PropertySource注解引用它们的配置类:
@Configuration
@PropertySource("classpath:/com/myco/a.properties")
public class ConfigA { }
@Configuration
@PropertySource("classpath:/com/myco/b.properties")
public class ConfigB { }
覆盖顺序取决于这些类在应用程序上下文中注册的顺序。
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(ConfigA.class);
ctx.register(ConfigB.class);
ctx.refresh();
在上面的场景中,b.properties中的属性将覆盖a.properties中存在的任何副本,因为ConfigB是最后注册的。
在某些情况下,在使用@PropertySource注解时,严格控制属性源顺序可能是不可能或不实际的。例如,如果上面的@Configuration类是通过组件扫描注册的,那么排序就很难预测。在这种情况下——如果重写很重要——建议用户回到使用编程的PropertySource API。详细信息可参见ConfigurableEnvironment和MutablePropertySources 的javadocs。
注意:根据Java 8约定,该注解是可重复的。但是,所有这样的@PropertySource注解都需要在同一层声明:要么直接在配置类上声明,要么作为元注解在相同的自定义注解上声明。不建议混合使用直接注解和元注解,因为直接注解将有效地覆盖元注解。