第03章 控制反转(Spring IoC)

Similar documents
IoC容器和Dependency Injection模式.doc

Microsoft Word - Hibernate与Struts2和Spring组合指导.doc

EJB-Programming-3.PDF

EJB-Programming-4-cn.doc

untitled

(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

基于CDIO一体化理念的课程教学大纲设计

untitled

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

设计模式 Design Patterns

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

untitled

本章学习目标 小风 Java 实战系列教程 SpringMVC 简介 SpringMVC 的入门案例 SpringMVC 流程分析 配置注解映射器和适配器 注解的使用 使用不同方式的跳转页面 1. SpringMVC 简介 Spring web mvc

D C 93 2

chp6.ppt

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

Java应用框架Spring实验指导书.doc

Chapter 9: Objects and Classes

JBuilder Weblogic

优迈科技教学大纲2009版本

基于ECO的UML模型驱动的数据库应用开发1.doc

《大话设计模式》第一章

Learning Java

2 Java 语 言 程 序 设 计 教 程 简 单 性 Java 语 言 的 语 法 与 C 语 言 和 C++ 语 言 很 接 近, 使 得 大 多 数 程 序 员 很 容 易 学 习 和 使 用 Java 另 一 方 面,Java 丢 弃 了 C++ 中 很 少 使 用 的 很 难

没 有 多 余 的 Contruol 或 Action 了 原 来 Domain 层 被 服 务 层 Service layer 遮 挡, 在 右 边 图 中, 则 Domain 层 直 接 暴 露 给 前 台 了, 没 有 被 遮 挡, 裸 露 了 这 样 一 步 到 位 实 现 领 域 模 型

2. AOP 底层技术实现 小风 Java 实战系列教程 关键词 : 代理模式 代理模型分为两种 : 1) 接口代理 (JDK 动态代理 ) 2) 子类代理 (Cglib 子类代理 ) 需求 :CustomerService 业务类, 有 save,update 方法, 希望在 save,updat

Java 1 Java String Date

FAQ -PowerDesigner9.5.DOC

Microsoft Word - 01.DOC


使用 XFire 与 Spring 开发 Web Service 2 实现功能与特点 基于 J2EE 平台的 Web Service 服务 开发方便, 配置简单 设计接口 实现服务 配置暴露接口 XFire 将自动生成对应的 wsdl 支持高级详细配置 与 Spring 无缝集成 运行环境 JDK

内 容 提 要 将 JAVA 开 发 环 境 迁 移 到 Linux 系 统 上 是 现 在 很 多 公 司 的 现 实 想 法, 而 在 Linux 上 配 置 JAVA 开 发 环 境 是 步 入 Linux 下 JAVA 程 序 开 发 的 第 一 步, 本 文 图 文 并 茂 地 全 程 指

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

Servlet

Chapter 9: Objects and Classes

jsp

Java

ebook

JavaIO.PDF

untitled

Microsoft Word - Learn Objective-C.doc

untitled

OSWorkflow Documentation

FileMaker 16 ODBC 和 JDBC 指南

mvc

Microsoft Word - 物件導向編程精要.doc

Microsoft PowerPoint - 09Future

Swing-02.pdf

new 进行创建对象, 是程序主动去创建依赖对象 ; 而 IoC 是有专门一个容器来创建这些对象, 即由 Ioc 容器来控制对象的创建 ; 谁控制谁? 当然是 IoC 容器控制了对象 ; 控制什么? 那就是主要控制了外部资源获取 ( 不只是对象包括比如文件等 ) 为何是反转, 哪些方面反转了 : 有

