深入理解Spring AOP实战(基于XML)
来源:互联网 发布:域名备案要钱吗 编辑:程序博客网 时间:2024/05/18 01:23
AOP是什么,有哪些概念?
参考: <深入理解Spring AOP实战(基于注解)>
1. XML中aop配置元素分析
在进行XML配置aop之前,需要了解spring的aop命名空间中,
提供了多少个元素用来在XML中声明切面。直接上图看看:
这些配置在以下xml中体会。
2. 定义切面
创建一个切面,该切面没有任何的注解。
package com.lanhuigu.spring;/** * 切面类 */public class Person { /** * 开会之前--找个位置坐下 */ public void takeSeats() { System.out.println("找位置坐"); } /** * 开会之前--手机调成静音 */ public void silenceCellPhones() { System.out.println("手机调成静音"); } /** * 开会之后--写会议总结报告 */ public void summary() { System.out.println("写会议总结报告"); }}
3. 创建目标类
接口:
package com.lanhuigu.spring;public interface ConferenceService { void conference();}
实现:
package com.lanhuigu.spring;public class ConferenceServiceImpl implements ConferenceService { @Override public void conference() { System.out.println("开会......"); }}
4. XML中声明切面
spring的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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 启用AspectJ自动代理 --> <aop:aspectj-autoproxy proxy-target-class="true"/> <!-- bean定义 --> <bean id="conferenceService" class="com.lanhuigu.spring.ConferenceServiceImpl"/> <!-- 切面类 --> <bean id="person" class="com.lanhuigu.spring.Person"/> <!-- 在<aop:config>中我们可以声明一个活多个通知器、切面或者切点。 --> <aop:config> <aop:aspect ref="person"> <!-- 前置通知 --> <aop:before pointcut="execution(* com.lanhuigu.spring.ConferenceServiceImpl.conference(..))" method="takeSeats" /> <!-- 前置通知 --> <aop:before pointcut="execution(* com.lanhuigu.spring.ConferenceServiceImpl.conference(..))" method="silenceCellPhones" /> <!-- 后置通知 --> <aop:after pointcut="execution(* com.lanhuigu.spring.ConferenceServiceImpl.conference(..))" method="summary" /> </aop:aspect> </aop:config> <!-- 在上面的配置中,切入点的定义是一样的,我们看到execution中的内容都是一样, 我们可以使用类似@Pointcut注解方式的元素<aop:pointcut>将其提出为公用部分, 而使用切点表达式的地方通过pointcut-ref引入。优化之后效果如下。 --> <!--<aop:config> <aop:aspect ref="person"> <!– 定义切点 –> <aop:pointcut id="conference" expression="execution(* com.lanhuigu.spring.ConferenceServiceImpl.conference(..))"/> <!– 前置通知 –> <aop:before pointcut-ref="conference" method="takeSeats" /> <!– 前置通知 –> <aop:before pointcut-ref="conference" method="silenceCellPhones" /> <!– 后置通知 –> <aop:after pointcut-ref="conference" method="summary" /> </aop:aspect> </aop:config>--></beans>
5. 测试类
package com.lanhuigu.spring;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAopXml { @Test public void testAopXml() { // 根据配置文件创建spring容器 ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); // 从容器中获取Bean ConferenceServiceImpl conferenceService = (ConferenceServiceImpl)context.getBean("conferenceService"); // 调用Bean方法 conferenceService.conference(); }}
6. 运行结果
找位置坐
手机调成静音
开会......
写会议总结报告
手机调成静音
开会......
写会议总结报告
阅读全文
0 0
- 深入理解Spring AOP实战(基于XML)
- 深入理解Spring AOP实战(基于注解)
- 深入理解Spring AOP
- Spring - AOP基于XML
- Spring框架学习-深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- 深入理解Spring AOP代理
- Spring AOP - 基于XML配置文件
- Spring基于XML配置AOP
- Spring4深入理解AOP02----AOP简介,AspectJ,AOP基于注解和XML配置(5种通知,切面优先级)
- 深入理解Spring 之 源码剖析AOP(XML配置方式)
- 深入理解Spring AOP之基本概念
- 深入理解Spring AOP之基本概念
- Spring学习总结7(AOP-基于XML)
- 基于XML的spring AOP配置
- Spring AOP---基于Xml配置应用demo
- Spring的AOP配置(基于xml)
- (一)Spring AOP:基于XML配置文件
- Spring -- 基于XML的AOP通知配置
- Oracle 12c报错:ORA-01078和LRM-00109的解决办法
- GAN系列学习(1)——前生今世
- 组织项目管理成熟度模型OPM3
- MySQL半同步--after_flush
- 二进制位从左到右翻转后的值
- 深入理解Spring AOP实战(基于XML)
- JDK动态代理探索
- Ubuntu 开机出现 grub rescue> 终端模式修复方法
- 了解全宇宙最神秘的团体,只需要这九张图
- vs2010MFC框架下,点击按钮弹出窗口,在子窗口中输入值,将这个值向主窗口传递
- win8--L2TP教程
- 12.14题解
- 单例模式中的饿汉式和懒汉式的区别?
- python中defaultdict方法的使用