今天在看spring与hibernate的代码,发现了一个地方非常难理解,就是我以前搭建的一个spring+hibernate的代码中在DAO中注入HibernateDaoSupport中注入的是的spring的LocalSessionFactoryBean,而属性确实hibernate的SessionFactory,考虑不通,按照正常的思路应该是要保ClassCaseException的,于是查询了一下spring refreence,发现了一句话的描述:
“This may be done by prepending the bean id with & when calling the getBean method of BeanFactory(including ApplicationContext). So for a given FactoryBean with an id myBean, invoking getBean("myBean")on the BeanFactory will return the product of the FactoryBean, but invoking getBean("&myBean") will returnthe FactoryBean instance itself”
这句话表明了事情的原委,如果是factorybean的话,他会取得他的方法的产品,所以诸如的是HibernateDaoSupport的产品,就是getSessionFactory的返回值。一个细小的地方,造成误会。
spring处理此处的地方代码如下,可以解释spring如何进行的数据处理
//仔细察看spring的注释
// Now we have the bean instance, which may be a normal bean or a FactoryBean.
// If it's 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;
if (logger.isDebugEnabled()) {
logger.debug("Bean with name '" + beanName + "' is a factory bean");
}
try {
beanInstance = factory.getObject();
}
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的BeanFactory的接口的中文解释
NULL 博文链接:https://lgd-java2eye.iteye.com/blog/756599
主要介绍了简单了解Spring中BeanFactory与FactoryBean的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
spring之beanfactory 的一些基本知识与其关系使用
Spring中ApplicationContext和beanfactory区别.rar
对BeanFactory的相关所有接口的功能的关键点进行了总结描述,中文版本
BeanFactory创建对象.docx
主要给大家介绍了关于Spring中BeanFactory与FactoryBean接口的区别的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
NULL 博文链接:https://364232252.iteye.com/blog/2369489
本篇文章主要介绍了Spring中BeanFactory解析bean详解 ,详细的介绍了使用BeanFactory对bean进行解析的实例,有兴趣的可以了解一下。
NULL 博文链接:https://ylxy3058.iteye.com/blog/2223489
spring3中实例化BeanFactory的3中方法
提供Spring框架的基本功能,其主要组件是BeanFactory,是工厂模式的实现。 Spring 上下文。向Spring 框架提供上下文信息,包括企业服务,如 JNDI、EJB、电子邮件、国际化、校验和调度等。 Spring AOP。通过配置管理...
Spring的IoC容器之BeanFactory 4.1 拥有BeanFactory之后的生活 4.2 BeanFactory的对象注册与依赖绑定方式 4.3 BeanFactory的XML之旅 4.4 容器背后的秘密 4.5 小结 Spring IoC容器ApplicationContext 5.1 统一资源...
Spring注解驱动开发第41讲——Spring IOC容器创建源码解析(一)之BeanFactory的创建以及预准备工作(合起来整个过程)
NULL 博文链接:https://bjameng.iteye.com/blog/1468057
主要为大家详细介绍了使用BeanFactory实现创建对象,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
spring 三种注入(就是从spring容器中将bean放入对象属性值中) Spring下描述依赖关系@Resource, @Autowired和@Inject的区别与联系 Spring中BeanFactory和ApplicationContext的区别 谈谈Spring IOC的理解,原理与...
Spring的IoC容器之BeanFactory[定义].pdf
BeanFactory是用于访问Spring Bean容器的根接口,是一个单纯的Bean工厂,也就是常说的IOC容器的顶层定义,各种IOC容器是在其基础上为了满