ApplicationContext覆盖了BeanFactory的所有功能,并提供了更多的特,容器创建时就创建了singleton Bean

相对BeanFactory而言,ApplicationContext提供了以下扩展功能:

1.国际化支持:继承MessageSource接口,提供国际化支持

2.资源访问:支持对文件和URL的访问。

3.事件传播:事件传播特性为系统中状态改变时的检测提供了良好支持。

4.多实例加载:可以在同一个应用中加载多个Context实例,即加多个配置文件。

  • 国际化处理的步骤

1)写相应的国家资源文件如:ApplicationResources_zh.properties,注意字符的转化类似struts的国际化

2)bean.xml 的配置


<p:bean id="messageSource " class="org.springframework.context.support.ResourceBundleMessageSource"> <p:property name="basename" value="com.kettas.spring.ioc.day3.ApplicationResource" /> </p:bean> <p:bean id="msc" class="com.kettas.spring.ioc.day3.MessageSourceConsumer" /> </p:beans>

3)实现类MessageSourceConsumer

具体的实现类implements MessageSourceAware。注入messageSource ms得到字符:String name = ms.getMessage("name", null, Locale.CHINA);name是资源文件的key值

Locale.CHINA是中文,Locale.ENGLISH英文

  • 资源访问:即外部资源文件的获取;资源文件

两种引入外部资源的方式:


<!-- <p:bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <p:property name="location" value="com/kettas/spring/ioc/day3/jdbc.properties"></p:property> </p:bean> -->

②,引入解析:


xmlns:context="http://www.springframework.org/schema/context" <context:property-placeholder location="com/kettas/spring/ioc/day3/jdbc.properties"/>

使用

jdbc.driver是资源的key值

其它:ApplicationContext.getResource方法提供了对资源文件访问支持,如:


Resource rs = ctx.getResource("classpath:config.properties"); File file = rs.getFile();
  • 事件传播:事件机制是观察者模式的实现

事件框架两个重要的成员:

1)ApplicationEvent:容器事件,必须由ApplicationContext发布。

2)ApplicationListener:监听器:可有其他任何监听器bean担任。

3)ApplicationContext是事件源必须由java程序显示的触发。

1)事件流程:

2)代码实例:

1,事件源。


public class LogEventSource implements ApplicationEventPublisherAware { private ApplicationEventPublisher publisher; public void setApplicationEventPublisher(ApplicationEventPublisher publisher){ this.publisher = publisher; } public void fireEvent(){ LogEvent evt = new LogEvent(this, "Test message"); publisher.publishEvent(evt); } }

2,事件监听,


public class Logger implements ApplicationListener { private Log logger = LogFactory.getLog(Logger.class); public void onApplicationEvent( ApplicationEvent evt) { logger.info("Event type: " + evt.getClass().getName()); if(evt instanceof LogEvent){ logger.info(((LogEvent)evt).getMessage()); } } }

3)配置文件


<p:bean id="les" class="com.kettas.spring.ioc.day3.LogEventSource"> 有相应的事件方法 </p:bean> <p:bean class="com.kettas.spring.ioc.day3.Logger"></p:bean> 处理事件的后台 </p:beans>

说明:LogEventSourc有相应的事件方法publisher.publishEvent(evt)主动触发容器事件; Logge处理事件的后台

  • 多实例加载

BeanPostProcessor bean后处理器 实现BeanPostProcessor接口 对bean实例进一步增加功能,实现两个方法processBeforeInitialization(Object bean,String name)方法(该方法的第一个参数是将进行后处理的实例bean,name是该bean的名字)和ProcessaAfterInitialization(Object bean,String name).在init()或destory之前做一些处理.Spring的工具类就是通过bean的后处理器完成的。

BeanFactoryPostProcessor 容器后处理器:实现接口BeanFactoryPostProcessor只负责处理容器本身 ,实现的方法是postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)参加资源的引入和获取,可以修改bean工厂bean的定义相当一个再配置的过程。类似BeanPostProcessor,ApplicationContext可以自动检测到容器中的容器后处理器,但是BeanFacory必须手动调用。

