spring技术内幕 读书笔记(二)

版权声明: https://blog.csdn.net/xclltssun/article/details/61422150

spring IoC 容器
简单的说,容器启动后把需要的加载的bean全部实例化,放到Map。等需要的时候从Map里取。

定位,载入,注册 (一共三步)
定位:找到要加载的源文件
用过ClassPathResource等来寻找到源文件。
载入:读取源文件,实例化对象
访问到的文件是字节码,需要还原POJO对象,对象是单例是原型,对象是懒惰加载还是即时加载,都通过BeanDefinition类实现。BeanDefinition是最关键类,没有其他可选,可以理解为要放入Map里的实例化对象。
注册:把实例化的对象放到Map中
DefaultListableBeanFactory是唯一实现了ConfigurableListableBeanFactory接口的类,所以它是最关键的类,只要用IOC就必须用到,没有其他可选。该类中私有属性beanDefinitionMap就是存放bean的实例对象,注册使用registerBeanDefinition方法。


问:通常我们定义的类在application_spring.xml文件中的class,我们使用ClassPathResource类加载。还有其他地方的class如何加载呢?

ClassPathResource类 以类路径的方式访问
ClassPathResource类实现AbstractFileResolvingResource还有其他类,也就是说还有其他方法加载方法
UrlResource类 通过url调用远程方式访问
ServletContextResource类 web应用根目录的方式访问(为了知道目录根在哪,所以它还实现ContextResource)
PortletContextResource类 web应用根目录的方式访问(为了知道目录根在哪,所以它还实现ContextResource,注意这里是Portlet容器非Servlet容器)

AbstractFileResolvingResource实现了AbstractResource抽象类
FileSystemResource类 以文件的绝对路径方式进行访问(有时候磁盘文件不可读呢,所以它还实现WritableResource)
VfsResource类 使用Vfs方式进行访问
InputStreamResource类 使用读取字符流方式进行访问
ByteArrayResource类 使用读取字节流方式进行访问
PathResource类 Resource implementation for java.nio.file.Path handles. Supports resolution as File, and also as URL
GzipResourceResolver的私有GzippedResource类 读取Gzip压缩访问
DescriptiveResource类 通过描述资源访问

看到了一篇相关文章总结的不错
http://www.cnblogs.com/zrtqsk/archive/2014/10/15/4015985.html

问:BeanFactory和BeanDefinition有什么关联?
BeanFactory 是bean的工厂,它主要是做bean的操作。
BeanDefinition是bean的定义,它描述了bean的数据结构,也可以理解为在IOC中抽象的对象。

问:XmlBeanFactory为什么被废弃了?
XmlBeanFactory类(spring3.1就已经废弃,官方文档:For advanced needs, consider using a DefaultListableBeanFactory with an XmlBeanDefinitionReader. The latter allows for reading from multiple XML resources and is highly configurable in its actual XML parsing behavior.就是说现在更牛了)

问:BeanFactory都有哪些?
BeanFactory接口
这是IoC的核心接口,定义了bean的规范,抽象方法包括
返回一个实例
Object getBean(String name) throws BeansException;
是否包含定义bean
boolean containsBean(String name);
是否定义了一个单例bean
boolean isSingleton(String name)
是否定义了一个原型bean
boolean isPrototype(String name)
给定的bean类型是否匹配
boolean isTypeMatch(String name, ResolvableType typeToMatch)
得到bean的类型
Class<?> getType(String name)
得到bean的别名
String[] getAliases(String name);

BeanFactory接口的三个子接口
HierarchicalBeanFactory接口
Sub-interface implemented by bean factories that can be part of a hierarchy.
得到了父bean的控制可以理解为更多层级的控制。
ListableBeanFactory接口
Extension of the BeanFactory interface to be implemented by bean factories that can enumerate all their bean instances, rather than attempting bean lookup by name one by one as requested by clients. BeanFactory implementations that preload all their bean definitions (such as XML-based factories) may implement this interface.
全部是对BeanDefinition的操作,返回BeanDefinition名字,类型,是否包含等等
AutowireCapableBeanFactory接口
Extension of the BeanFactory interface to be implemented by bean factories that are capable of autowiring, provided that they want to expose this functionality for existing bean instances.
在BeanFactory基础上实现对其自动装配,如创建,实例化,销毁, 实例化前后方法等等。
ConfigurableBeanFactory接口
Configuration interface to be implemented by most bean factories. Provides
facilities to configure a bean factory, in addition to the bean factory client methods in the BeanFactory interface.
主要添加了配置功能,设置bean的父层级,配置bean后置处理,缓存bean metadata,作用域定义区等。
该接口extends HierarchicalBeanFactory, SingletonBeanRegistry。
ConfigurableListableBeanFactory接口
Configuration interface to be implemented by most listable bean factories.
In addition to ConfigurableBeanFactory, it provides facilities to analyze and modify bean definitions, and to pre-instantiate singletons.
该接口继承了ConfigurableListableBeanFactory,ListableBeanFactory,AutowireCapableBeanFactory接口,继承了上述的所有接口,还增加了其他功能:忽略自动装配注解依赖类型,忽略自动装配注解依赖类,注册依赖,遍历bean名字,冻结配置等等。

相关文章 http://blog.csdn.net/u011179993/article/details/51598567

问:实例化对象都是返回对象的本身么?
不是,如果实例化的对象实现了FactoryBean,那么返回的是泛型中的对象。因为创建bean的时候默认调用了getObject()方法。那如果非要实现返回的是原来对象,那么请加&

猜你喜欢

转载自blog.csdn.net/xclltssun/article/details/61422150