org.springframework.beans.factory.config
Class PropertiesFactoryBean

java.lang.Object
  extended byorg.springframework.beans.factory.config.AbstractFactoryBean
      extended byorg.springframework.beans.factory.config.PropertiesFactoryBean
All Implemented Interfaces:
FactoryBean, InitializingBean
Direct Known Subclasses:
ResourceMapFactoryBean

public class PropertiesFactoryBean
extends AbstractFactoryBean

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
 
Fields inherited from class org.springframework.beans.factory.config.AbstractFactoryBean
logger
 
Constructor Summary
PropertiesFactoryBean()
           
 
Method Summary
protected  Object createInstance()
          Template method that subclasses must override to construct the object returned by this factory.
 Class getObjectType()
          Return the type of object that this FactoryBean creates, or null if not known in advance.
protected  void loadProperties(Properties props)
          Load properties into the given instance.
protected  Properties mergeProperties()
          Return a merged Properties instance containing both the loaded properties and properties set on this FactoryBean.
 void setFileEncoding(String encoding)
          Set the encoding 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(Properties properties)
          Set local properties, e.g. via the "props" tag in XML bean definitions.
 void setPropertiesPersister(PropertiesPersister propertiesPersister)
          Set the PropertiesPersister to use for parsing properties files.
 
Methods inherited from class org.springframework.beans.factory.config.AbstractFactoryBean
afterPropertiesSet, getObject, isSingleton, setSingleton
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertiesFactoryBean

public PropertiesFactoryBean()
Method Detail

setProperties

public void setProperties(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.


setFileEncoding

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

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

getObjectType

public 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, for example on autowiring.

For a singleton, this should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.

This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.

NOTE: Autowiring will simply ignore FactoryBeans that return null here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.

Returns:
the type of object that this FactoryBean creates, or null if not known at the time of the call
See Also:
ListableBeanFactory.getBeansOfType(java.lang.Class)

createInstance

protected Object createInstance()
                         throws Exception
Description copied from class: AbstractFactoryBean
Template method that subclasses must override to construct the object returned by this factory.

Invoked on initialization of this FactoryBean in case of a singleton; else, on each getObject() call.

Specified by:
createInstance in class AbstractFactoryBean
Returns:
the object returned by this factory
Throws:
Exception - if an exception occured during object creation
See Also:
AbstractFactoryBean.getObject()

mergeProperties

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

Throws:
IOException

loadProperties

protected void loadProperties(Properties props)
                       throws IOException
Load properties into the given instance.

Throws:
IOException - in case of I/O errors
See Also:
setLocations(org.springframework.core.io.Resource[])


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