spring23:Aspectj实现异常通知@AfterThrowing

49 篇文章 0 订阅

切入类:

 

package com.atChina.Test4;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

/**
 * @Aspect:来自aspectj框架,表示当前类是切面类
 * 切面类是用来给业务方法增强功能的类
 */
@Aspect
public class MyAspect {

	/**
	 * @AfterThrowing:异常通知,目标方法抛出异常时执行
	 *  属性:1. value,表示切入点表达式(切面功能加入的位置)
	 *  	 2. throwing,自定义的变量,表示目标方法的异常对象,需要和通知方法的参数名
	 *  位置:在方法的上面
	 * 
	 * 特点:
	 *  1.在目标方法抛出异常时执行
	 *  2.不是异常处理程序,只是得到异常的消息
	 *  3.可以作为目标方法的监控程序,检查目标方法是否正常执行
	 */
	@AfterThrowing(value="execution(* *..SomeServiceImpl.doException())", throwing="ex")
	public void myAfterThrowing(Throwable ex){
		System.out.println("出现了异常"+ex);
	}
}

 普通bean

package com.atChina.Test4;

public interface SomeService {
	public void doSome();
	public String doOther(String params);
	public Student getStudent();
	public String doAround();
	public void doException();
}

package com.atChina.Test4;

public class SomeServiceImpl implements SomeService {

	@Override
	public void doSome() {
		System.out.println("执行了doSome业务方法...");
	}

	@Override
	public String doOther(String params) {
		// TODO Auto-generated method stub
		System.out.println("执行了doSome业务方法..."+params);
		return params;
	}

	@Override
	public Student getStudent() {
		Student st = new Student();
		st.setAge(22);
		st.setName("孙悟空");
		return st;
	}

	@Override
	public String doAround() {
		System.out.println("执行了doAround业务方法...");
		return "doAround";
	}

	@Override
	public void doException() {
		// TODO Auto-generated method stub
		int i = 5/0;
		System.out.println("doException...");
	}
}


package com.atChina.Test4;

public class Student {
	private String name;
	private int age;
	
	public void setName(String name) {
		this.name = name;
	}
	
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
}


 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- 引用Spring的多个Schema空间的格式定义文件 -->
<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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd ">

		<!-- 声明目标类对象 -->
		<bean id="someService" class="com.atChina.Test4.SomeServiceImpl" />
		
		<!-- 声明切面类对象 -->
		<bean id="myAspect" class="com.atChina.Test4.MyAspect" />
		
		<!-- 声明自动代理生成器,创建代理对象 -->
		<aop:aspectj-autoproxy /> <!-- 寻找aspectj框架能够识别的标签 -->
</beans>

测试方法:

@Test
	public void test2(){
		String configLocation = "com/atChina/Test4/applicationContext.xml"; // 类路径的根目录
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
		// 目标对象有接口,aspectj默认使用的是jdk动态代理
		SomeService proxy = (SomeService) ctx.getBean("someService");
		System.out.println(proxy.getClass().getName());
		// proxy.doSome();
		System.out.println("===================================");
		proxy.doException();
	}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值