spring用PropertyPlaceholderConfigurer读取配置

我们在BeanFactoryPostProcessor的doc中有这么一句:

/*
*
 * <p>See PropertyResourceConfigurer and 
 * its concrete implementations
 * for out-of-the-box solutions 
 * that address such configuration needs.
 */

我们点进PropertyResourceConfigurer

查看doc:

/**
 * Allows for configuration of individual 
 * bean property values from a property resource,
 * i.e. a properties file. Useful for 
 * custom config files targeted at system
 * administrators that override bean properties 
 * configured in the application context.
 */

它是用来解析配置文件的信息的。

 * <p>Two concrete implementations are provided in the distribution:
 * <ul>
 * <li>{@link PropertyOverrideConfigurer} for "beanName.property=value" style overriding
 * (<i>pushing</i> values from a properties file into bean definitions)
 * <li>{@link PropertyPlaceholderConfigurer} for replacing "${...}" placeholders
 * (<i>pulling</i> values from a properties file into bean definitions)
 * </ul>
 * /

它有两个实现类,我们将用后者PropertyPlaceholderConfigurer来演示如何读取配置文件的信息。这里取值的方式是常用SpEl表达式${...}


xml配置:

我们沿用Restaurant类。

<!--this configuration is for the TestPropertyPlaceholderConfigurerXml class-->
<bean id="restaurantBean" class="com.ocean.testBeanFactoryPostProcessor.Restaurant">
		<property name="welcome" value="${customWelcome}"></property>
	</bean>

	<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="classpath:restaurant.properties"></property>
	</bean>

我们在resources目录下建restaurant.properties文件,内容是:

customWelcome='this welcome is from restaurant.properties'

测试:

public class TestPropertyPlaceholderConfigurerXml {
	public static void main(String[] args) {
		ClassPathXmlApplicationContext applicationContext
				 = new ClassPathXmlApplicationContext("factory-example.xml");

		Restaurant restaurantBean = (Restaurant) applicationContext.getBean("restaurantBean");
		restaurantBean.printWelcome();
	}
}

打印出的信息是:

'this welcome is from restaurant.properties'

说明PropertyPlaceholderConfigurer取值成功了。


Annotation配置:

把xml配置的内容注释掉。

@Configuration
@PropertySource(value = "classpath:restaurant.properties")
public class ConfigurationClass {

	@Value("${customWelcome}")
	private String welcome;

	@Bean(value = "jack-restaurant")
	public Restaurant restaurant(){
		Restaurant restaurant = new Restaurant();
		restaurant.setWelcome(welcome);
		return restaurant;
	}

	@Bean
	public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer(){
		return new PropertyPlaceholderConfigurer();
	}
}

注解配置的PropertyPlaceholderConfigurer 需要配合着@PropertySource注解来使用。

注入PropertyPlaceholderConfigurer 的时候,我用了static关键字,是希望它最早被注入。

测试:

public class TestPropertyPlaceholderConfigurerAnnotation {
	public static void main(String[] args) {

		AnnotationConfigApplicationContext applicationContext
				 = new AnnotationConfigApplicationContext(ConfigurationClass.class);

		Restaurant restaurant = applicationContext.getBean("jack-restaurant", Restaurant.class);

		restaurant.printWelcome();
	}
}

此时报错了:

 Could not resolve placeholder 'customWelcome' in value "${customWelcome}"

代码肯定没问题。

我们猜想,是不是PropertyPlaceholderConfigurer 在读取到配置文件的信息之前,@Value("${customWelcome}")已经去取值了。

查看PropertyPlaceholderConfigurer的文档:

<p>As of Spring 3.1, 
{@link org.springframework.context.support.
PropertySourcesPlaceholderConfigurer
 * PropertySourcesPlaceholderConfigurer} 
 * should be used preferentially over 
 * this implementation; it is
 * more flexible through taking advantage of 
 * the {@link org.springframework.core.env.Environment} 
 * and
 * {@link org.springframework.core.env.PropertySource} 
 * mechanisms also made available in Spring 3.1.

spring3.1之后,PropertySourcesPlaceholderConfigurer优先于PropertySourcesPlaceholderConfigurer加载。

于是我们注入PropertySourcesPlaceholderConfigurer

@Bean
	public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(){
		return new PropertySourcesPlaceholderConfigurer();
	}

再次测试,通过。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值