KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > PropertiesFactoryBean


1 /*
2  * Copyright 2002-2005 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 JavaDoc;
20 import java.util.Properties JavaDoc;
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 singleton.
38  *
39  * @author Juergen Hoeller
40  * @see java.util.Properties
41  */

42 public class PropertiesFactoryBean extends PropertiesLoaderSupport
43         implements FactoryBean, InitializingBean {
44
45     private boolean singleton = true;
46
47     private Object JavaDoc singletonInstance;
48
49
50     /**
51      * Set if a singleton should be created, or a new object
52      * on each request else. Default is "true" (a singleton).
53      */

54     public final void setSingleton(boolean singleton) {
55         this.singleton = singleton;
56     }
57
58     public final boolean isSingleton() {
59         return singleton;
60     }
61
62
63     public final void afterPropertiesSet() throws IOException JavaDoc {
64         if (this.singleton) {
65             this.singletonInstance = createInstance();
66         }
67     }
68
69     public final Object JavaDoc getObject() throws IOException JavaDoc {
70         if (this.singleton) {
71             return this.singletonInstance;
72         }
73         else {
74             return createInstance();
75         }
76     }
77
78     public Class JavaDoc getObjectType() {
79         return Properties JavaDoc.class;
80     }
81
82
83     /**
84      * Template method that subclasses may override to construct the object
85      * returned by this factory. Default returns the plain merged Properties.
86      * <p>Invoked on initialization of this FactoryBean in case of
87      * a singleton; else, on each <code>getObject()</code> call.
88      * @return the object returned by this factory
89      * @throws IOException if an exception occured during properties loading
90      * @see #getObject()
91      * @see #mergeProperties()
92      */

93     protected Object JavaDoc createInstance() throws IOException JavaDoc {
94         return mergeProperties();
95     }
96
97 }
98
99
Popular Tags