Spring框架之最终增强

Spring框架的最终增强:

先解释一下:

最终增强的特点:

  无论方法是否抛出异常,无论目标方法时候发生异常,都会在目标方法最后织入增强处理,即:该增强都会得到执行。

  类似于异常处理机制中的finally块的作用,一般情况用于资源的释放。

  可以为各个功能模块提供统一的处理方案,可拔插的处理方案。

<aop:after>元素是用来定义最终增强的。

 1 package aop;
 2 
 3 import java.util.Arrays;
 4 
 5 import org.apache.log4j.Logger;
 6 import org.aspectj.lang.JoinPoint;
 7 
 8 //日志处理类  增强处理类-日志
 9 public class UserServiceLogger {
10     private Logger logger = Logger.getLogger(UserServiceLogger.class);
11 
12     // 前置增强
13     public void before(JoinPoint joinPoint) {
14         logger.info("调用" + joinPoint.getTarget() + "的"
15                 + joinPoint.getSignature() + "方法,方法参数是:"
16                 + Arrays.toString(joinPoint.getArgs()));
17     }
18 
19     // 后置增强
20     public void afterReturning(JoinPoint joinPoint,Object result) {
21         logger.info("调用" + joinPoint.getTarget() + "的"
22                 + joinPoint.getSignature() + "方法,方法的返回值是:"
23                 +result);
24     }
25     
26     // 异常抛出增强
27     public void afterThrowingError(JoinPoint joinPoint,RuntimeException e) {
28         logger.info("调用" + joinPoint.getTarget() + "的"
29                 + joinPoint.getSignature().getName() + "方法,发生异常:"
30                 +e);
31     }
32     //最终增强
33     public void after(JoinPoint joinPoint) {
34         logger.info("调用" + joinPoint.getTarget() + "的"
35                 + joinPoint.getSignature().getName() + "方法,结束了"
36                 );
37     }
38     
39 }
 1 package dao.impl;
 2 
 3 import dao.UserDao;
 4 import entity.User;
 5 
 6 /**
 7  * 用户DAO类,实现IDao接口,负责User类的持久化操作
 8  */
 9 public class UserDaoImpl implements UserDao {
10 
11     public void save(User user) {
12         // 这里并未实现完整的数据库操作,仅为说明问题
13         System.out.println("保存用户信息到数据库");
14         throw new RuntimeException("为了测试程序异常");
15     }
16 }

核心配置文件:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5     http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 6     http://www.springframework.org/schema/aop
 7     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
 8     <!--以上是Spring框架的头信息 使用p命名空间注入 -->
 9     <bean id="dao" class="dao.impl.UserDaoImpl"></bean>
10     <bean id="service" class="service.impl.UserServiceImpl">
11         <property name="dao" ref="dao"></property>
12     </bean>
13     <!-- 声明增强方法所在的Bean -->
14     <bean id="theLogger" class="aop.UserServiceLogger"></bean>
15     <aop:config>
16         <!--定义切入点 -->
17         <aop:pointcut expression="execution(public void addNewUser(entity.User))"
18             id="pointcut" />
19         <aop:aspect ref="theLogger">
20             <aop:after method="after"
21                 pointcut-ref="pointcut"  />
22         </aop:aspect>
23     </aop:config>
24 </beans>

测试方法:

 1 package test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import service.UserService;
 8 import service.impl.UserServiceImpl;
 9 
10 import entity.TestEntity;
11 import entity.User;
12 
13 
14 public class AopTest {
15 
16     @Test
17     public void aopTest() {
18         ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
19         UserService a = (UserService) ctx.getBean("service");
20         User user=new User();
21         user.setUsername("丫丫");
22         a.addNewUser(user);
23     }
24 
25 }

运行结果:

保存用户信息到数据库
12-30 13:01:04[INFO]aop.UserServiceLogger
-调用service.impl.UserServiceImpl@47d90b9e的addNewUser方法,结束了

原文地址:https://www.cnblogs.com/dongyaotou/p/12118130.html