Spring之FactoryBean(工厂Bean)
Spring之FactoryBean(工厂Bean)
org.springframework.beans.factory.FactoryBean是一个接口,是Spring中用于定义自定义服务的核心接口,他本身是在bean工厂中定义的一个bean,同时又是用于创建另一个Spring服务对象的工厂。
FactoryBean.java
package org.springframework.beans.factory;
public interface FactoryBean {
* Return an instance (possibly shared or independent) of the object
* managed by this factory. As with a BeanFactory, this allows
* support for both the Singleton and Prototype design pattern.
*
If this method returns null, the factory will consider the
* FactoryBean as not fully initialized and throw a corresponding
* FactoryBeanNotInitializedException.
* @return an instance of the bean (should not be null; a null value
* will be considered as an indication of incomplete initialization)
* @throws Exception in case of creation errors
* @see FactoryBeanNotInitializedException
Object getObject() throws Exception;
Class getObjectType();
boolean isSingleton();
}
下面是FactoryBean的getObject方法的调用层次。
我们可以看出,当我们用BeanFactory或ApplicationContext去那getBean()时,如果这个Bean是被一个BeanFactory,那么BeanFactory得到的会是FactoryBean.getObject()的对象。
AbstractBeanFactory的getObjectForSharedInstance(String,Object)方法。
// Now we have the bean instance, which may be a normal bean or a FactoryBean.
// If it&aposs a FactoryBean, we use it to create a bean instance, unless the
// caller actually wants a reference to the factory.
if (beanInstance instanceof FactoryBean) {
if (!isFactoryDereference(name)) {
// Return bean instance from factory.
FactoryBean factory = (FactoryBean) beanInstance; //Cast toFactoryBean.
if (logger.isDebugEnabled()) {
logger.debug("Bean with name '" + beanName + "' is a factory bean");
}
try {
beanInstance = factory.getObject(); // Get the realObject
}
catch (Exception ex) {
throw new BeanCreationException(beanName, "FactoryBean threw exception on object creation", ex);
}
if (beanInstance == null) {
throw new FactoryBeanNotInitializedException(
beanName, "FactoryBean returned null object: " +
"probably not fully initialized (maybe due to circular bean reference)");
}
}
else {
// The user wants the factory itself.
if (logger.isDebugEnabled()) {
logger.debug("Calling code asked for FactoryBean instance for name '" + beanName + "'");
}
}
}
Spring通过FactoryBean来提供内置的许多有用的核心服务。当然你也可以实现自己的FactoryBean,但是你大不可以不必这样做,Spring已经提供了大量的。
-
org.springframework.jndi JndiObjectFactoryBean:JNDI LookUp。
-
org.springframework.orm.hibernate3. LocalSessionFactoryBean:配置本地的Hibernate SessionFactory的工厂bean。
-
org.springframework.aop.framework ProxyFactoryBean:通用的用于获得AOP代理的工厂bean。
-
org.springframework.transaction.interceptor.TransactionProxyFactoryBean:用于为对象创建事务代理,用于实现简介易用申明性事务管理。
-
…………etc
发表评论
- 浏览: 220281 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
共 13 张
最近加入圈子
最新评论
-
使用Terracotta和Tomcat建 ...
renavatior 写道"运行start.bat 9081 这样我们就启动了目 ...
-- by rainsf -
使用Terracotta和Tomcat建 ...
"运行start.bat 9081 这样我们就启动了目录9081中的tomcat ...
-- by renavatior -
广州3年多经验 5500的 ...
fucku 写道广州的软件厂家可比深圳多多了,不过比起北京上海来,还是少了很多, ...
-- by yongfan_420 -
广州3年多经验 5500的 ...
广州的软件厂家可比深圳多多了,不过比起北京上海来,还是少了很多,导致机会也没有这 ...
-- by fucku -
广州3年多经验 5500的 ...
想高工资就去厂家咯,老在集成商里面混能有多大个奔头
-- by fucku






评论排行榜