spring管理配置文件之PropertyPlaceholderConfigurer


先看一个最简单的spring管理一个bean的例子

定义一个Person类

package springresearch;

public class Person {
	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;
	}
	
}

在spring的applicationContext.xml最后进行bean管理和属性注入

<bean id="person" class="springresearch.Person">
		<property name="name" value="liuqi" />
		<property name="age" value="21" />
</bean>


好了,现在我们打算随时修改bean最后的name和age属性,而不修改源文件,或者说我们有大量的bean需要管理,其中一些bean的属性处于动态变化之中,怎么办?

最好的办法就是将这些属性单独拿出来放到配置文件中,方便以后的修改和阅读

我们定义一个config.properties文件

name=liuqi
age=21


按说我们应该写一个读取文件的工具类,然后读取配置文件最后的属性,动态地注入到person中。

然而spring作为一种框架,提供了大量的现成类来集成管理各种技术,只需要我们将这些类配置出来即可。spring中管理配置文件的类就是PropertyPlaceholderConfigurer。

<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:config.properties" />
		<property name="fileEncoding" value="utf-8" />
</bean>

这里的 classpath关键词在下一篇文章做详细解析

然后我们在spring中配置其他bean时就可以使用config中的属性了

<bean id="person" class="springresearch.Person">
		<property name="name" value="${name}" />
		<property name="age" value="${age}" />
</bean>

最终结果

public class Test {
	public static void main(String[] args) {
		ApplicationContext ac = new FileSystemXmlApplicationContext(
				"classpath:applicationContext.xml");
		Person person = (Person) ac.getBean("person");
		System.out.println(person.getName());
		System.out.println(person.getAge());
	}
}

得出的结果一样

一般来说 配置数据库、配置日志文件都需要这样写,本文只是简单地讲解了一下spring中加载配置文件的方式,接下来的文章我会详细研究PropertyPlaceholderConfigurer的源代码以及深入理解,欢迎关注。

  • 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、付费专栏及课程。

余额充值