spring切面:注解:抛出异常增强

用注解定义增强
在项目中添加spring AOP相关的jar文件;
使用注解定义前置增强和后置增强实现日志功能;
编写spring配置文件,织入注解定义的增强。

1.aop/UserLogger注释增强类

package aop;

import java.util.Arrays;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.AfterReturning;


@Aspect
public class UserLogger {
	private static final Logger log = Logger.getLogger(UserLogger.class);
@Before("execution(* biz.impl.*.*(..))")
public void before(JoinPoint jp){
	System.out.println("注解前置增强");
	System.out.println("调用的方法:"+jp.getSignature()+",传递参数:"+Arrays.toString(jp.getArgs()));
}
@AfterReturning(pointcut="execution(* biz.impl.*.*(..))",returning="returnResult")
public void afterReturning(JoinPoint jp,Object returnResult){
	System.out.println("注解后置增强");
	System.out.println("调用的方法:"+jp.getSignature()+",传递参数:"+Arrays.toString(jp.getArgs())+",返回参数:"+returnResult);
}
@AfterThrowing(pointcut="execution(* biz.impl.*.*(..))",throwing="e")
public void afterThrowing(JoinPoint jp,RuntimeException e){

	log.error("调用的方法:"+jp.getSignature()+",传递参数:"+Arrays.toString(jp.getArgs())+",方法发生异常:"+e);
}
}

2.applicationContext.xml配置文件

<?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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
 <!--  将前置增强类配成bean,交给spring管理 
	<bean id="loggerBefore" class="aop.LoggerBefore"></bean>
	将后置增强类配成bean,交给spring管理
	<bean id="afterReturning" class="aop.AfterReturning"></bean>
	配置切面(将增强类关联给某个条件范围内的方法)
	<bean id="errorLogger" class="aop.ErrorLogger"></bean>  -->
	<bean id="aroundLogger" class="aop.AroundLogger"></bean>
	<bean class="aop.UserLogger"></bean>
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
	<!-- <aop:config>
	   配置切入点pointcut
	   <aop:pointcut expression="execution(* biz.impl.*.*(..))" id="myPoint"/>
	   <aop:advisor advice-ref="loggerBefore" pointcut-ref="myPoint"/>
	   <aop:advisor advice-ref="afterReturning" pointcut-ref="myPoint"/>
	   <aop:advisor advice-ref="errorLogger" pointcut-ref="myPoint"/>
	   <aop:advisor advice-ref="aroundLogger" pointcut-ref="myPoint"/>
	</aop:config> -->
    <bean id="userBiz" class="biz.impl.UserBizImpl" p:userDao-ref="userDao">
      <!--  <property name="userDao" ref="userDao2"></property> -->
    </bean>
   <bean id="userDao" class="dao.impl.UserDaoImpl" />
   <bean id="userDao2" class="dao.impl.UserDaoImpl2" />
</beans>

3.dao.impl/UserDaoImpl类

package dao.impl;

import javax.management.RuntimeErrorException;

import dao.UserDao;
import entity.User;

public class UserDaoImpl implements UserDao {

	@Override
	public User findUser() {
		System.out.println("===========Dao层查询User 1==============");
		User u=new User("Tom",22);
		throw new RuntimeException("出现异常");
		//return u;
	}

}

4.Test方法

public static void main(String[] args) {
		
		//加载spring容器,解析配置文件
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		 UserBiz userBiz=(UserBiz) ac.getBean("userBiz");
		 User u= userBiz.getUser(101);
		 System.out.println(u.getUname()+","+u.getAge());
	}

在这里插入图片描述

