一、Spring AOP简介(百度百科)

面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),是软件开发中的一个热点,也是 Spring

框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度

降低,提高程序的可重用性,同时提高了开发的效率。

主要的功能是:日志记录,性能统计,安全控制,事务处理,异常处理等等。

二、Spring AOP实例

(1,前置通知;2,后置通知;3,环绕通知;4,返回通知;5,异常通知;)

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 业务逻辑bean -->
<bean id="userService" class="top.ruandb.service.impl.UserServiceImpl" ></bean>
<!-- 自定义的切面 -->
<bean id="userServiceAspect" class="top.ruandb.advice.UserServiceAspect"></bean>
<!-- AOP配置 -->
<aop:config>
<!-- 定义切面 -->
<aop:aspect id="userServiceAspect" ref="userServiceAspect">
<!-- 定义切点,表达式: -->
<aop:pointcut expression="execution(* top.ruandb.service.*.*(..))" id="businessService"/>
<!-- 前置通知,在方法执行之前通知 -->
<aop:before method="doBefore" pointcut-ref="businessService"/>
<!-- 后置通知:在方法执行之后通知 -->
<aop:after method="doAfter" pointcut-ref="businessService"/>
<!-- 环绕通知:在方法前后环绕 -->
<aop:around method="doAround" pointcut-ref="businessService"/>
<!-- 返回通知:方法返回后通知 -->
<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
<!-- 异常通知:出现异常后通知 -->
<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
</aop:aspect>
</aop:config> </beans>
//切面
public class UserServiceAspect { public void doBefore(JoinPoint jp) {
System.out.println(jp.getTarget().getClass().getName()+"前置通知:添加用户之前");
}
public void doAfter(JoinPoint jp) {
System.out.println(jp.getTarget().getClass().getName()+"后置通知:添加用户之后");
}
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
System.out.println(pjp.getTarget().getClass().getName()+"环绕通知:环绕前");
Object obj = pjp.proceed();
System.out.println(pjp.getTarget().getClass().getName()+"环绕通知:环绕后");
return obj;
}
public void doAfterReturning(JoinPoint jp) {
System.out.println(jp.getTarget().getClass().getName()+"返回通知:返回通知");
}
public void doAfterThrowing(JoinPoint jp,Throwable ex) {
System.out.println(jp.getTarget().getClass().getName()+"异常通知:程序异常了"+ex.getMessage());
}
} public class SpringTest {
ApplicationContext ac ;
@Before
public void setUp() {
ac = new ClassPathXmlApplicationContext("applicationContext.xml");
}
@After
public void tearDown() {
ac = null;
} @Test
public void test1() {
UserServiceI userService = (UserServiceI) ac.getBean("userService") ;
userService.addUser("rdb"); }
} 结果:
top.ruandb.service.impl.UserServiceImpl前置通知:添加用户之前
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕前
添加用户rdb
top.ruandb.service.impl.UserServiceImpl返回通知:返回通知
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕后
top.ruandb.service.impl.UserServiceImpl后置通知:添加用户之后 结果(异常):
top.ruandb.service.impl.UserServiceImpl前置通知:添加用户之前
top.ruandb.service.impl.UserServiceImpl环绕通知:环绕前
添加用户rdb
top.ruandb.service.impl.UserServiceImpl异常通知:程序异常了/ by zero
top.ruandb.service.impl.UserServiceImpl后置通知:添加用户之后

最新文章

  1. Struts2中method={1}
  2. java中的重绘
  3. UVA 11133 - Eigensequence DP
  4. python autopy
  5. 1.Tomcat配置
  6. 2014-07-24 .NET实现微信公众号的消息回复与自定义菜单
  7. 开源的Owin 的身份验证支持 和跨域支持
  8. 派生类地址比基类地址少4(子类与基类指针强行转换的时候,值居然会发生变化,不知道Delphi BCB是不是也这样) good
  9. ds18b20再硬件设计部分的注意事项
  10. 大白话Vue源码系列(03):生成render函数
  11. “AS3.0高级动画编程”学习:第一章高级碰撞检测
  12. C#:如何使方法过时,如何否决方法
  13. flask请求流程详解
  14. demo1:会下蛋的机器人
  15. Windows10下pip的配置文件设置
  16. mysql update 忘加 where 文件恢复
  17. Junit测试时,如何截获到Console的输出
  18. 软件测试——Peer Review(简介)
  19. WordPress后台的文章、分类,媒体,页面,评论,链接等所有信息中显示ID并将ID设置为第一列
  20. (zxing.net)一维码Code 93的简介、实现与解码

热门文章

  1. 四、配置及使用Zabbix监控系统
  2. 九、部署audit监控文件
  3. 【NX二次开发】创建扫描特征
  4. SpringBoot线程池的创建、@Async配置步骤及注意事项
  5. DDoS防护方式以及产品
  6. Redis配置统计字典
  7. 在windows的情况下面右键添加vim
  8. codeforces 830E dp
  9. bcprov-jdk15-145.rar
  10. Flask(7)- request 对象