spring技术内幕笔记:XMLBeanFactory
来源:互联网 发布:手机淘宝怎么看评论 编辑:程序博客网 时间:2024/05/16 09:58
1.BeanFactory
(1)定义了spring IoC容器的最基本形式,包含了一系列的获取容器中bean的方法,以及获取bean其他信息的方法
(2)一般由其他的容器类如ApplicationContext、XMLBeanFactory实现该接口,管理IoC中的bean。
/** * Ioc容器:BeanFactory,是一个接口,定义了基本的IoC容器规范,包含了一系列的获取容器中bean的方法 */public interface BeanFactory {/** * 用来区分通过容器获取FactoryBean产生的对象和获取FactoryBean本身 * e.g.: &myJndiObject获取的是FactoryBean * FactoryBean是一个bean,BeanFactory是一个bean工厂,Spring中所有的bean都由BeanFactory管理 */String FACTORY_BEAN_PREFIX = "&";/** * 通过name获取容器中管理的bean * @param name bean的name * @return bean的一个实例 */Object getBean(String name) throws BeansException;/** * 通过name和class类型获取bean * @param name bean的name * @param requiredType bean的类型 * @return an instance of the bean */<T> T getBean(String name, Class<T> requiredType) throws BeansException;/** * 通过class 类型获取bean * @return an instance of the single bean matching the required type */<T> T getBean(Class<T> requiredType) throws BeansException;/** * 根据name和显示声明的参数获取bean * @param name * @param args 原型模式中显示声明的参数 * @return an instance of the bean */Object getBean(String name, Object... args) throws BeansException;/** * 根据class类型和显示声明的参数获取bean * @param requiredType * @param args * @return an instance of the bean */<T> T getBean(Class<T> requiredType, Object... args) throws BeansException;/** * 根据指定的name判断容器中是否包含这个bean * @param name the name of the bean to query * @return whether a bean with the given name is present */boolean containsBean(String name);/** * 根据name查询该bean是否是单例模式的bean * @param name the name of the bean to query * @return whether this bean corresponds to a singleton instance */boolean isSingleton(String name) throws NoSuchBeanDefinitionException;/** * 根据name查询该bean是否是原型模式的bean * @param name the name of the bean to query * @return whether this bean will always deliver independent instances */boolean isPrototype(String name) throws NoSuchBeanDefinitionException;/** * 根据指定的name和type,判断以这个name命名的bean是否符合指定的class类型 * @param name the name of the bean to query * @param targetType the type to match against * @return {@code true} if the bean type matches, */boolean isTypeMatch(String name, Class<?> targetType) throws NoSuchBeanDefinitionException;/** * 获取指定name的bean的class类型 * @param name the name of the bean to query * @return the type of the bean, or {@code null} if not determinable */Class<?> getType(String name) throws NoSuchBeanDefinitionException;/** * 根据指定name获取bean的所有别名 * @param name the bean name to check for aliases * @return the aliases, or an empty array if none */String[] getAliases(String name);}
2.BeanDefinition
描述了容器中的bean对象,在bean容器启动中,其实是将xml配置文件中每个<bean></bean>元素转换成了一个BeanDefinition对象
3.DefaultListableBeanFactory
(1)包含了基本IoC容器所具有的重要功能
(2)其他容器可以继承该类,具备容器的基本功能,然后在此基础上,扩展其他功能。
4.XMLBeanFactory
(1)Spirng IoC容器的一个实现,继承了DefaultListableBeanFactory,实现容器的基本功能
(2)扩展功能:通过读取以XML文件方式定义的BeanDefinition的IoC容器
(3)该类已经不推荐使用
/** * XmlBeanFactory(现已被废弃,不推荐使用) * Spirng IoC容器的一个实现,继承了DefaultListableBeanFactory,实现容器的基本功能 * 扩展功能:通过读取以XML文件方式定义的BeanDefinition的IoC容器 */@Deprecated@SuppressWarnings({"serial", "all"})public class XmlBeanFactory extends DefaultListableBeanFactory { /** * XmlBeanDefinitionReader对象,从资源对象中载入BeanDefination */ private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this); /** * 构造函数,通过传入一个资源对象,创建实例 * @param resource spring中的资源对象(XML) */ public XmlBeanFactory(Resource resource) throws BeansException { this(resource, null); } /** * 构造函数 * @param resource spring中的资源对象(XML) * @param parentBeanFactory BeanFactory对象 * @throws BeansException in case of loading or parsing errors */ public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException { super(parentBeanFactory); //从资源对象中载入BeanDefination this.reader.loadBeanDefinitions(resource); }}
编程式IoC建立过程:
/*-------IoC容器的建立过程------*/ //加载xml配置文件,实例化资源对象 ClassPathResource resource=new ClassPathResource("beans.xml"); //创建一个BeanFactory,这里使用DefaultListableBeanFactory DefaultListableBeanFactory factory=new DefaultListableBeanFactory(); //创建reader对象 XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(factory); //载入xml文件形式的Beandefinition,通过一个回调配置给BeanFactory reader.loadBeanDefinitions(resource);
参考:
(1)spring技术内幕
(2)digdeep:深入剖析 Spring 框架的 BeanFactory
(3)一天不进步,就是退步:spring beans源码解读之--XmlBeanFactory
阅读全文
0 0
- spring技术内幕笔记:XMLBeanFactory
- Spring 技术内幕---XmlBeanFactory涉及到的回调
- 读spring技术内幕--笔记
- Spring 技术内幕初探 笔记1
- spring xmlBeanFactory
- 《Spring技术内幕》笔记-第三章 Spring AOP的实现
- Spring技术内幕
- Spring技术内幕总结
- Spring技术内幕-架构
- spring 技术内幕--IOC
- Spring技术内幕
- Spring 技术内幕
- spring技术内幕
- Spring 技术内幕-读书笔记
- Spring技术内幕1
- Spring技术内幕2
- 《Spring技术内幕》学习笔记__IoC容器体系结构
- 《Spring技术内幕》学习笔记7——AOP基础
- 布局动画
- 209. Minimum Size Subarray Sum
- Shiro初识HelloWorld
- 07_BootScrap——CRUD(动态页面下篇)方法二
- 检验的要求
- spring技术内幕笔记:XMLBeanFactory
- 输出空心的菱形
- jhat使用
- 【NOIP2012提高组D2T3】【洛谷P1081】开车旅行
- 文章标题
- nginx中ngx_http_access_module做访问控制
- Linux系统上各种软件安装 卸载
- Web App之记账本
- 用户评论实体识别及情感分类