spring PropertyPlaceholderConfigurer分析,带实例

1:写在前面

该类的作用是替换配置文件中配置的占位符信息为properties文件中实际的值,原理是PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,利用其在创建bean前能够修改BeanDefinition的特点,来替换修改占位符为properties文件中的实际值。另外,关于该类的源码分析可以参考这里。在spring中还一个类PropertyOverrideConfiguer,功能类似,感兴趣的朋友也可以看下。

2:例子

2.1:定义bean

public class MyPropertyPlaceholderConfigurerBean {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}

2.2:xml和配置文件

  • xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="propertyPlaceholderConfigurer"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location"
                  value="classpath:studyhelp/testpropertyplaceholderconfigurer.properties"/>
        <property name="fileEncoding" value="UTF-8"/>
    </bean>

    <bean class="yudaosourcecode.studyhelp.MyPropertyPlaceholderConfigurerBean"
          id="myPropertyPlaceholderConfigurerBean">
        <property name="name" value="${my.name}"/>
        <property name="age" value="${my.age}"/>
    </bean>
</beans>
  • 配置文件
my.name=张三的歌
my.age=900
  • 测试
@Test
public void testpropertyplaceholderconfigurer() {
    ClassPathXmlApplicationContext ac
            = new ClassPathXmlApplicationContext("classpath*:studyhelp/testpropertyplaceholderconfigurer.xml");
    System.out.println(ac.getBean("myPropertyPlaceholderConfigurerBean"));
}
  • 运行
{"age":900,"name":"张三的歌"}

Process finished with exit code 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,可以通过继承PropertyPlaceholderConfigurer类来获取配置文件中的配置项。 首先,创建一个类,继承自PropertyPlaceholderConfigurer,重写processProperties方法,该方法在Spring应用程序上下文启动时会被调用,可以在该方法中获取配置文件中的属性值,例如: ```java public class MyPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private Properties properties; public MyPropertyPlaceholderConfigurer() { this.properties = new Properties(); } @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); this.properties.putAll(props); } public String getProperty(String key) { return this.properties.getProperty(key); } } ``` 在该类中,我们重写了processProperties方法,并在该方法中将配置文件中的属性值保存到一个Properties对象中。然后,提供了一个getProperty方法,可以根据属性名获取属性值。 接下来,在Spring Boot启动类中,注入该类的实例,并通过该实例获取配置文件中的属性值,例如: ```java @SpringBootApplication public class MyApp { @Autowired private MyPropertyPlaceholderConfigurer propertyConfigurer; public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } @PostConstruct public void init() { String propertyValue = propertyConfigurer.getProperty("my.property"); System.out.println("my.property value is: " + propertyValue); } } ``` 在上述代码中,我们通过@Autowired注入了MyPropertyPlaceholderConfigurer实例,并在init方法中,获取了配置文件中的属性值,并打印出来。 在application.yml或application.properties中,可以定义my.property属性值,例如: ```yaml my.property: my custom property value ``` 这样,当应用启动时,就会打印出"my.property value is: my custom property value"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值