01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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, InitializingBean {
47:
48: private boolean singleton = true;
49:
50: private Object singletonInstance;
51:
52: /**
53: * Set whether a shared 'singleton' Properties instance should be
54: * created, or rather a new Properties instance on each request.
55: * <p>Default is "true" (a shared singleton).
56: */
57: public final void setSingleton(boolean singleton) {
58: this .singleton = singleton;
59: }
60:
61: public final boolean isSingleton() {
62: return this .singleton;
63: }
64:
65: public final void afterPropertiesSet() throws IOException {
66: if (this .singleton) {
67: this .singletonInstance = createInstance();
68: }
69: }
70:
71: public final Object getObject() throws IOException {
72: if (this .singleton) {
73: return this .singletonInstance;
74: } else {
75: return createInstance();
76: }
77: }
78:
79: public Class getObjectType() {
80: return Properties.class;
81: }
82:
83: /**
84: * Template method that subclasses may override to construct the object
85: * returned by this factory. The default implementation returns the
86: * plain merged Properties instance.
87: * <p>Invoked on initialization of this FactoryBean in case of a
88: * shared singleton; else, on each {@link #getObject()} call.
89: * @return the object returned by this factory
90: * @throws IOException if an exception occured during properties loading
91: * @see #mergeProperties()
92: */
93: protected Object createInstance() throws IOException {
94: return mergeProperties();
95: }
96:
97: }
|