PropertiesFactoryBean和PropertyPlaceholderConfigurer管理属性文件

9 篇文章 0 订阅

org.springframework.beans.factory.config.PropertiesFactoryBean管理属性文件,通过@Value("#{settingBean[‘properties_key’]}")注解来获取相应的属性。

org.springframework.beans.factory.config.PropertyPlaceholderConfigurer也可以管理配置文件,通过@Value("${property_key}")注解来获取相应的属性。通过自己实现一个PropertyPlaceholderConfigure类来加载,当然也可以不用自己来实现,Spring会自己为我们加载property文件。

//JDBC.java
package sping.analysis.properties.file;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 通过PropertyPlaceholderConfigurer来加载
 * 
 * @author slHuang
 * @since 2019-02-10
 */
@Component("jdbcBean")
public class JDBC {

	@Value("${jdbc.driverClassName}")
	private String driver;
	
	@Value("${jdbc.url}")
	private String url;
	
	@Value("${jdbc.username}")
	private String username;
	
	@Value("${jdbc.password}")
	private String password;

	@Override
	public String toString() {
		return "JDBC [driver=" + driver + ", url=" + url + ", username=" + username + ", password=" + password + "]";
	}
	
}
//JDBC2.java
package sping.analysis.properties.file;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 通过PropertiesFactoryBean来加载
 * 
 * @author slHuang
 * @since 2019-02-10
 */
@Component("jdbc2Bean")
public class JDBC2 {

	@Value("#{settingBean['jdbc.driverClassName']}")
	private String driver;
	
	@Value("#{settingBean['jdbc.url']}")
	private String url;
	
	@Value("#{settingBean['jdbc.username']}")
	private String username;
	
	@Value("#{settingBean['jdbc.password']}")
	private String password;

	@Override
	public String toString() {
		return "JDBC [driver=" + driver + ", url=" + url + ", username=" + username + ", password=" + password + "]";
	}
	
}
//MyPropertiesFile.java
package sping.analysis.properties.file;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

/**
 * @component 默认是单例的
 * 
 * @author slHuang
 * @since 2019-02-10
 */
public class MyPropertiesFile extends PropertyPlaceholderConfigurer {

	private Map<String, Object> propMap;
	
	public MyPropertiesFile() {
		propMap = new HashMap<String, Object>();
	}
	
	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		for (Object key : props.keySet()) {
			String keyStr = key.toString();
			System.out.println(keyStr + ":" + props.getProperty(keyStr));
			propMap.put(keyStr, props.getProperty(keyStr));
		}
	}
	
	public void usePropertiesFile() {
		System.out.println(propMap);
		System.out.println(propMap.get("jdbc.url"));
	}
	
}
//TestApplication.java
package sping.analysis.properties.file;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestApplication {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		System.out.println( "Hello World!" );
		ApplicationContext context = new ClassPathXmlApplicationContext("application2.xml");
        
		System.out.println();
        System.out.println("------------Load properties file-------------->");
        System.out.println("通过继承PropertyPlaceholderConfigurer的方式,获取并存储properties:");
        MyPropertiesFile myPropertiesFile = (MyPropertiesFile) context.getBean("settingBean2");
        myPropertiesFile.usePropertiesFile();
        
        System.out.println();
        System.out.println("通过PropertyPlaceholderConfigurer类加载property文件,并使用@value('property_id')注解来获取值:");
        JDBC jdbc = (JDBC) context.getBean("jdbcBean");
        System.out.println(jdbc);
        System.out.println("<------------Load properties file--------------");
        
	}
	
}
//TestApplication2.java
package sping.analysis.properties.file;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @Resource等同于@Autowired + @Qualifier
 * @Resource默认是根据名称来依赖注入的
 * @Autowired默认是根据类型来依赖注入的
 * 
 * @author slHuang
 * @since 2019-02-10
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:application2.xml")
public class TestApplication2 {
	
	@Resource(name="jdbc2Bean")
	//@Autowired @Qualifier("jdbc2Bean")
	private JDBC2 jdbc2Bean;
	
	@Test
	public void test() {
		System.out.println(jdbc2Bean.toString());
	}
	
}
//application2.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"
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd">

    <!-- <context:annotation-config/> -->
    <context:component-scan base-package="sping.analysis.properties.file"/>
    
    <!-- S=Load Properties file -->
    <!-- 默认的
    <bean id="propertiesBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations" value="classpath:application.properties"/>
    </bean> -->
    <!-- 加载Proper文件的一种简写形式,包括了PropertyPlaceholderConfigurer和PropertiesFactoryBean
    <context:property-placeholder location="userinfo.properties"/> --> 

    <!-- 自己实现的  -->
    <bean id="settingBean2" class="sping.analysis.properties.file.MyPropertiesFile">
        <property name="locations" value="classpath:application.properties"/>
        <property name="fileEncoding" value="UTF-8"></property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>
    <!-- E=Load Properties file -->
 
    <bean id="settingBean" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations" value="classpath:application.properties"/>
        <property name="fileEncoding" value="UTF-8"></property>
    </bean>
 
    <!-- more bean definitions go here -->

</beans>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值