(转)Spring读书笔记-----使用Spring容器(二)
一、使用ApplicationContext
前面介绍了,我们一般不会使用BeanFactory实例作为Spring容器,而是使用ApplicationContext实例作为容器,它增强了BeanFactory的功能。
ApplicationContext允许以声明式方式操作容器,无须手动创建它。在Web应用启动时自动创建ApplicationContext。当然,也可以采用编程方式创建ApplicationContext。
除了提供BeanFactory所支持的全部功能外,ApplicationContext还有如下功能:
1、 ApplicationContext继承MessageSource接口,因此提供国际化支持。
2、 资源访问。
3、 事件机制。
4、 载入多个配置文件。
5、 以声明式的方式启动,并创建Spring容器。
当系统创建ApplicationContext容器时,默认会预初始化所有的singleton Bean。也就是说,当ApplicationContext容器初始化完成后,容器中所有singleton Bean也实例化完成,这就意味着:系统前期创建ApplicationContext时将有较大的系统开销,但一旦ApplicationContext初始化完成,程序后面获取singleton Bean实例时将拥有较好的性能。
二、ApplicationContext的国际化支持
ApplicationContext接口继承MessageSource接口,因此具备国际化功能。MessageSource接口中定义了三个方法用于国际化功能。
String getMessage(Stringcode,Object[] args,Locale loc);
StringgetMessage(String code,Object[] args,String default,Locale loc);
StringgetMessage(MessageSourceResolvable resolvable,Local loc);
ApplicationContext正是通过这三个方法来实现国际化的。当程序创建ApplicationContext容器时,Spring会自动查找在配置文件中名为messageSource的bean实例,一旦找到这个Bean实例,上述三个方法的调用被委托给该MessageSource Bean。如果没有该Bean,ApplicationContext会查找其父定义中的messagesource Bean,如果找到,它会作为messageSource Bean使用。但是如果无法找到messageSource,系统将会创建一个空的staticMessageSource Bean,该Bean的能接受上述三个方法的调用。
在Spring中配置messagesourceBean时通常使用ResourceBundleMessageSource.如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="messsageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
- <property name="basenames">
- <list>
- <value>.....</value>
- <value>.....</value>
- <value>.....</value>
- </list>
- </property>
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="messsageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>.....</value>
<value>.....</value>
<value>.....</value>
</list>
</property>
</bean>
</beans>
三、ApplicationContext的事件机制
ApplicationContext的事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener接口,可以实现ApplicationContext的事件处理。如果容器中有一个ApplicationListener
Bean,每当ApplicationContext发布 ApplicationEvent时,ApplicationListenerBean就会自动触发。
Spring的事件框架有如下两个重要成员:
1、 ApplicationEvent:容器事件,必须由ApplicationContext发布。
2、 ApplicationListener:监听器,可由容器中的任何监听器Bean担任。
Spring的事件机制需要事件源、事件和事件监听器组成。只是此处的事件是ApplicationContext,且事件必须由java程序显示触发。下图简单示范了ApplicationContext的事件流程。
下面实例展示了Spring容器的事件机制。
1)、定义一个ApplicationEvent类,其对象就是Spring容器事件。
- <span style="font-family:Arial;">public class EmailEvent extends ApplicationEvent {
- private static final long serialVersionUID = 1L;
- private String address;
- private String text;
- // 定义一个带参的构造函数
- public EmailEvent(Object source) {
- super(source);
- }
- public EmailEvent(Object source, String address, String text) {
- super(source);
- this.address = address;
- this.text = text;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- public String getText() {
- return text;
- }
- public void setText(String text) {
- this.text = text;
- }
- }</span>
public class EmailEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L; private String address;
private String text; // 定义一个带参的构造函数
public EmailEvent(Object source) {
super(source);
} public EmailEvent(Object source, String address, String text) {
super(source);
this.address = address;
this.text = text;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getText() {
return text;
} public void setText(String text) {
this.text = text;
} }
容器事件的监听器类必须实现ApplicationListener接口,它的实现方法如下:
onAPplicationEvent(ApplicationEventevent):每当容器内发生任何事件时,此方法都会被触发。
2)、编写该容器的监听器类。
- public class EmailNotifier implements ApplicationListener{
- //该方法会在容器发生事件时触发
- public void onApplicationEvent(ApplicationEvent event) {
- if(event instanceof EmailEvent){
- //只处理EmailEvent,发送email通知
- EmailEvent emailEvent = (EmailEvent) event;
- System.out.println("需要发送邮件的接收地址为:"+emailEvent.getAddress());
- System.out.println("需要发送邮件的邮件正文是:"+emailEvent.getText());
- }
- else {
- //容器内置事件不作任何处理
- System.out.println("容器本身的事件:"+event);
- }
- }
- }
public class EmailNotifier implements ApplicationListener{ //该方法会在容器发生事件时触发
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof EmailEvent){
//只处理EmailEvent,发送email通知
EmailEvent emailEvent = (EmailEvent) event;
System.out.println("需要发送邮件的接收地址为:"+emailEvent.getAddress()); System.out.println("需要发送邮件的邮件正文是:"+emailEvent.getText());
}
else {
//容器内置事件不作任何处理
System.out.println("容器本身的事件:"+event);
}
} }
3)、将监听器类配置在容器中。
在为Spring容器注册监听器时,我们只需在Spring配置文件中配置一个实现了ApplicationListener的Bean即可,Spring容器会把这个Bean当做容器事件的监听器。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <!-- 配置监听器 -->
- <bean class="com.app.listener.EmailNotifier"/>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 配置监听器 -->
<bean class="com.app.listener.EmailNotifier"/> </beans>
通过上面的3个步骤就可以实现Spring容器的事件了。当系统创建Spring容器,加载Spring容器时会自动触发容器事件,容器事件监听器可以监听到这些事件。同时我们也可以调用ApplicationContext的pulishEvent()方法来主动触发容器事件。
- public class SpringTest {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
- //创建一个ApplicationEvent对象
- EmailEvent emailEvent = new EmailEvent("hell","spring_test@163.com","this is a test");
- //主动触发容器事件
- ctx.publishEvent(emailEvent);
- }
- }
public class SpringTest { public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
//创建一个ApplicationEvent对象
EmailEvent emailEvent = new EmailEvent("hell","spring_test@163.com","this is a test"); //主动触发容器事件
ctx.publishEvent(emailEvent);
} }
如果Bean想发布事件,则Bean必须获得其容器的引用。如果程序中没有直接获取容器的引用,则应该让Bean实现ApplicationContextAware或BeanFactoryAware接口,从而获得容器的引用。
除了我们可以自己实现Spring容器的事件外,Spring也提供了几个内置事件:
1、 ContextRefreshedEvent:ApplicationContext容器初始化或者刷新时触发该事件。
2、 ContextStartedEvent:当使用ConfigurableApplicationContext接口的start()方法启动ApplicationContext容器时触发该事件。
3、 ContextClosedEvent:当使用ConfigurableApplicationContext接口的close()方法关闭ApplicationContext容器时触发该事件。
4、 ContextStopedEvent: 当使用ConfigurableApplicationContext接口的stop()方法停止ApplicationContext容器时触发该事件。
读李刚《轻量级Java EE企业应用实战》
(转)Spring读书笔记-----使用Spring容器(二)的更多相关文章
- Spring 读书笔记-----使用Spring容器(一)
pring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spri ...
- (转)Spring 读书笔记-----使用Spring容器(一)
Spring有两个核心接口:BeanFactory和ApplicationContext,其中ApplicationContext是BeanFactory的子接口.他们都可代表Spring容器,Spr ...
- Spring读书笔记-----使用Spring容器(二)
一.使用ApplicationContext 前面介绍了,我们一般不会使用BeanFactory实例作为Spring容器,而是使用ApplicationContext实例作为容器,它增强了BeanFa ...
- Spring读书笔记——bean创建(上)
通过<Spring读书笔记--bean加载>和<Spring读书笔记--bean解析>,我们明白了两件事. Spring如何加载消化一个xml配置文件 Spring如何将xml ...
- Spring读书笔记——bean创建(下)
有关Spring加载bean系列,今天这是最后一篇了,主要接上篇对于从Spring容器中获取Bean的一些细节实现的补充. <Spring读书笔记--bean加载>--Spring如何加载 ...
- (转) Spring读书笔记-----Spring的Bean之配置依赖
前一篇博客介绍了Spring中的Bean的基本概念和作用域(Spring读书笔记-----Spring的Bean之Bean的基本概念),现在介绍Spring Bean的基本配置. 从开始我们知道Jav ...
- Spring读书笔记——bean解析
前情回顾 上篇<Spring读书笔记--bean加载>我们从代码角度介绍了有哪些类负责解析XML文件,又是如何一步步从XML格式脱变成我们熟悉的bean的,直到DefaultBeanDef ...
- Spring读书笔记-----Spring的Bean之Bean的基本概念
从前面我们知道Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...
- (转)Spring读书笔记-----Spring的Bean之Bean的基本概念
从前面我们知道Spring其实就是一个大型的工厂,而Spring容器中的Bean就是该工厂的产品.对于Spring容器能够生产那些产品,则取决于配置文件中配置. 对于我们而言,我们使用Spring框架 ...
随机推荐
- Oracle的常见错误及解决办法
ORA-12528: TNS:listener: all appropriate instances are blocking new connections ORA-12528问题是因为监听中的服务 ...
- 深入浅出 - Android系统移植与平台开发(十一)- Android系统的定制(瘋耔修改篇一)
首先非常感谢原文作者为我们提供的知识库,因为有你们的贡献,我们的开发难度更显简单 原文 : http://blog.csdn.net/mr_raptor/article/details/30113 ...
- Unreachable catch block for IOException. This exception is never thrown from the try statement body
Unreachable catch block for IOException. This exception is never thrown from the try statement body ...
- c#4.0新特性之协变与逆变
1.C#3.0以前的协变与逆变 如果你是第一次听说这个两个词,别担心,他们其实很常见.C#4.0中的协变与逆变[1](Covariance and contravariance)有了进一步的完善,主要 ...
- 【转】win7 虚拟机virtualbox中ubuntu12.04安装samba实现文件共享
原文网址:http://blog.csdn.net/watkinsong/article/details/8878786 昨天心血来潮,又装了个虚拟机,然后安装了ubuntu12.04,为了实现在虚拟 ...
- (转载)1248 - Every derived table must have its own alias
(转载)http://hi.baidu.com/lylegend13/item/a79f17eb51f5dff7e0a5d43b 1. select count(distinct CName) fro ...
- dev中gridview控件 z
目录:一.客户端常用1.常用API2.聚焦行变更事件3.客户端选择多行4.客户端选择行5. 获取选择的行数目6.单击行时,选中行7.通过checkbox 选择行8.选择所有行9.启动编辑框,Conta ...
- HDU-4300 Clairewd’s message
http://acm.hdu.edu.cn/showproblem.php?pid=4300 很难懂题意.... Clairewd’s message Time Limit: 2000/1000 MS ...
- jQuery获取属性之自己遇到的问题
刚开始是这种写法 用的 attr 结果获取不到 if($("#reg_username_span").attr("display") == 'block') ...
- 零基础学习视频解码之FFMpeg中比较重要的函数以及数据结构
http://www.cnblogs.com/tanlon/p/3879081.html 在正式开始解码练习前先了解下关于FFmpeg中比较重要的函数以及数据结构. 1. 数据结构: (1) AVF ...