Spring技术入门(二)
来源:互联网 发布:淘宝做话费充值 编辑:程序博客网 时间:2024/05/22 20:01
Spring容器:
Spring有两个核心接口:BeanFactory和ApplicationContext,最基本的接口就是BeanFactory,BeanFactory负责配置、创建、管理Bean,ApplicationContext是它的子接口,也被称为Spring上下文。Spring容器还负责管理Bean与Bean之间的依赖关系。
BeanFactory接口包含如下几个基本方法:
boolean containsBean(String name):判断Spring容器是否包含id为name的Bean实例;
<T> T getBean(Class<T> requiredType):获取Spring容器中属于requiredType类型的、唯一的Bean的实例。
Object getBean(String name):返回容器id为name的Bean实例。
<T> T getBean(String name,Class requiredType):返回容器中id为name,并且类型为requiredType的Bean。
Class<?> getType(String name):返回容器中指定的Bean实例的类型。
这些东西在解压出来的DOCS文件里面都有说明,大家可以看自己研究
上面已经说了,ApplicationContext是BeanFactory的子接口,所以ApplicationContext的功能还更强大些,除了提供BeanFactory所支持的功能外,还包括其他功能:
An ApplicationContext provides:
- Bean factory methods for accessing application components. Inherited from
ListableBeanFactory
. - The ability to load file resources in a generic fashion. Inherited from the
ResourceLoader
interface. - The ability to publish events to registered listeners. Inherited from the
ApplicationEventPublisher
interface. - The ability to resolve messages, supporting internationalization. Inherited from the
MessageSource
interface. - Inheritance from a parent context. Definitions in a descendant context will always take priority. This means, for example, that a single parent context can be used by an entire web application, while each servlet has its own child context that is independent of that of any other servlet.
- In addition to standard
BeanFactory
lifecycle capabilities, ApplicationContext implementations detect and invokeApplicationContextAware
beans as well asResourceLoaderAware
,ApplicationEventPublisherAware
andMessageSourceAware
beans.
ApplicationContext的事件机制:
ApplicationContext的事件机制是观察者设计模式(让一个或多个观察者对象观察一个主题对象,当主题对象的状态发生变化时,系统能够通知所有的依赖于此对象的观察者对象,从而使得观察者对象能够自动更新)的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现AplicationContext的事件处理。如果容器中有一个ApplicationListener Bean,每当ApplicationContex发布ApplicationEvent时,ApplicationListener Bean将自动被触发。
Spring的事件框架两个钟重要成员:
ApplicationEvent:容器事件,必须由ApplicationContext发布。
ApplicationListener:监听器,可由容器中的任何监听器Bean担任。
容器中Bean的作用域:
Spring支持五种作用域,
singleton:单例模式,在整个Spring IOC容器中,使用singleton定义的Bean将只有一个实例。
prototype:原型模式,每次通过容器的getBean方法获取prototype定义的Bean时,都将产生一个新的Bean实例。
request:对于每次HTTP请求,使用request定义的Bean都将产生一个新实例,即每次HTTP请求将会产生不同的Bean实例。
session:同上
global session:同上
用的最多的也就是singleton和request,Spring默认使用的是singleton作用域。
Spring容器实例化:
容器实例化的时候,官方文档介绍的有两种方式加载Spring配置文件,一种是ClassPaht方式,一种是FileSystem方式。
ClassPaht方式:
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
FileSystem方式:
ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
如果有多个配置文件,可以在括号里写成{"a.xml","b.xml","c.xml"}
下面来写一个例子看看,大家的包一定要从下载的文件里面拷贝过来要不然会出错的
项目截图:
applicationContext.xml代码:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"></beans>以上代码没有加载任何的Bean
Spring容器初始化代码:
package cn.ailot;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest {public static void main(String[] args) {//创建spring的ApplicationContextApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//输出spring容器System.out.println(ctx);}}运行效果:
配置正确后运行就可以看到以上的效果。
上面初始化都是没有bean的,现在我们来加一个bean进去
MyBean.java
package cn.ailot;public class MyBean {public void sayHello(){System.out.println("hello world!");} }一个最简单的类。
applicationContext.xml代码:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="myBean" class="cn.ailot.MyBean"></bean></beans>SpringTest.java
package cn.ailot;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class SpringTest {public static void main(String[] args) {//创建spring的ApplicationContextApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");//输出spring容器System.out.println(ctx);MyBean bean = (MyBean)ctx.getBean("myBean"); //获取实例化的beanbean.sayHello(); //执行bean的方法}}加载bean后的运行结果图:
- Spring技术入门(二)
- Spring技术入门(一)
- Spring Security3 入门二
- Spring-WS(二)-入门
- Spring技术入门(三)
- Spring技术入门(四)
- Spring技术入门(五)
- Spring技术入门(六)
- Spring技术入门(七)
- jsp spring 技术(二) ----- IOC
- Spring IoC容器--入门 二
- struts2+spring+mybatis入门二
- Spring入门笔记(二)
- spring入门基础(二)
- spring aop入门(序列二)
- Spring入门学习(二)
- spring-boot入门(二)
- Spring MVC入门(二)
- 动物这么叫--纯虚函数
- JVM调优总结(一) ——概念
- CCITT标准CRC16(1021) Java代码
- Linux下使用交叉编译工具ltib开发powerpc(mpc83xx)
- msysGit 安装配置
- Spring技术入门(二)
- jQuery操作<input type="radio">
- ElasticSearch 原理笔记
- 网站特效
- Linux中rc的含义
- linux tomcat
- 二叉树的遍历
- UltraEdit不自动生成保存备份文件(.bak)
- 世界杯:一句顺其自然,里面包含了我多少绝望和不甘心