Spring Property 02
2017年9月1日大约 1 分钟
Spring Property
这个版本太老,但是思想是正确的。
PropertyPlaceholderConfigurer
对于 PropertySourcesPlaceholderConfigurer
的继承在 3.1 之后已经被废弃了。所以采用 PropertyPlaceholderConfigurer
的方式。
- spring-property-config.xml
- MyPropertyPlaceholderConfigurer.class
这里为了测试方便,直接使用构造器初始化的形式。
package com.ryo.spring.learn.old.property.extern;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import java.util.Properties;
public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
public MyPropertyPlaceholderConfigurer() {
Properties properties = System.getProperties();
properties.setProperty("name", "ryo");
this.setProperties(properties);
}
}
- MyPropertyPlaceholderConfigurerTest.class
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:spring-property-config.xml",
})
public class MyPropertyPlaceholderConfigurerTest {
@Value("${name}")
private String name;
@Test
public void nameTest() {
System.out.println(name);
}
}
测试输出结果:
ryo
可见,可以直接将设置的属性在 spring 中直接使用。原来此处就不赘述了。
讲下简单的用途:
1、对敏感信息进行加密
2、配置可以统一获取。根据不同的情况获取不同的配置属性。直接设置到 Properties 中。
Default Value
More
贡献者
binbin.hou