Spring前置通知與後置通知

Spring前置通知與後置通知

一、Spring 前置通知與後置通知流程

1、加入jar包

com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar

commons-logging-1.1.3.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar

2、在配置文件中加入aop的命名空間


3、基於註解的方式

(1)在配置文件中加入如下配置
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>


(2)把橫切關注點的代碼抽象到切面的類中。
i. 切面首先是一個ioc的bean,也即是加入@Component註解
ii. 切面還需要加入@Aspect 註解


(3)在類中聲明各種通知
i 聲明一個方法
ii 在方法前面加入@Before 註解


(4)可以在通知方法中聲明一個類型爲 JoinPoint 的參數。然後就能訪問鏈接細節,如方法名稱和參數值。


二、具體代碼如下:

1、ArithmeticCalculator接口

package com.at.aop.impl;

public interface ArithmeticCalculator {

	int add(int i,int j);
	int sub(int i,int j);
	
	int mul(int i,int j);
	int div(int i,int j);
}


2、ArithmeticCalculatorImpl實現類

package com.at.aop.impl;

import org.springframework.stereotype.Component;

@Component
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {

	@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 mul(int i, int j) {
		int result = i * j;
		return result;
	}

	@Override
	public int div(int i, int j) {
		int result = i / j;
		return result;
	}

}

3、LoggingAspect類

package com.at.aop.impl;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//幫這個類聲明爲一個切面:需要把該類放入到IOC容器中,再聲明爲一個切面
@Aspect
@Component
public class LoggingAspect {

	//聲明該方法是一個前置通知:在目標方法開始之前執行。
	@Before("execution(* com.at.aop.impl.ArithmeticCalculator.*(int, int))")
	public void beforeMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		List<Object> args = Arrays.asList(joinPoint.getArgs());
		System.out.println(" 方法 "+methodName+" 開始 "+args);
	}
	
	//後置通知:在目標方法執行後(無論是否發生異常),執行的通知。
	//在後置通知中還不能訪問目標方法執行的結果。這個結果需要在返回通知中訪問(下文講)
	@After("execution(* com.at.aop.impl.ArithmeticCalculator.*(int, int))")
	public void afterMethod(JoinPoint joinPoint){
		String methodName = joinPoint.getSignature().getName();
		System.out.println(" 方法 "+methodName+" 結束 ");
	}
}

4、TestAspect測試類

package com.at.aop.impl;

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

public class TestAspect {

	public static void main(String[] args) {
		
		//1、創建spring的ioc容器
		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//2、從ioc容器中獲取bean的事例
		ArithmeticCalculator ac = ctx.getBean(ArithmeticCalculator.class);
		
		//3、使用bean
		int result = ac.add(3, 6);
		System.out.println("result "+result);
		
		int result2 = ac.div(12, 6);
		System.out.println("result "+result2);
	}
}


5、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: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">
	
	<!-- 配置自動掃描的包 -->
	<context:component-scan base-package="com.at.aop.impl"></context:component-scan>
	
	<!-- 使AspjectJ 註解起作用:自動爲匹配的類生產代理對象 -->
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

6、測試結果

信息: Loading XML bean definitions from class path resource [applicationContext.xml]
 方法 add 開始 [3, 6]
 方法 add 結束 
result 9
 方法 div 開始 [12, 6]
 方法 div 結束 
result 2


By  luoyepiaoxue2014
微博地址:http://weibo.com/luoyepiaoxue2014 http://weibo.com/luoyepiaoxue2014
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章