使用ApplicationContext的更多相关文章

  1. Spring获取ApplicationContext

    在Spring+Struts+Hibernate中,有时需要使用到Spring上下文.项目启动时,会自动根据applicationContext配置文件初始化上下文,可以使用ApplicationCo ...

  2. spring applicationContext.xml和hibernate.cfg.xml设置

    applicationContext.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans ...

  3. spring源码:ApplicationContext的增强功能(li)

    ApplicationContext作为资源加载器:ApplicationContext作为事件发布者: Java原生提供了事件发布机制------EventObject对象作为发布的事件,Event ...

  4. 为什么applicationContext.xml和spring-servlet.xml中都有注解过滤<context:component-scan base-package="myproject"> 和<context:component-scan base-package="myproject.controller" />

    在刚学习SpringMVC框架整合时,你也许会产生疑问为什么Spring.xml和SpringMVC.xml中都有注解过滤. <context:component-scan base-packa ...

  5. Spring:ApplicationContext (2)

    在使用Spring时,通常会配置一个applictioncontext.xml 来指定ApplicationContext的相关信息. 当使用SpringMVC时,还会再另外指定一个[server-n ...

  6. Spring ApplicationContext 简解

    ApplicationContext是对BeanFactory的扩展,实现BeanFactory的所有功能,并添加了事件传播,国际化,资源文件处理等.   configure locations:(C ...

  7. [Java] ApplicationContext 辅助类

    我们经常需要获取各种 bean , 需要用到 context. 下面的类可以方便的使用 context , 获取 bean 等. import java.io.File; import java.ut ...

  8. 项目中运行报错: Loading XML bean definitions from class path resource [applicationContext.xml]

    记录一下: org.springframework.context.support.AbstractApplicationContext prepareRefresh Refreshing org.s ...

  9. web.xml配置错误导致applicationContext.xml配置重复加载

    web.xml相关配置 <context-param><param-name>log4jRefreshInterval</param-name><param- ...

  10. 手动获取spring的ApplicationContext和bean对象

    WEB项目: 方法1: 1 ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(S ...

随机推荐

  1. python学习笔记(十二)之函数

    牛刀小试: 定义一个无参函数 >>> def myFirstFunc(): ... print("Hello python") ... print("h ...

  2. vue-loader 调用了cssLoaders方法配置了css加载器属性。

    module: { loaders: [ // 这里也是相应的配置,test就是匹配文件,loader是加载器, { test: /\.vue$/, loader: 'vue' }, { test: ...

  3. 使用vscode实现git同步

    用了git最方便的就是项目同步管理,回到家打开vscode只需要点击一下pull就能全部同步过来.是不是很方便....毕竟之前我都是拿u盘拷贝回家或者存到云盘再下载下来..   我这里之前用的是国内的 ...

  4. D - Binary Lexicographic Sequence URAL - 1081 (贪心)

    题目链接:https://cn.vjudge.net/contest/275079#problem/D 具体思路:首先,我们可以观察到1-n位数的种数连起来是一个很有规律的数列,然后我们开始倒序建立. ...

  5. DNSLOG在渗透测试中的玩法儿

    首先了解一下DNS是啥??? DNS(Domain Name System,域名系统),万维网上作为域名和IP地址相互映射的一个分布式数据库,能够使用户更方便的访问互联网,而不用去记住能够被机器直接读 ...

  6. flask插件系列之SQLAlchemy基础使用

    sqlalchemy是一个操作关系型数据库的ORM工具.下面研究一下单独使用和其在flask框架中的使用方法. 直接使用sqlalchemy操作数据库 安装sqlalchemy pip install ...

  7. SPI协议及其工作原理浅析【转】

    转自:http://www.laoliu-soft.net/category/tech_chap/tech_linux/ 一.概述. SPI, Serial Perripheral Interface ...

  8. Mutex, semaphore, spinlock的深度解析

    Mutex是一把钥匙,一个人拿了就可进入一个房间,出来的时候把钥匙交给队列的第一个.一般的用法是用于串行化对critical section代码的访问,保证这段代码不会被并行的运行. Semaphor ...

  9. hdu 2852 KiKi's K-Number (线段树)

    版权声明:本文为博主原创文章,未经博主允许不得转载. hdu 2852 题意: 一个容器,三种操作: (1) 加入一个数 e (2) 删除一个数 e,如果不存在则输出 No Elment! (3) 查 ...

  10. 记一次spring boot中MongoDB Prematurely reached end of stream的异常解决

    在spring boot项目中使用了mongodb,当一段时间没有操作mongodb,下次操作mongodb时就会出现异常.异常如下: org.springframework.data.mongodb ...