Spring项目中方便灵活读取properties文件

在Spring项目中,一般会有多个properties文件,例如数据库连接信息等,那么在Spring的配置文件和Java代码中怎么灵活的使用这些key-value形式的配置呢?

1. 新建类PropertyConfigurer继承PropertyPlaceholderConfigurer,并复写mergeProperties()方法,在该方法中调用super.mergeProperties()方法,把返回值保存至成员变量

public class PropertyConfigurer extends PropertyPlaceholderConfigurer
{
	private Properties properties;

	@Override
	protected Properties mergeProperties() throws IOException
	{
		properties = super.mergeProperties();

		return properties;
	}

	public Properties getProperties()
	{
		return properties;
	}

}

2. 新建Config类,并使用单例模式,有一个静态成员变量propertyConfigurer

public class Config
{
	private static Config config = new Config();

	private static PropertyConfigurer propertyConfigurer;

	private Config()
	{
	}

	public static Config getInstance(PropertyConfigurer propertyConfigurer)
	{
		Config.propertyConfigurer = propertyConfigurer;

		return config;
	}

	public static String getString(String key)
	{
		return propertyConfigurer.getProperties().getProperty(key);
	}

	public static String getString(String key, String defaultValue)
	{
		return propertyConfigurer.getProperties().getProperty(key, defaultValue);
	}

	public static int getInt(String key)
	{
		return Integer.valueOf(propertyConfigurer.getProperties().getProperty(key));
	}

	public static int getInt(String key, int defaultValue)
	{
		return Integer.valueOf(propertyConfigurer.getProperties().getProperty(key, String.valueOf(defaultValue)));
	}

}

3. config.spring.xml配置,其中locations属性为properties文件所在路径

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<bean id="propertyConfigurer" class="com.realjt.summer.config.PropertyConfigurer"
		p:fileEncoding="utf-8" p:localOverride="true">
		<property name="locations">
			<list>
				<value>/WEB-INF/config/*.properties</value>
				<value>classpath:*.properties</value>
			</list>
		</property>
		<property name="ignoreResourceNotFound" value="true"></property>
		<property name="ignoreUnresolvablePlaceholders" value="true"></property>
	</bean>

	<bean id="config" class="com.realjt.summer.config.Config"
		factory-method="getInstance">
		<constructor-arg ref="propertyConfigurer"></constructor-arg>
	</bean>

</beans>

4. 配置使用

4.1 在spring.xml中通过EL表达式引用properties文件中变量,例如

<property name="url" value="${url}" />

4.2 在Java代码中通过Spring注解形式引用properties文件中变量,例如

@Value("${url}")
private String url;

4.3 直接使用Config类静态方法,例如

Config.getString("url");


  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值