Spring 一二事(9) - xml 形式的 AOP
AOP在spring中是非常重要的一个
在切面类中,有5种通知类型:
aop:before 前置通知
aop:after-returning 后置通知
aop:after 最终通知
aop:after-throwing 异常通知
aop:around 环绕通知
<bean id="personDAO" class="com.lee.spring002.aop.xml.PersonDAOImpl"></bean>
<bean id="transaction" class="com.lee.spring002.aop.xml.Transaction"></bean> <aop:config >
<!-- 切入点表达式,作用:确定目标类 -->
<!-- spring 会自动检测这个表达式下的类是否是切面,如果是,则不会包含进来 -->
<aop:pointcut expression="execution(* com.lee.spring002.aop.xml.PersonDAOImpl.*(..))" id="perform"/>
<!-- ref 指向切面 -->
<aop:aspect ref="transaction">
<!-- 前置通知 -->
<aop:before method="beginTransaction" pointcut-ref="perform"/> <!--
后置通知
可以获取目标方法的返回值(前置方法获取不到)
如果目标方法抛出异常,后置通知则不会继续执行
-->
<aop:after-returning method="commit" pointcut-ref="perform" returning="val"/> <!--
最终通知
无论目标方法是否抛出异常都将执行此方法
-->
<aop:after method="finallyDisplay" pointcut-ref="perform"/> <!--
异常通知
-->
<aop:after-throwing method="exception" pointcut-ref="perform" throwing="content"/> <!--
环绕通知
能控制目标方法能否执行
前置通知和后置通知能在目标方法的前后加代码,但是不能控制方法的执行
-->
<aop:around method="arround" pointcut-ref="perform"/>
</aop:aspect>
</aop:config>
关于切面的表达式简单说一下:
- 任意公共方法的执行:
execution(public * *(..))
- 任何一个名字以“set”开始的方法的执行:
execution(* set*(..))
AccountService接口定义的任意方法的执行:execution(* com.xyz.service.AccountService.*(..))
- 在service包中定义的任意方法的执行:
execution(* com.xyz.service.*.*(..))
- 在service包或其子包中定义的任意方法的执行:
execution(* com.xyz.service..*.*(..))
IPersonDAO.java
package com.lee.spring002.aop.xml;
public interface IPersonDAO {
public String savePerson();
}
PersonDAOImpl.java
package com.lee.spring002.aop.xml;
public class PersonDAOImpl implements IPersonDAO {
@Override
public String savePerson() {
System.out.println("PersonDAOImpl - savePerson()");
// int a = 1 / 0;
return "save successfully";
}
}
Transaction.java
package com.lee.spring002.aop.xml; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; /**
* 这是个切面
*
* @author leechenxiang
* @date 2016年1月12日
*
*/
public class Transaction { public void beginTransaction(JoinPoint jp) {
System.out.println("连接点名称: " + jp.getSignature().getName());
System.out.println("目标类名称: " + jp.getTarget().getClass());
System.out.println("Begin transaction...");
} /**
*
* @param jp
* @param val 目标方法返回值,要与returning对应
*/
public void commit(JoinPoint jp, Object val) {
System.out.println("Transaction commit..."); System.out.println("returning: " + val.toString());
} public void finallyDisplay() {
System.out.println("finally...");
} public void exception(JoinPoint jp, Throwable content) {
System.out.println("exception: " + content);
System.out.println("exception: " + content.getMessage());
} public void arround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("arround...");
pjp.proceed(); // 调用目标方法,如果这行不写,则目标方法不执行
}
}
测试:
package com.lee.spring002.aop.xml; import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TransactionTest { /**
* 原理:
* 1、当spring容器启动的时候,加载两个bean,对两个bean进行实例化
* 2、当spring容器对配置文件解析到<aop:config>的时候
* 3、把切入点表达式解析出来,按照切入点表达式匹配spring容器内容的bean
* 4、如果匹配成功,则为该bean创建代理对象
* 5、当客户端利用context.getBean获取一个对象时,如果该对象有代理对象,则返回代理对象
* 如果没有代理对象,则返回对象本身
*/
@Test
public void testPerson() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
IPersonDAO personDAO = (IPersonDAO)context.getBean("personDAO");
personDAO.savePerson();
} }
github地址:https://github.com/leechenxiang/maven-spring002-aop
Spring 一二事(9) - xml 形式的 AOP的更多相关文章
- Spring 一二事(10) - annotation AOP
先贴出POM的内容,这个毕竟是用的maven来简单构建的 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:x ...
- Spring 一二事(8) - annotation 形式的 MVC
<!-- component:把一个类放入到spring容器中,该类就是一个component 在base-package指定的包及子包下扫描所有的类 --> <context:co ...
- [原创]java WEB学习笔记108:Spring学习---基于配置文件的形式实现AOP
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- Spring 一二事(7) - annotation
之前的文章大多都是一带而过,一方面比较简单,一方面不是用的注解形式 在企业开发中,主要还是使用的注解来进行开发的 1 <!-- component:把一个类放入到spring容器中,该类就是一个 ...
- Spring 一二事(4) - 单例
spring bean配置后再默认情况下是单例的,如果需要配置可以选择 prototype, request, session和global session 在配置spring mvc的action时 ...
- Spring 一二事(1)
简单介绍一下spring,一方面带新手入入门,一方面自己也重温一下第一个小工厂先暂时不用maven,下一个会用maven来来配置 jar包只需要一个,spring版本为2.5(暂时为2.5,后续更新, ...
- Spring 一二事(5) - 依赖注入
<!-- 依赖注入的装配过程 --> <bean id="person" class="com.lee.spring007.di.xml.setter. ...
- Spring 一二事(2)
静态工厂方法及实例工厂的使用: applicationContext.xml: <!-- factory-method 是指调用静态工厂方法 --> <bean id="h ...
- Spring 一二事(6) - IOC MVC 简易搭建
<bean id="personAction" class="com.lee.spring008.IOC.DI.MVC.PersonAction"> ...
随机推荐
- Octave中plot函数的用法
octave:14> help plot'plot' is a function from the file C:\Octave\Octave3.6.4_gcc4.6.2\share\octav ...
- java面试第五天
修饰符abstract:抽象的,定义框架不去实现,可以修饰类和方法 abstract修饰类: 会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声明的类型,也就是编译时类型 抽象类 ...
- vue单文件(sfc)编译为js的流程
1.流程 2.参考文章地址 https://segmentfault.com/a/1190000012336392 3.Vue框架的parseComponent https://github.com/ ...
- margin和padding的学习
你在学习margin和padding的时候是不是懵了--什么他娘的内边距,什么他娘的外边距.呵呵呵,刚開始我也有点不理解,后来通过查资料学习总算弄明确了,如今我来谈一下自己对margin和paddin ...
- exception http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application
http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed w ...
- 【面试笔试】Java常见面试笔试总结
Java 基础 1.有哪些数据类型 Java定义了8种简单类型:byte.short.int.long.char.float.double和boolean. 2.面向对象的语言特征 封装.继承.多态 ...
- Codeforces554C:Kyoya and Colored Balls(组合数学+费马小定理)
Kyoya Ootori has a bag with n colored balls that are colored with k different colors. The colors are ...
- C#多线程之 ManualResetEvent和AutoResetEvent
初次体验 ManualResetEvent和AutoResetEvent主要负责多线程编程中的线程同步:以下一段是引述网上和MSDN的解析: 在.Net多线程编程中,AutoResetEvent和Ma ...
- iOS 直播推流 - 搭建基于RTMP的本地Nginx服务器
前端时间,公司要调研直播相关的内容,特地花时间进行了一番调研. 本篇将记录其中的推流篇-本地推理播放测试. 关于Nginx: 配置Nginx以支持HLS.RTMP的推流与拉流,iOS系统使用LFLiv ...
- swift上传图片
import UIKit import AFNetworking class YJRequest: NSObject { //#pragma mark - 上传图片 func uploadImageW ...