1: public class MyOutputStream implements AutoCloseable { 3: public void close() throws IOException { 4: throw new IOException(); 5: } 6:

SDK 概要 使用 Maven 的用户可以从 Maven 库中搜索 "odps-sdk" 获取不同版本的 Java SDK: 包名 odps-sdk-core odps-sdk-commons odps-sdk-udf odps-sdk-mapred odps-sdk-graph 描述 ODPS 基

untitled

输入 project name 选择完成

J2ME ISBN J2ME MIDP MIDP 2.0 API J2ME Netbeans IDE 4.1 Mobility Pack 4.1 MIDlet MIDlet MIDP PUSH API PDA API Ja

RunPC2_.doc

(Microsoft Word - \272\364\263q\245|\244A_ _\304\254\253\330\336\263__\272\353\302\262\263\370\247i.doc)

RUN_PC連載_12_.doc

使 用 Java 语 言 模 拟 保 险 箱 容 量 门 板 厚 度 箱 体 厚 度 属 性 锁 具 类 型 开 保 险 箱 关 保 险 箱 动 作 存 取 款

内 容 简 介 本 书 是 一 本 关 于 语 言 程 序 设 计 的 教 材, 涵 盖 了 语 言 的 基 本 语 法 和 编 程 技 术, 其 中 包 含 了 作 者 对 语 言 多 年 开 发 经 验 的 总 结, 目 的 是 让 初 学 的 读 者 感 受 到 语 言 的 魅 力, 并 掌

1. 2. Flex Adobe 3.

Microsoft Word - 王彬_已修改_.doc

untitled

使用MapReduce读取XML文件

Microsoft Word - ch04三校.doc

序号:001

J2EE MVC with Webwork2 Xwork, to J2EE MVC with Webwork2 Xwork

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

4.1 AMI MQSeries API AMI MQI AMI / / AMI JavaC C++ AMI / AMI AMI - / /

untitled

epub83-1

目 录 1. 业 务 流 程 系 统 开 发 面 临 的 挑 战 与 机 遇 业 务 流 程 管 理 新 一 代 开 源 业 务 流 程 开 发 平 台 BPMX BPMX3 是 什 么 为 什 么 要 优 先 采 用 BPMX

软 件 工 程 专 业 习 指 南 目 录 一 软 件 工 程 专 业 设 置 背 景 与 发 展 前 景... 3 二 软 件 工 程 专 业 实 践 教 条 件... 4 三 软 件 工 程 专 业 课 程 类 型 及 核 方 式 软 件 工 程 专 业 课 程 类 型...7

untitled

在Spring中使用Kafka:Producer篇

WebSphere Studio Application Developer IBM Portal Toolkit... 2/21 1. WebSphere Portal Portal WebSphere Application Server stopserver.bat -configfile..

北 风 网 讲 师 原 创 作 品 ---- 仅 供 学 员 内 部 交 流 使 用 前 言 吾 尝 终 日 而 思 矣, 不 如 须 臾 之 所 学 也 ; 吾 尝 跂 而 望 矣, 不 如 登 高 之 博 见 也 登 高 而 招, 臂 非 加 长 也, 而 见

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

FileMaker 15 ODBC 和 JDBC 指南

Eclipse 插件之Spring IDE

<4D F736F F F696E74202D20332D322E432B2BC3E6CFF2B6D4CFF3B3CCD0F2C9E8BCC6A1AAD6D8D4D8A1A2BCCCB3D0A1A2B6E0CCACBACDBEDBBACF2E707074>

untitled

Microsoft Word - 2CA13內文.doc

View:Struts 提 供 了 action form 创 建 form bean, 用 于 在 controller 和 view 间 传 输 数 据 此 外,Struts 提 供 了 自 定 义 JSP 标 签 库, 辅 助 开 发 者 用 JSP 创 建 交 互 式 的 以 表 单 为 基

附录J:Eclipse教程

北京北信源软件股份有限公司招股书(申报稿)

Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

weblogic

1

javaexample-02.pdf

untitled

全国计算机技术与软件专业技术资格(水平)考试

2009年3月全国计算机等级考试二级Java语言程序设计笔试试题

D getinitparameternames() 9 下 列 选 项 中, 属 于 Servlet API 中 提 供 的 request 对 象 的 包 装 类 的 是 ( ) A HttpServletRequestWrapper B HttpServletRequest C HttpServ

计 算 机 系 统 应 用 年 第 25 卷 第 4 期 线 程 的 复 用 [2,3]. 通 常 情 况 下, 服 务 器 端 程 序 在 启 动 时 创 建 若 干 数 量 的 线 程 对 象 并 缓 存 起 来, 此 时 它 们 处 于

<4D F736F F D20BBF9D3DA416E64726F6964C6BDCCA8B5C4B5E7D7D3C5C4C2F4CFB5CDB32E646F63>

59 1 CSpace 2 CSpace CSpace URL CSpace 1 CSpace URL 2 Lucene 3 ID 4 ID Web 1. 2 CSpace LireSolr 3 LireSolr 3 Web LireSolr ID

Stateless Session Beans(无状态bean)的学习

目 錄 版 次 變 更 記 錄... 2 原 始 程 式 碼 類 型 之 使 用 手 冊... 3 一 安 裝 軟 體 套 件 事 前 準 備... 3 二 編 譯 流 程 說 明

epub 61-2

Transcription:

3 Spring IoC GoF Design Patterns: Elements of Reusable Object-Oriented Software Programming to an Interface not an Implementation Java Java Java GoF Service Locator IoC IoC Spring IoC 3.1 IoC IoC IoC Dependency Injection 1 IoC Hollywood Don t call me, I ll call you 2 IoC IoC IoC IoC IoC IoC 1 IoC IoC IoC JNDI IoC 1 Robert C. Martin The Dependency Inversion Principle http://www.objectmentor.com/resources/articles/dip.pdf 2 Rod Johnson TheServerSide.com Introducing the Spring Framework http://www.theserverside.com/articles/article.tss?l=springframework

Spring IoC IoC API IoC IoC Spring PicoContainer Apache HiveMind Spring IoC Java/J2EE Spring AOP Spring MVC Framework O/R Mapping DAO Spring Acegi Open Source Spring IoC 3.2 Spring IoC Spring IoC IoC Spring IoC Spring IoC Spring IoC BeanFactory org.springframework.beans.factory XML JavaBean Java Applet ApplicationContext org.springframework.context ApplicationContext BeanFactory BeanFactory Spring IoC J2EE Spring BeanFactory ApplicationContext 3.2.1 BeanFactory Spring BeanFactory Spring Applet Swing/SWT BeanFactory EJB Spring BeanFactory BeanFactory Spring IoC BeanFactory BeanFactory JavaBean BeanFactory IoC IoC BeanFactory Spring JavaBean 30

3 Spring IoC Spring org.springframework.beans.factory.beanfactory XmlBeanFactory 3-1 BeanFactory 3-1 BeanFactory BeanFactory XmlBeanFactory example4 appcontext.xml HelloWorldClient appcontext.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean name="filehelloworld" class="com.openv.spring.helloworld"> <constructor-arg> <ref bean="filehello"/> </constructor-arg> <bean name="filehello" class="com.openv.spring.filehellostr"> <constructor-arg> <value>helloworld.properties</value> </constructor-arg> </beans> Spring appcontext.xml spring-beans.dtd spring-beans.dtd JavaBean BeanFactory Spring BeanFactory Web B spring-beans.dtd 31

Spring example4 3-2 UML 3-2 example4 UML HelloWorldClient.java 1 public class HelloWorldClient { protected static final Log log = LogFactory.getLog(HelloWorldClient.class); public HelloWorldClient(){ Resource resource = new ClassPathResource("appcontext.xml"); BeanFactory factory = new XmlBeanFactory(resource); HelloWorld hw = (HelloWorld)factory.getBean("fileHelloWorld"); log.info(hw.getcontent()); public static void main(string[] args) { new HelloWorldClient(); Spring appcontext.xml XmlBeanFactory appcontext.xml Spring org.springframework.core.io appcontext.xml try { InputStream ins = new FileInputStream("src/appcontext.xml"); BeanFactory factory = new XmlBeanFactory(ins); HelloWorld hw = (HelloWorld) factory.getbean("filehelloworld"); log.info(hw.getcontent()); catch (FileNotFoundException ex) { log.error(" appcontext.xml ", ex); ApplicationContext appcontext.xml ClassPathXmlApplicationContext appcontext = new ClassPathXmlApplicationContext( new String[] { "appcontext.xml" ); BeanFactory factory = (BeanFactory) appcontext; HelloWorld hw = (HelloWorld) factory.getbean("filehelloworld"); log.info(hw.getcontent()); BeanFactory Spring 32

3 Spring IoC Spring Web J2EE Web Spring ApplicationContext appcontext.xml JavaBean BeanFactory getbean JavaBean JavaBean example4 BeanFactory 1 Bean BeanFactory Bean IoC BeanFactory JavaBean IoC Spring JavaBean Spring BeanDefinition Bean IoC JavaBean 4 JavaBean JavaBean IoC JavaBean Spring JavaBean IoC JavaBean 3-3 JavaBean 3-3 BeanFactory Bean 3 3 Spring Live 33

Spring Spring DTD B spring-beans.dtd 2 Bean JavaBean 3-3 JavaBean JavaBean <bean name="filehelloworld" class="com.openv.spring.helloworld"> HelloWorld JavaBean <bean name="filehelloworld" class="com.openv.spring.helloworld" factory-method= createhelloworldinstance > HelloWorld createhelloworldinstance <bean name= helloworldfactory class= com.openv.spring.helloworldfactory /> <bean name="filehelloworld" factory-bean= helloworldfactory factory-method= createhelloworldinstance > filehelloworld JavaBean HelloWorldFactory createhelloworldinstance Spring JavaBean example4 Spring <bean name="filehelloworld" class="com.openv.spring.helloworld"> <constructor-arg> <ref bean="filehello"/> </constructor-arg> <ref> Spring filehello JavaBean Spring DTD <ref> bean Spring XML BeanFactory ApplicationContext JavaBean local Spring XML JavaBean Spring XML Spring IDE JavaBean local XML parent JavaBean 34

3 Spring IoC 3 JavaBean 3-3 JavaBean <bean> autowire Spring autowire Spring JavaBean JavaBean B <bean> dependency-check Spring JavaBean B setter JavaBean Java primitive Spring JavaBean null <bean id="example11service" class="org.springframework.transaction.interceptor.transactionproxyfa ctorybean"> <property name="transactionmanager"> <ref local="transactionmanager"/> <property name="target"> <ref local="example11servicetarget"/> <property name="transactionattributes"> <props> <prop key="get*"> PROPAGATION_REQUIRED,readOnly </prop> <prop key="set*"> PROPAGATION_REQUIRED </prop> </props> Spring TransactionProxyFactoryBean transactionmanager target transactionattributes transactionmanager <ref> transactionmanager POJO Spring transactionmanager <bean id="transactionmanager" class="org.springframework.transaction.jta.jtatransactionmanager"> <property name="usertransactionname"> <value>java:comp/usertransaction</value> 35

Spring <ref> JavaBean <property name="transactionmanager"> <bean class="org.springframework.transaction. jta.jtatransactionmanager"> <property name="usertransactionname"> <value>java:comp/usertransaction</value> JavaBean JavaBean JtaTransactionManager JavaBean setbeanfactory org.springframework.beans.factory. BeanFactoryAware void setbeanfactory(beanfactory beanfactory) throws BeansException; Spring BeanFactory JavaBean InitializingBean org.springframework.beans.factory.initializingbean BeanFactory JavaBean BeanFactory InitializingBean JavaBean void afterpropertiesset() throws Exception afterpropertiesset InitializingBean <bean> init-method InitializingBean <bean name="filehelloworld" class="com.openv.spring.helloworld" init-method= init > HelloWorld init 6 JavaBean JavaBean 4 JavaBean Spring JavaBean JavaBean getbean JavaBean 5 JavaBean Spring Web Spring JavaBean 36

3 Spring IoC DisposableBean JavaBean Spring destroy-method JavaBean Spring JavaBean singleton JavaBean prototype JavaBean Spring JavaBean JavaBean Spring JavaBean DisposableBean org.springframework.beans.factory.disposablebean BeanFactory JavaBean BeanFactory DisposableBean JavaBean void destroy() throws Exception destroy DisposableBean <bean> destroy-method DisposableBean <bean name="filehelloworld" class="com.openv.spring.helloworld" destroy-method= destroy > HelloWorld destroy Spring DisposableBean destroy destroy-method 6 Bean Bean <bean> <bean> OO Spring Bean Bean <bean> Tapestry Bean Bean Bean Spring JPetStore 7 PropertyPlaceholderConfigurer PropertyOverrideConfigurer PropertyPlaceholderConfigurer Spring Java LDAP Apache DBCP <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> <property name="driverclassname"> <value>${jdbc.driverclassname</value> <property name="url"> <value>${jdbc.url</value> 37

Spring <property name="username"> <value>${jdbc.username</value> <property name="password"> <value>${jdbc.password</value> ${jdbc.driverclassname ${jdbc.url ${jdbc.username ${jdbc.password Java <bean id="propertyconfigurer" class="org.springframework.beans.factory.config. PropertyPlaceholderConfigurer"> <property name="location"> <value>jdbc.properties</value> jdbc.properties jdbc.driverclassname=com.mysql.jdbc.driver jdbc.url=jdbc:mysql://localhost/example jdbc.username=sa jdbc.password= PropertyOverrideConfigurer Spring JavaDoc 8 BeanFactoryAware BeanNameAware Spring BeanFactory JavaBean JavaBean BeanFactory JavaBean JavaBean BeanFactoryAware <bean id="datasource" class="org.springframework.jndi.jndiobjectfactorybean"> <property name="jndiname"> <value>java:/mysqlds</value> <bean id="example11servicetarget" class="com.openv.spring.service.impl.example11managerimpl"> <property name="userinfo"> <ref local="userinfodao"/> example11servicetarget datasource Spring 38

3 Spring IoC example11servicetarget datasource BeanFactoryAware Example11ManagerImpl BeanFactoryAware Java BeanFactoryAware JavaBean BeanNameAware org.springframework.beans.factory.beannameaware void setbeanname(string name); InitializingBean afterpropertiesset init-method JavaBean 3.2.2 ApplicationContext BeanFactory JavaBean BeanFactory BeanFactory Spring ApplicationContext ApplicationContext org.springframework.web.context.contextloaderservlet org.springframework.web.context. ContextLoaderListener Web ApplicationContext Spring BeanFactory getbean() JavaBean getbean() JavaBean BeanFactory BeanFactory Spring ApplicationContext ContextLoaderServlet ContextLoaderListener JavaBean ApplicationContext ContextLoaderServlet ContextLoaderListener 3-4 3-5 Log4jConfigServlet Spring ContextLoaderServlet <context-param> <param-name>log4jconfiglocation</param-name> <param-value>/web-inf/classes/log4j.properties</param-value> </context-param> <servlet> <servlet-name>log4jconofigservlet</servlet-name> 39

Spring <servlet-class> org.springframework.web.util.log4jconfigservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> Spring Web MVC DispatcherServlet 3-4 ContextLoaderServlet 3-5 ContextLoaderListener Log4jConfigListener Spring web.xml <context-param> <param-name>log4jconfiglocation</param-name> 40

3 Spring IoC <param-value>/web-inf/classes/log4j.properties</param-value> </context-param> <listener> <listener-class> org.springframework.web.util.log4jconfiglistener </listener-class> </listener> ApplicationContext BeanFactory J2EE ApplicationContext BeanFactory ApplicationContext 1 Web ApplicationContext BeanFactory ApplicationContext ContextLoaderServlet ContextLoaderListener ApplicationContext ApplicationContext Servlet 2.4 Web ContextLoaderServlet ContextLoaderListener Web ContextLoaderListener example11 <?xml version="1.0"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">. <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/applicationcontext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.contextloaderlistener </listener-class> </listener> </web-app> Servlet 2.2 Web ContextLoaderServlet Servlet 2.3 Web ContextLoaderListener 41

Spring. <context-param> <param-name>contextconfiglocation</param-name> <param-value>/web-inf/applicationcontext.xml</param-value> </context-param> <servlet> <servlet-name>context</servlet-name> <servlet-class> org.springframework.web.context.contextloaderservlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> 2 ApplicationContextAware Spring Spring ApplicationContext JavaBean JavaBean ApplicationContext JavaBean JavaBean Spring JavaBean <ref> ApplicationContextAware org.springframework.context. ApplicationContextAware void setapplicationcontext(applicationcontext applicationcontext) throws BeansException; <bean id="datasource" class="org.springframework.jndi.jndiobjectfactorybean"> <property name="jndiname"> <value>java:/mysqlds</value> <bean id="example11servicetarget" class="com.openv.spring.service.impl.example11managerimpl"> <property name="userinfo"> <ref local="userinfodao"/> example11servicetarget datasource Spring XML example11servicetarget datasource ApplicationContextAware Example11ManagerImpl ApplicationContextAware Java ApplicationContextAware Example11ManagerImpl ApplicationContextAware 42

3 Spring IoC applicationcontext setter datasource JavaBean Spring ApplicationContext getbean() JavaBean id name import org.springframework.context.applicationcontext; import org.springframework.context.applicationcontextaware; public class Example11ManagerImpl implements ApplicationContextAware { private ApplicationContext applicationcontext; public void setapplicationcontext(applicationcontext applicationcontext){ this.applicationcontext = applicationcontext; public DataSource getdatasource(){ DataSource ds = null; ds = (DataSource)applicationContext.getBean("dataSource"); return ds; BeanFactoryAware 3 ApplicationContext ApplicationContext Spring Spring ApplicationContext 3 ClassPathXmlApplicationContext Web classpath WEB-INF/classes WEB-INF/lib jar Spring ClassPathXmlApplicationContext FileSystemXmlApplicationContext Spring FileSystemXmlApplicationContext XmlWebApplicationContext ContextLoaderListener ContextLoaderServlet Spring 3.3 IoC 3.3.1 发布并监听事件 Spring ApplicationContext BeanFactory 43

Spring Spring Spring org.springframework.context.event.applicationevent org.springframework.context.applicationlistener Observer ApplicationEvent ApplicationContext publishevent Spring ApplicationEvent 3-6 ApplicatinEvent ApplicationContext org.springframework.web.context.support.requesthandledevent Spring WebApplicationContext RequestHandledEvent org.springframework.context.event.contextrefreshedevent Spring ApplicationContext Spring ContextRefreshedEvent org.springframework.context.event.contextclosedevent Spring ApplicationContext Spring ContextRefreshedEvent 3-6 ApplicationEvent Spring ApplicationContext Spring ContextClosedEvent org.springframework.context.support. AbstractApplicationContext public void close() { if (logger.isinfoenabled()) { logger.info("closing application context [" + getdisplayname() + "]"); 44

3 Spring IoC // publish corresponding event publishevent(new ContextClosedEvent(this)); // Destroy all cached singletons in this context, // invoking DisposableBean.destroy and/or "destroy-method". ConfigurableListableBeanFactory beanfactory = getbeanfactory(); if (beanfactory!= null) { beanfactory.destroysingletons(); Spring RequestHandledEvent org.springframework.web.servlet. FrameworkServlet if (ispublishevents()) { // Whether or not we succeeded, publish an event. this.webapplicationcontext.publishevent(new RequestHandledEvent(this, request.getrequesturi(), processingtime, request.getremoteaddr(), request.getmethod(), getservletconfig().getservletname(), WebUtils.getSessionId(request), getusernameforrequest(request), failurecause)); ApplicationEvent JavaBean ApplicationListener public interface ApplicationListener extends EventListener { /** * Handle an application event. * @param event the event to respond to */ void onapplicationevent(applicationevent event); onapplicationevent Spring org.springframework.web.context.support.performancemonitorlistener PerformanceMonitorListener.java public class PerformanceMonitorListener implements ApplicationListener { protected final Log logger = LogFactory.getLog(getClass()); protected final ResponseTimeMonitorImpl responsetimemonitor = new ResponseTimeMonitorImpl(); public void onapplicationevent(applicationevent event) { if (event instanceof RequestHandledEvent) { 45

Spring RequestHandledEvent rhe = (RequestHandledEvent) event; // could use one monitor per URL this.responsetimemonitor.recordresponsetime(rhe.getprocessingtimemillis()); if (logger.isinfoenabled()) { // Stringifying objects is expensive. Don't do it unless it will // show. logger.info("performancemonitorlistener: last=[" + rhe.getprocessingtimemillis() + "ms]; " + this.responsetimemonitor + "; client=[" + rhe.getclientaddress() + "]"); ApplicationListener Spring ApplicationEvent instanceof 3.3.2 自定义 JavaBean 属性编辑器 Spring <property name="transactionattributes"> <props> <prop key="get*"> PROPAGATION_REQUIRED,readOnly </prop> <prop key="set*"> PROPAGATION_REQUIRED </prop> </props> TransactionProxyFactoryBean.java transactionattributes TransactionAttributeSource Spring TransactionAttributeSource JDK JavaBean JDK java.beans.propertyeditor JDK java.beans.propertyeditorsupport PropertyEditor Java TransactionAttributeEditor RuleBasedTransactionAttribute Spring 3-7 Spring FileEditor 46

3 Spring IoC java.io.file 3-7 Spring PropertyEditorSupport setastext() getastext() FileEditor org.springframework.beans.propertyeditors.fileeditor.java public class FileEditor extends PropertyEditorSupport { public void setastext(string text) throws IllegalArgumentException { if (StringUtils.hasText(text)) { setvalue(new File(text)); else { setvalue(null); 47

Spring public String getastext() { if (getvalue()!= null) { return ((File) getvalue()).getabsolutepath(); else { return ""; setastext text File getastext File IoC IoC Spring IoC J2EE IoC Spring Spring 3.4 Spring IoC IoC IoC Spring Spring IoC Spring AOP IoC+AOP J2EE Spring Spring IoC Spring AOP Java/J2EE Spring ApplicationContext Spring AOP 48