Spring框架-之异常抛出增强
前面我们学习了Spring框架的前置增强和后置增强,今天我们看下异常抛出增强,顾名思义,该增强类型是当程序运行的时候发生异常的时候才会执行,如果程序运行期间没有发生一异常,是不会执行的。
废话不多说了,直接上代码:
UserServiceLogger.java
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 }
在数据访问层模拟抛出异常:
package dao.impl;
import dao.UserDao;
import entity.User;
/**
* 用户DAO类,实现IDao接口,负责User类的持久化操作
*/
public class UserDaoImpl implements UserDao {
public void save(User user) {
// 这里并未实现完整的数据库操作,仅为说明问题
System.out.println("保存用户信息到数据库");
throw new RuntimeException("为了测试程序异常");
}
}
applicationContext.xml核心配置文件:
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-throwing method="afterThrowingError"
21 pointcut-ref="pointcut" throwing="e" />
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 09:19:54[INFO]aop.UserServiceLogger
-调用service.impl.UserServiceImpl@69b2283a的addNewUser方法,发生异常:java.lang.RuntimeException: 为了测试程序异常