(转)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 DBMS_SESSION
Version 11.1 General Purpose Try dbms_session.reset_package. This call will reset all packages var ...
- Master Nginx(1) - Installing Nginx and Third-Party Modules
Installing NGINX and Third-Party Modules Installing Nginx using a package manager Linux(deb-based) s ...
- 把调试好的SQL语句转换为JAVA代码小工具
关键点:Pattern实现SQL拆解.ZeroClipboard.js实现复制到剪切板 主要代码: <%@ page language="java" import=" ...
- webdriver 操作 Firefox 在关闭浏览器时弹出 “Plugin Container for Firefox已停止工作” 处理办法。
上来先来一个图: 在使用webdriver操作Firefox浏览器的时候,一路顺畅.可是在最后关闭浏览器后,出现了如上面图示的警告!这个警告很早就出现了,因为也不影响测试结果,也就一直没理. 后来新搭 ...
- 直接下载adobe的完整安装包
在线安装从来没有成功过的. https://get.adobe.com/cn/reader/direct/
- add-apt-repository cloud-archive:liberty
apt-get update && apt-get upgrade;
- AppiumLibrary
Strategy Example Description identifier Click Element|identifier=my_element Matches by @id or @name ...
- String+,StringBuilder,String.format运行效率比较
实现String字符串相加的方法有很多,常见的有直接相加,StringBuilder.append和String.format,这三者的运行效率是有差异的,String是final类型的,每次相加都会 ...
- java 反射(reflect)总结,附对象打印工具类
java反射机制认知 java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法和属性:这种动态获取类的信息以及动态调用对象的方法的 ...
- Http(2)
1.传递的请求参数如何获取 GET方式: 参数放在URI后面 POST方式: 参数放在实体内容中 核心的API: request.getParameter("参数名"); 根据参 ...