商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文

用FactoryBean让Spring配置动起来

作者:Feiing  2007-04-04

 【IT168技术文档】不少朋友讨论 spring 配置时认为 spring 配置中只能静态的设置一些参数(典型情况如数据库配置, 定时器配置等)导致不方便, 其实 spring 已经提供了非常便利的方式来实现动态配置, 我们要做的只是实现一个自己的 FactoryBean , 来看一下 FactoryBean 接口的定义

  代码

1/** 2* Interface to be implemented by objects used within a BeanFactory 3* that are themselves factories. If a bean implements this interface, 4* it is used as a factory, not directly as a bean. 5* 6* <p><b>NB: A bean that implements this interface cannot be used 7* as a normal bean.</b> A FactoryBean is defined in a bean style, 8* but the object exposed for bean references is always the object 9* that it creates. 10* 11* <p>FactoryBeans can support singletons and prototypes, and can 12* either create objects lazily on demand or eagerly on startup. 13* 14* <p>This interface is heavily used within the framework, for 15* example for the AOP ProxyFactoryBean or JndiObjectFactoryBean. 16* It can be used for application components, but this is not common 17* outside of infrastructure code. 18* 19* @author Rod Johnson 20* @author Juergen Hoeller 21* @since 08.03.2003 22* @see org.springframework.beans.factory.BeanFactory 23* @see org.springframework.aop.framework.ProxyFactoryBean 24* @see org.springframework.jndi.JndiObjectFactoryBean 25*/ 26public interface FactoryBean { 27 28 /** 29 * Return an instance (possibly shared or independent) of the object 30 * managed by this factory. As with a BeanFactory, this allows 31 * support for both the Singleton and Prototype design pattern. 32 * <p>If this method returns <code>null</code>, the factory will consider 33 * the FactoryBean as not fully initialized and throw a corresponding 34 * FactoryBeanNotInitializedException. 35 * @return an instance of the bean (should not be <code>null</code>; 36 * a <code>null</code> value will be considered as an indication of 37 * incomplete initialization) 38 * @throws Exception in case of creation errors 39 * @see FactoryBeanNotInitializedException 40 */ 41 Object getObject() throws Exception; 42 43 /** 44 * Return the type of object that this FactoryBean creates, or <code>null</code> 45 * if not known in advance. This allows to check for specific types 46 * of beans without instantiating objects, for example on autowiring. 47 * <p>For a singleton, this should try to avoid singleton creation 48 * as far as possible; it should rather estimate the type in advance. 49 * For prototypes, returning a meaningful type here is advisable too. 50 * <p>This method can be called <i>before</i> this FactoryBean has 51 * been fully initialized. It must not rely on state created during 52 * initialization; of course, it can still use such state if available. 53 * <p><b>NOTE:</b> Autowiring will simply ignore FactoryBeans that return 54 * <code>null</code> here. Therefore it is highly recommended to implement 55 * this method properly, using the current state of the FactoryBean. 56 * @return the type of object that this FactoryBean creates, 57 * or <code>null</code> if not known at the time of the call 58 * @see ListableBeanFactory#getBeansOfType 59 */ 60 Class getObjectType(); 61 62 /** 63 * Is the bean managed by this factory a singleton or a prototype? 64 * That is, will <code>getObject()</code> always return the same object 65 * (a reference that can be cached)? 66 * <p><b>NOTE:</b> If a FactoryBean indicates to hold a singleton object, 67 * the object returned from <code>getObject()</code> might get cached 68 * by the owning BeanFactory. Hence, do not return <code>true</code> 69 * unless the FactoryBean always exposes the same reference. 70 * <p>The singleton status of the FactoryBean itself will generally 71 * be provided by the owning BeanFactory; usually, it has to be 72 * defined as singleton there. 73 * @return if this bean is a singleton 74 * @see #getObject() 75 */ 76 boolean isSingleton(); 77 78} 79


  看了以后发现, FactoryBean 用于在 spring 容器中创建其他的 Bean, 我们平时用得最多的 JndiObjectFactoryBean, hibernate 的 LocalSessionFactoryBean 都是 FactoryBean 的具体实现, 既然如此, 读取动态配置就变得易如反掌了, 假如我们要实现动态读取数据库配置的功能, 拿使用率最高的 BasicDatasource 为例, 简单的实现一个 BasicDatasource FactoryBean 如下即可

  代码

1public class BasicDataSourceFactoryBean implements FactoryBean { 2 public Object getObject() throws Exception { 3  BasicDataSource dataSource = new BasicDataSource(); 4  // 读取外部配置, 设置到 dataSource 中 ... 5  return dataSource; 6 } 7 8 public Class getObjectType() { 9  return BasicDataSource.class; 10 } 11 12 public boolean isSingleton() { 13  return true; 14 } 15} 16


  然后在 spring 中如此声明

  代码

1<bean id="dataSource" class="BasicDataSourceFactoryBean "2... 你的配置来源 3/bean> 4 5

  就这么简单。

1
【内容导航】
第1页: 第1页
©版权所有。未经许可,不得转载。
[责任编辑:均岩]