标签:
public class PropertiesFactoryBeanextends PropertiesLoaderSupportimplements FactoryBean, InitializingBean
Allows for making a properties file from a classpath location available as Properties instance in a bean factory. Can be used to populate any bean property of type Properties via a bean reference.
Supports loading from a properties file and/or setting local properties on this FactoryBean. The created Properties instance will be merged from loaded and local values. If neither a location nor local properties are set, an exception will be thrown on initialization.
Can create a singleton or a new object on each request. Default is a singleton.
一个系统中通常会存在如下一些以Properties形式存在的配置文件
1.数据库配置文件demo-db.properties:
2.消息服务配置文件demo-mq.properties:
3.远程调用的配置文件demo-remote.properties:
一、系统中需要加载多个Properties配置文件
应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。
配置方式:
我们也可以将配置中的List抽取出来:
二、整合多工程下的多个分散的Properties
应用场景:工程组中有多个配置文件,但是这些配置文件在多个地方使用,所以需要分别加载。
配置如下:
注意:其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true。这里一定需要按照这种方式设置 这两个参数。
三、Bean中直接注入Properties配置文件中的值
应用场景:Bean中需要直接注入Properties配置文件中的值 。例如下面的代码中需要获取上述demo-remote.properties中的值:
配置如下:
Client类中使用Annotation如下:
四、Bean中存在Properties类型的类变量
应用场景:当Bean中存在Properties类型的类变量需要以注入的方式初始化
1. 配置方式:我们可以用(三)中的配置方式,只是代码中注解修改如下
2. 配置方式:也可以使用xml中声明Bean并且注入
代码如下:
上述的各个场景在项目群中特别有用,需要灵活的使用上述各种配置方式。
原文:http://kingxss.iteye.com/blog/1880681
补充:
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.config;
import java.util.Properties;
import junit.framework.TestCase;
import org.springframework.core.JdkVersion;
import org.springframework.core.io.ClassPathResource;
/**
* @author Juergen Hoeller
* @since 01.11.2003
*/
public class PropertiesFactoryBeanTests extends TestCase {
public void testWithPropertiesFile() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
}
public void testWithPropertiesXmlFile() throws Exception {
// ignore for JDK < 1.5
if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) {
return;
}
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test-properties.xml"));
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
}
public void testWithLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
pfb.setProperties(localProps);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("value2", props.getProperty("key2"));
}
public void testWithPropertiesFileAndLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
localProps.setProperty("tb.array[0].age", "0");
pfb.setProperties(localProps);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
}
public void testWithPropertiesFileAndMultipleLocalProperties() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties props1 = new Properties();
props1.setProperty("key2", "value2");
props1.setProperty("tb.array[0].age", "0");
Properties props2 = new Properties();
props2.setProperty("spring", "framework");
props2.setProperty("Don", "Mattingly");
Properties props3 = new Properties();
props3.setProperty("spider", "man");
props3.setProperty("bat", "man");
pfb.setPropertiesArray(new Properties[] {props1, props2, props3});
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
assertEquals("framework", props.getProperty("spring"));
assertEquals("Mattingly", props.getProperty("Don"));
assertEquals("man", props.getProperty("spider"));
assertEquals("man", props.getProperty("bat"));
}
public void testWithPropertiesFileAndLocalPropertiesAndLocalOverride() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
localProps.setProperty("tb.array[0].age", "0");
pfb.setProperties(localProps);
pfb.setLocalOverride(true);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("0", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
}
public void testWithPrototype() throws Exception {
PropertiesFactoryBean pfb = new PropertiesFactoryBean();
pfb.setSingleton(false);
pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
Properties localProps = new Properties();
localProps.setProperty("key2", "value2");
pfb.setProperties(localProps);
pfb.afterPropertiesSet();
Properties props = (Properties) pfb.getObject();
assertEquals("99", props.getProperty("tb.array[0].age"));
assertEquals("value2", props.getProperty("key2"));
Properties newProps = (Properties) pfb.getObject();
assertTrue(props != newProps);
assertEquals("99", newProps.getProperty("tb.array[0].age"));
assertEquals("value2", newProps.getProperty("key2"));
}
}
标签:
原文地址:http://www.cnblogs.com/langtianya/p/5300388.html