1 /* 2 * Copyright 2002-2010 the original author or authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.springframework.beans.factory.config; 18 19 import java.io.IOException; 20 import java.util.Properties; 21 22 import org.springframework.beans.factory.FactoryBean; 23 import org.springframework.beans.factory.InitializingBean; 24 import org.springframework.core.io.support.PropertiesLoaderSupport; 25 26 /** 27 * Allows for making a properties file from a classpath location available 28 * as Properties instance in a bean factory. Can be used to populate 29 * any bean property of type Properties via a bean reference. 30 * 31 * <p>Supports loading from a properties file and/or setting local properties 32 * on this FactoryBean. The created Properties instance will be merged from 33 * loaded and local values. If neither a location nor local properties are set, 34 * an exception will be thrown on initialization. 35 * 36 * <p>Can create a singleton or a new object on each request. 37 * Default is a singleton. 38 * 39 * @author Juergen Hoeller 40 * @see #setLocation 41 * @see #setProperties 42 * @see #setLocalOverride 43 * @see java.util.Properties 44 */ 45 public class PropertiesFactoryBean extends PropertiesLoaderSupport 46 implements FactoryBean<Properties>, InitializingBean { 47 48 private boolean singleton = true; 49 50 private Properties singletonInstance; 51 52 53 /** 54 * Set whether a shared 'singleton' Properties instance should be 55 * created, or rather a new Properties instance on each request. 56 * <p>Default is "true" (a shared singleton). 57 */ setSingleton(boolean singleton)58 public final void setSingleton(boolean singleton) { 59 this.singleton = singleton; 60 } 61 isSingleton()62 public final boolean isSingleton() { 63 return this.singleton; 64 } 65 66 afterPropertiesSet()67 public final void afterPropertiesSet() throws IOException { 68 if (this.singleton) { 69 this.singletonInstance = createProperties(); 70 } 71 } 72 getObject()73 public final Properties getObject() throws IOException { 74 if (this.singleton) { 75 return this.singletonInstance; 76 } 77 else { 78 return createProperties(); 79 } 80 } 81 getObjectType()82 public Class<Properties> getObjectType() { 83 return Properties.class; 84 } 85 86 87 /** 88 * Template method that subclasses may override to construct the object 89 * returned by this factory. The default implementation returns the 90 * plain merged Properties instance. 91 * <p>Invoked on initialization of this FactoryBean in case of a 92 * shared singleton; else, on each {@link #getObject()} call. 93 * @return the object returned by this factory 94 * @throws IOException if an exception occured during properties loading 95 * @see #mergeProperties() 96 */ createProperties()97 protected Properties createProperties() throws IOException { 98 return (Properties) createInstance(); 99 } 100 101 /** 102 * Template method that subclasses may override to construct the object 103 * returned by this factory. The default implementation returns the 104 * plain merged Properties instance. 105 * <p>Invoked on initialization of this FactoryBean in case of a 106 * shared singleton; else, on each {@link #getObject()} call. 107 * @return the object returned by this factory 108 * @throws IOException if an exception occured during properties loading 109 * @deprecated as of Spring 3.0, in favor of {@link #createProperties()} 110 */ 111 @Deprecated createInstance()112 protected Object createInstance() throws IOException { 113 return mergeProperties(); 114 } 115 116 } 117