com.interface21.jdbc.core.support
Class JdbcBeanFactory

java.lang.Object
  |
  +--com.interface21.jdbc.core.support.JdbcBeanFactory
All Implemented Interfaces:
BeanFactory, ListableBeanFactory

public class JdbcBeanFactory
extends java.lang.Object
implements ListableBeanFactory

ListableBeanFactory implementation that reads values from a database table. Expects columns for bean name, property name and value as string. Formats for each are identical to the properties format recognized by ListableBeanFactoryImpl.

Version:
$Id: JdbcBeanFactory.java,v 1.2 2003/06/06 16:13:22 jhoeller Exp $
Author:
Rod Johnson

Constructor Summary
JdbcBeanFactory(javax.sql.DataSource ds, java.lang.String sql)
          Create a new JdbcBeanFactory
 
Method Summary
 java.lang.String[] getAliases(java.lang.String name)
          Return the aliases for the given bean name, if defined.
 java.lang.Object getBean(java.lang.String name)
          Return an instance (possibly shared or independent) of the given bean name.
 java.lang.Object getBean(java.lang.String name, java.lang.Class requiredType)
          Return an instance (possibly shared or independent) of the given bean name.
 int getBeanDefinitionCount()
          Return the number of beans defined in the factory
 java.lang.String[] getBeanDefinitionNames()
          Return the names of all beans defined in this factory
 java.lang.String[] getBeanDefinitionNames(java.lang.Class type)
          Return the names of beans matching the given object type (including subclasses).
 boolean isSingleton(java.lang.String name)
          Is this bean a singleton?
 void refresh()
          Refresh the factory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JdbcBeanFactory

public JdbcBeanFactory(javax.sql.DataSource ds,
                       java.lang.String sql)
Create a new JdbcBeanFactory
Parameters:
ds - datasource to use to obtain connections. The datasource will be cached during the life of this bean factory.
sql - the first three columns must be bean name, property name and value. Any join and any other columns are permitted: e.g. SELECT BEAN_NAME, PROPERTY, VALUE FROM CONFIG WHERE CONFIG.APP_ID = 1 It's also possible to perform a join. Column names are not significant--only the ordering of these first three columns.
Method Detail

refresh

public void refresh()
Refresh the factory. Uses copy-on-write for thread safety

getBean

public java.lang.Object getBean(java.lang.String name)
Description copied from interface: BeanFactory
Return an instance (possibly shared or independent) of the given bean name. This method allows a bean factory to be used as a replacement for the Singleton or Prototype design pattern.
Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.
Specified by:
getBean in interface BeanFactory
Following copied from interface: com.interface21.beans.factory.BeanFactory
Parameters:
name - name of the bean to return
Returns:
the instance of the bean
Throws:
NoSuchBeanDefinitionException - if there's no such bean definition

getBean

public java.lang.Object getBean(java.lang.String name,
                                java.lang.Class requiredType)
                         throws BeansException
Description copied from interface: BeanFactory
Return an instance (possibly shared or independent) of the given bean name. Provides a measure of type safety by throwing an exception if the bean is not of the required type. This method allows a bean factory to be used as a replacement for the Singleton or Prototype design pattern.
Note that callers should retain references to returned objects. There is no guarantee that this method will be implemented to be efficient. For example, it may be synchronized, or may need to run an RDBMS query.
Specified by:
getBean in interface BeanFactory
Following copied from interface: com.interface21.beans.factory.BeanFactory
Parameters:
name - name of the bean to return
requiredType - type the bean may match. Can be an interface or superclass of the actual class. For example, if the value is Object.class, this method will succeed whatever the class of the returned instance.
Returns:
the instance of the bean
Throws:
BeanNotOfRequiredTypeException - if the bean is not of the required type
NoSuchBeanDefinitionException - if there's no such bean definition

getBeanDefinitionCount

public int getBeanDefinitionCount()
Description copied from interface: ListableBeanFactory
Return the number of beans defined in the factory
Specified by:
getBeanDefinitionCount in interface ListableBeanFactory
Following copied from interface: com.interface21.beans.factory.ListableBeanFactory
Returns:
the number of beans defined in the factory

getBeanDefinitionNames

public java.lang.String[] getBeanDefinitionNames()
Description copied from interface: ListableBeanFactory
Return the names of all beans defined in this factory
Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Following copied from interface: com.interface21.beans.factory.ListableBeanFactory
Returns:
the names of all beans defined in this factory. Returns the empty String[], rather than null, if no beans are defined.

getBeanDefinitionNames

public java.lang.String[] getBeanDefinitionNames(java.lang.Class type)
Description copied from interface: ListableBeanFactory
Return the names of beans matching the given object type (including subclasses).
Specified by:
getBeanDefinitionNames in interface ListableBeanFactory
Following copied from interface: com.interface21.beans.factory.ListableBeanFactory
Parameters:
type - class or interface to match
Returns:
the names of beans matching the given object type (including subclasses). Never returns null.

isSingleton

public boolean isSingleton(java.lang.String name)
                    throws NoSuchBeanDefinitionException
Description copied from interface: BeanFactory
Is this bean a singleton? That is, will getBean() always return the same object?
Specified by:
isSingleton in interface BeanFactory
Following copied from interface: com.interface21.beans.factory.BeanFactory
Parameters:
name - name of the bean to query
Returns:
is this bean a singleton
Throws:
NoSuchBeanDefinitionException - if there is no bean with the given name

getAliases

public java.lang.String[] getAliases(java.lang.String name)
Description copied from interface: BeanFactory
Return the aliases for the given bean name, if defined.
Specified by:
getAliases in interface BeanFactory
Following copied from interface: com.interface21.beans.factory.BeanFactory
Parameters:
name - the bean name to check for aliases
Returns:
the aliases, or an empty array if none


Rod Johnson and Spring contributors 2001-2003.