### 回答1: 是的,在 Spring Boot 中使用 AOP 时,如果在切面类中抛出自定义异常,实际上会被包装成一个 `UndeclaredThrowableException` 异常抛出。这是因为,在使用 JDK 动态代理的 AOP 实现中,代理类方法的调用是通过反射来实现的,当在切面类中抛出异常时,如果该异常并不是被代理类方法声明抛出的异常,那么就会被包装成一个 `UndeclaredThrowableException` 异常抛出。 为了解决这个问题, 你可以在切面类中捕获你的自定义异常,然后用另外的方式来处理(例如记录日志,发送邮件等) 也可以改用CGLIB的代理方式。只要在配置类上加上 ``` @EnableAspectJAutoProxy(proxyTargetClass = true) ``` 选择CGLIB的代理方式即可,但是CGLIB比JDK多一些内存占用。 ### 回答2: 在Spring Boot中,使用切面类(Class)时,如果切面类方法抛出自定义异常,有时候会导致UndeclaredThrowableException异常。 UndeclaredThrowableException是一个运行时异常,它表示未声明的Throwable对象,即无法在方法签名中声明的异常。当切面类方法抛出自定义异常时,但是该异常不在方法签名中声明或者不是方法中throws语句抛出的任何已知异常时,Spring会将该异常包装在UndeclaredThrowableException中抛出。 通常使用Spring的AOP(面向切面编程)功能时,我们会定义切面类和切点来实现一些横向的关注点。在切面类中可以定义一些通知(advice)方法,当目标方法执行前、后、或者抛出异常时执行。其中,抛出异常的时候,可以自定义异常类来标识特定的错误或业务逻辑。 然而,由于Java的异常处理机制,只能在方法声明中包括方法可能抛出的所有已检查异常。而对于未检查的异常,我们无法在方法签名中显式声明。因此,如果切面类中的方法抛出自定义异常,但是该异常不是方法签名中声明的已检查异常,就会导致UndeclaredThrowableException异常的出现。 为了解决这个问题,可以考虑两种方式: 1. 将自定义异常类声明为继承RuntimeException等未检查异常。这样就不需要在方法签名中声明该自定义异常,也不会导致UndeclaredThrowableException异常的抛出。 2. 在切面类的通知方法中,捕获自定义异常并处理,而不是将其抛出。这样即使异常不在方法签名中声明,也不会导致UndeclaredThrowableException异常。可以通过日志记录、返回特定的错误码等方式来处理异常,以保证程序的正常执行。 总之,当切面类中的方法抛出自定义异常时,如果该异常不在方法签名中声明,就会抛出UndeclaredThrowableException异常。为了避免这种情况,我们可以将自定义异常类声明为未检查异常,或者在通知方法中捕获并处理该异常。 ### 回答3: 在使用Spring Boot的切面类中,如果切面方法中抛出了自定义异常,可能会导致UndeclaredThrowableException异常被抛出。 UndeclaredThrowableException异常是Java反射机制的异常,它是由于通过反射调用方法时,被调用方法抛出了一个检查异常,但调用方没有声明该异常,导致未进行异常处理抛出的异常。 切面类是用于实现面向切面编程的一种方式,它可以在方法执行前、执行后、抛出异常时等关键点插入额外的逻辑。当切面方法中抛出自定义异常时,如果被调用的方法没有声明该异常,反射机制会将该异常包装在UndeclaredThrowableException中抛出。 为了解决这个问题,我们可以在切面方法中声明抛出自定义异常,并且被调用方也要声明该异常或它的父类异常,并且在调用方进行异常处理。如果被调用方无法修改,我们可以通过try-catch捕获UndeclaredThrowableException异常,并处理其中的原始异常。 在切面类中抛出自定义异常时,需要确保异常类型正确与声明相符,否则反射机制可能无法正确处理,仍然会抛出UndeclaredThrowableException异常。 总之,Spring Boot中的切面类可能会抛出UndeclaredThrowableException异常,这是由于反射机制中方法抛出了一个检查异常,但调用方没有声明该异常所导致的。我们可以通过在切面方法中声明自定义异常、被调用方声明相应异常、进行异常处理等方式解决该问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值