1. 程式人生 > >Spring AOP 基於 XML ---- 宣告通知

Spring AOP 基於 XML ---- 宣告通知

知識點如下


 

例項如下

介面類

package cn.com.day02;

public interface Calculator {
	/*
	 * author:命運的信徒 date:2018/12/21 arm:aop基礎
	 * 這裡是四個方法;返回的結果是Int型別
	 */
	// +
	int add(int i, int j);

	// -
	int sub(int i, int j);

	// *
	int take(int i, int j);

	// 除法
	int div(int i, int j);
}

實現類

package cn.com.day02;
import org.springframework.stereotype.Component;
@Component
public class CalculatorImp implements Calculator {
	/*
	 * 自帶日誌的
	 */
	@Override
	public int add(int i, int j) {
		int result = i + j;
		return result;
	}

	@Override
	public int sub(int i, int j) {

		int result = i - j;
		return result;
	}

	@Override
	public int take(int i, int j) {

		int result = i * j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		// TODO Auto-generated method stub
		int result = i / j;
		return result;
	}

}

切面 類(環繞通知)

package cn.com.day02;

import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
public class AroundAspect {
	public Object aroundMethod(ProceedingJoinPoint pcj){
		//前置通知
		String name=pcj.getSignature().getName();
		Object [] arg=pcj.getArgs();
		System.out.println("#####the method "+name+"is begin"+Arrays.asList(arg));
		//返回通知
		Object o=null;
		try {
			o=pcj.proceed();
			System.out.println("#####the method "+name+"is return"+o);
		} catch (Throwable e) {
			//異常通知
			System.out.println("#####the method "+name+"is error"+e);	
		}
		//後置通知
		System.out.println("#####the method "+name+"is end");
		return o;
		
		
	}
}

切面類(四個分開了寫的)

package cn.com.day02;
import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
//切面優先順序,值越小,優先順序越高
public class AspectClass {
	public void getLogging(JoinPoint joinpoint) {
		// 根據連線點獲取方法名
		String methodname = joinpoint.getSignature().getName();
		// 獲取引數名稱
		List<Object> args = Arrays.asList(joinpoint.getArgs());
		System.out.println("the method " + methodname + "is begins with "
				+ args);
	}
   public void after(JoinPoint joinpoint){
		String name=joinpoint.getSignature().getName();
		System.out.println("the method " + name + "is end");
		
	}
	//返回通知
	//只有在正常執行的情況下才會執行的,這個方法可以返回結果值
	public void afterReturn(JoinPoint joinpoint,Object result){
		String name=joinpoint.getSignature().getName();
		System.out.println("the method " + name + "is end"+result);
		
	}
	//異常通知,只有報錯的時候才會執行的方法,可以訪問到異常
	public void afterThrow(JoinPoint joinpoint,Exception ex){
		String name=joinpoint.getSignature().getName();
		System.out.println("the method " + name + "is throw error"+ex);
		
	}
}

配置檔案

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<!-- 通過xml檔案的形式配置AOP -->
<bean id="ca" class="cn.com.day02.CalculatorImp"></bean>
<bean id="as" class="cn.com.day02.AspectClass"></bean>
<bean id="ar" class="cn.com.day02.AroundAspect"></bean>
<!-- 用基於 XML 的配置宣告切面 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="pointcut" expression="execution(* cn.com.day02.CalculatorImp.*(..))" />


<aop:aspect order="1" ref="as" id="aspect1">
<aop:before method="getLogging" pointcut-ref="pointcut" />
<aop:after method="after" pointcut-ref="pointcut" />
<aop:after-returning method="afterReturn" pointcut-ref="pointcut"  returning="result"/>
<aop:after-throwing method="afterThrow" pointcut-ref="pointcut"   throwing="ex"/>
</aop:aspect>


<aop:aspect id="aspect2" order="1" ref="ar">
<aop:around method="aroundMethod" pointcut-ref="pointcut" />
</aop:aspect>
</aop:config>
</beans>

測試類

package cn.com.day02;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
	public static void main(String[] args) {
		ApplicationContext ioc = new ClassPathXmlApplicationContext(
				"applicationContext.aop.xml");
		CalculatorImp cx = (CalculatorImp) ioc.getBean("ca");
		int result = cx.add(12, 4);
		int re = cx.div(12, 2);
	}
}