Class BeanHelper


  • public final class BeanHelper
    extends java.lang.Object

    A helper class for creating bean instances that are defined in configuration files.

    This class provides static utility methods related to bean creation operations. These methods simplify such operations because a client need not deal with all involved interfaces. Usually, if a bean declaration has already been obtained, a single method call is necessary to create a new bean instance.

    This class also supports the registration of custom bean factories. Implementations of the BeanFactory interface can be registered under a symbolic name using the registerBeanFactory() method. In the configuration file the name of the bean factory can be specified in the bean declaration. Then this factory will be used to create the bean.

    Since:
    1.3
    Version:
    $Id: BeanHelper.java 1534393 2013-10-21 22:02:27Z henning $
    Author:
    Commons Configuration team
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Object createBean​(BeanDeclaration data)
      Returns a bean instance for the specified declaration.
      static java.lang.Object createBean​(BeanDeclaration data, java.lang.Class<?> defaultClass)
      Returns a bean instance for the specified declaration.
      static java.lang.Object createBean​(BeanDeclaration data, java.lang.Class<?> defaultClass, java.lang.Object param)
      The main method for creating and initializing beans from a configuration.
      static BeanFactory deregisterBeanFactory​(java.lang.String name)
      Deregisters the bean factory with the given name.
      static BeanFactory getDefaultBeanFactory()
      Returns the default bean factory.
      static void initBean​(java.lang.Object bean, BeanDeclaration data)
      Initializes the passed in bean.
      static void initBeanProperties​(java.lang.Object bean, BeanDeclaration data)
      Initializes the beans properties.
      static void registerBeanFactory​(java.lang.String name, BeanFactory factory)
      Register a bean factory under a symbolic name.
      static java.util.Set<java.lang.String> registeredFactoryNames()
      Returns a set with the names of all currently registered bean factories.
      static void setDefaultBeanFactory​(BeanFactory factory)
      Sets the default bean factory.
      static void setProperty​(java.lang.Object bean, java.lang.String propName, java.lang.Object value)
      Set a property on the bean only if the property exists
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • registerBeanFactory

        public static void registerBeanFactory​(java.lang.String name,
                                               BeanFactory factory)
        Register a bean factory under a symbolic name. This factory object can then be specified in bean declarations with the effect that this factory will be used to obtain an instance for the corresponding bean declaration.
        Parameters:
        name - the name of the factory
        factory - the factory to be registered
      • deregisterBeanFactory

        public static BeanFactory deregisterBeanFactory​(java.lang.String name)
        Deregisters the bean factory with the given name. After that this factory cannot be used any longer.
        Parameters:
        name - the name of the factory to be deregistered
        Returns:
        the factory that was registered under this name; null if there was no such factory
      • registeredFactoryNames

        public static java.util.Set<java.lang.String> registeredFactoryNames()
        Returns a set with the names of all currently registered bean factories.
        Returns:
        a set with the names of the registered bean factories
      • getDefaultBeanFactory

        public static BeanFactory getDefaultBeanFactory()
        Returns the default bean factory.
        Returns:
        the default bean factory
      • setDefaultBeanFactory

        public static void setDefaultBeanFactory​(BeanFactory factory)
        Sets the default bean factory. This factory will be used for all create operations, for which no special factory is provided in the bean declaration.
        Parameters:
        factory - the default bean factory (must not be null)
      • initBean

        public static void initBean​(java.lang.Object bean,
                                    BeanDeclaration data)
                             throws ConfigurationRuntimeException
        Initializes the passed in bean. This method will obtain all the bean's properties that are defined in the passed in bean declaration. These properties will be set on the bean. If necessary, further beans will be created recursively.
        Parameters:
        bean - the bean to be initialized
        data - the bean declaration
        Throws:
        ConfigurationRuntimeException - if a property cannot be set
      • setProperty

        public static void setProperty​(java.lang.Object bean,
                                       java.lang.String propName,
                                       java.lang.Object value)
        Set a property on the bean only if the property exists
        Parameters:
        bean - the bean
        propName - the name of the property
        value - the property's value
        Throws:
        ConfigurationRuntimeException - if the property is not writeable or an error occurred
      • createBean

        public static java.lang.Object createBean​(BeanDeclaration data,
                                                  java.lang.Class<?> defaultClass,
                                                  java.lang.Object param)
                                           throws ConfigurationRuntimeException
        The main method for creating and initializing beans from a configuration. This method will return an initialized instance of the bean class specified in the passed in bean declaration. If this declaration does not contain the class of the bean, the passed in default class will be used. From the bean declaration the factory to be used for creating the bean is queried. The declaration may here return null, then a default factory is used. This factory is then invoked to perform the create operation.
        Parameters:
        data - the bean declaration
        defaultClass - the default class to use
        param - an additional parameter that will be passed to the bean factory; some factories may support parameters and behave different depending on the value passed in here
        Returns:
        the new bean
        Throws:
        ConfigurationRuntimeException - if an error occurs
      • createBean

        public static java.lang.Object createBean​(BeanDeclaration data,
                                                  java.lang.Class<?> defaultClass)
                                           throws ConfigurationRuntimeException
        Returns a bean instance for the specified declaration. This method is a short cut for createBean(data, null, null);.
        Parameters:
        data - the bean declaration
        defaultClass - the class to be used when in the declaration no class is specified
        Returns:
        the new bean
        Throws:
        ConfigurationRuntimeException - if an error occurs