7.1JavaEE——Spring中的Bean的管理——Spring IoC容器

一、BeanFactory接口 BeanFactory接口的常用方法

方法名称

描述

getBean(String name) 

根据参数名称获取Bean 

getBean(String name,Class<T> type) 

根据参数名称、参数类型获取Bean 

<T>T getBean(Class<T> requiredType) 

根据参数类型获取Bean 

Object getBean(String name,Object... args) 

根据参数名称获取Bean 

isTypeMatch(String name,Resolvable Typetype) 

判断是否有与参数名称、参数类型匹配的Bean 

Class <?>getType(String name) 

根据参数名称获取类型 

String[] getAliases(String name) 

根据实例的名字获取实例的别名数组 

boolean containsBean(String name) 

根据Bean的名称判断Spring容器是否含有指定的 

Bean 

 BeanFactory接口实例的语法格式

BeanFactory beanFactory=new XmlBeanFactory
	(new FileSystemResource(”D:/bean.xml”));

        Spring提供了几个BeanFactory接口的实现类,其中最常用的是XmlBeanFactory,它可以读取XML文件并根据XML文件中的配置信息生成BeanFactory接口的实例,BeanFactory接口的实例用于管理Bean。XmlBeanFactory类读取XML文件生成BeanFactory接口实例的具体语法格式如下。

二、ApplicationContext接口 

ApplicationContext接口的特点        

        ApplicationContext接口建立在BeanFactory接口的基础之上,它丰富了BeanFactory接口的特性,例如,添加了对国际化、资源访问、事件传播等方面的支持。

        ApplicationContext接口可以为单例的Bean实行预初始化,并根据<property>元素执行setter方法,单例的Bean可以直接使用,提升了程序获取Bean实例的性能。 

ApplicationContext接口的常用实现类 

类名称

描述

ClassPathXmlApplicationContext

从类路径加载配置文件,实例化ApplicationContext接口

FileSystemXmlApplicationContext

从文件系统加载配置文件,实例化ApplicationContext接口

AnnotationConfigApplicationContext

从注解中加载配置文件,实例化ApplicationContext接口

WebApplicationContext

在Web应用中使用,从相对于Web根目录的路径中加载配置文件,

实例化ApplicationContext接口

ConfigurableWebApplicationContext

扩展了WebApplicationContext类,

它可以通过读取XML配置文件的方式实例化

WebApplicationContext类

猜你喜欢

转载自blog.csdn.net/W_Fe5/article/details/131666218