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: 为了测试程序异常


 



复制代码

posted on   ~码铃薯~  阅读(889)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示