org.springframework.beans.factory.config
Class PropertiesFactoryBean

java.lang.Object
  extended byorg.springframework.beans.factory.config.PropertiesFactoryBean
All Implemented Interfaces:
FactoryBean, InitializingBean

public class PropertiesFactoryBean
extends java.lang.Object
implements FactoryBean, InitializingBean

Allows for making a properties file from a classpath location available as Properties instance in a bean factory. Can be used for 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 singleton.

Author:
Juergen Hoeller
See Also:
Properties

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
PropertiesFactoryBean()
           
 
Method Summary
 void afterPropertiesSet()
          Create a singleton instance on initialization if in singleton mode.
 java.lang.Object getObject()
          Return either singleton instance or newly created instance, depending on the singleton property of this FactoryBean.
 java.lang.Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
 boolean isSingleton()
          Is the bean managed by this factory a singleton or a prototype? That is, will getObject() always return the same object?
protected  java.util.Properties loadProperties()
          Load the Properties instance.
protected  java.util.Properties mergeProperties()
          Return a merged Properties instance containing both the loaded properties and properties set on this FactoryBean.
 void setCharset(java.lang.String charset)
          Set the charset to use for parsing properties files.
 void setLocation(Resource location)
          Set a location of a properties file to be loaded.
 void setLocations(Resource[] locations)
          Set locations of properties files to be loaded.
 void setProperties(java.util.Properties properties)
          Set local properties, e.g.
 void setPropertiesPersister(PropertiesPersister propertiesPersister)
          Set the PropertiesPersister to use for parsing properties files.
 void setSingleton(boolean singleton)
          Set if a singleton should be created, or a new object on each request else.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

PropertiesFactoryBean

public PropertiesFactoryBean()
Method Detail

setProperties

public void setProperties(java.util.Properties properties)
Set local properties, e.g. via the "props" tag in XML bean definitions. These can be considered defaults, to be overridden by properties loaded from files.


setLocation

public void setLocation(Resource location)
Set a location of a properties file to be loaded.


setLocations

public void setLocations(Resource[] locations)
Set locations of properties files to be loaded.


setCharset

public void setCharset(java.lang.String charset)
Set the charset to use for parsing properties files. Default is none, using java.util.Properties' default charset.

See Also:
PropertiesPersister.load(java.util.Properties, java.io.InputStream)

setPropertiesPersister

public void setPropertiesPersister(PropertiesPersister propertiesPersister)
Set the PropertiesPersister to use for parsing properties files. The default is DefaultPropertiesPersister.

See Also:
DefaultPropertiesPersister

setSingleton

public void setSingleton(boolean singleton)
Set if a singleton should be created, or a new object on each request else. Default is true.


afterPropertiesSet

public void afterPropertiesSet()
                        throws java.io.IOException
Create a singleton instance on initialization if in singleton mode.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
java.io.IOException

getObject

public java.lang.Object getObject()
                           throws java.io.IOException
Return either singleton instance or newly created instance, depending on the singleton property of this FactoryBean. Delegates to mergeProperties for actual instance creation.

Specified by:
getObject in interface FactoryBean
Returns:
an instance of the bean (should never be null)
Throws:
java.io.IOException
See Also:
mergeProperties()

mergeProperties

protected java.util.Properties mergeProperties()
                                        throws java.io.IOException
Return a merged Properties instance containing both the loaded properties and properties set on this FactoryBean.

Throws:
java.io.IOException

loadProperties

protected java.util.Properties loadProperties()
                                       throws java.io.IOException
Load the Properties instance. Invoked either by afterPropertiesSet or by getObject, depending on singleton or prototype mode.

Returns:
the freshly loaded Properties instance
Throws:
java.io.IOException - in case of I/O errors.

getObjectType

public java.lang.Class getObjectType()
Description copied from interface: FactoryBean
Return the type of object that this FactoryBean creates, or null if not known in advance. This allows to check for specific types of beans without instantiating objects, e.g. on autowiring.

For a singleton, this can simply return getObject().getClass(), or even null, as autowiring will always check the actual objects for singletons. For prototypes, returning a meaningful type here is highly advisable, as autowiring will simply ignore them else.

Specified by:
getObjectType in interface FactoryBean
Returns:
the type of object that this FactoryBean creates, or null
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class, boolean, boolean)

isSingleton

public boolean isSingleton()
Description copied from interface: FactoryBean
Is the bean managed by this factory a singleton or a prototype? That is, will getObject() always return the same object?

The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory.

Specified by:
isSingleton in interface FactoryBean
Returns:
if this bean is a singleton


Copyright (C) 2003-2004 The Spring Framework Project.