spring_(19)Spring _重用切點表示式md
阿新 • • 發佈:2018-12-02
例子程式
基本結構
ArithmeticCalculator.java
package com.spring.aop;
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);
}
ArithmeticCalculatorImpl.java
package com.spring.aop;
import org. springframework.stereotype.Component;
@Component("arithmeticCalculatorImpl")
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;
}
}
LogginAspect.java
package com.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@Aspect
@Component
public class LogginAspect {
/**
* 定義一個方法,用於宣告切入點表示式。一般地,該方法中再不需要填入其他的程式碼
* 使用@Pointcut來宣告切入點表示式
* 後面的其他通知直接使用方法名來引用當前的切入點表示式
*/
@Pointcut("execution(public int com.spring.aop.ArithmeticCalculator.*(..))")
public void declareJointPointExpression(){
}
/**
* 在com.spring.aop.ArithmeticCalculator 介面的每一個實現類的方法開始之前執行一段程式碼
*/
@Before("declareJointPointExpression()")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
Object[] args = joinPoint.getArgs();
System.out.println("The method "+methodName+" begins with "+Arrays.asList(args));
}
/**
* 在方法之後執行的程式碼。無論是否出現異常
* @param joinPoint
*/
@After("declareJointPointExpression()")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends ");
}
/**
* 在方法正常結束後執行的程式碼
* 返回通知是可以訪問到方法的返回值的
* @param joinPoint
*/
@AfterReturning(value = "declareJointPointExpression()",
returning = "result")
public void afterReturning(JoinPoint joinPoint,Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" ends with "+ result);
}
/**
* 在目標方法出現異常時會執行的程式碼。
* 可以訪問到異常物件;且可以指定在出現特定異常時再執行通知程式碼
* @param joinPoint
* @param ex
*/
@AfterThrowing(value = "declareJointPointExpression()",
throwing = "ex")
public void afterThrowing(JoinPoint joinPoint,Exception ex){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName+" occurs exception "+ ex);
}
/**
* 環繞通知需要攜帶ProceedingJoinPoint型別的引數。
* 環繞通知類似於動態代理的全過程:ProceedingJoinPoint型別的引數可以決定是否執行目標方法
* 且環繞通知必須有返回值,返回值即為目標方法的返回值
*/
@Around("declareJointPointExpression()")
public Object aroundMethod(ProceedingJoinPoint pjd){
Object result = null;
String methodName = pjd.getSignature().getName();
//執行目標方法
try{
//前置通知
System.out.println("環繞The method "+methodName+" begins with "+Arrays.asList(pjd.getArgs()));
result = pjd.proceed();
//返回通知
System.out.println("環繞The method "+methodName+" ends with "+result);
}catch (Throwable e){
//異常通知
System.out.println("環繞The method "+methodName+" occurs exception:" + e);
throw new RuntimeException(e);
}
//後置通知
System.out.println("環繞The method "+methodName+" ends");
return result;
}
}
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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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">
<!--配置自動掃描的包-->
<context:component-scan base-package="com.spring.aop"></context:component-scan>
<!--配置自動為匹配aspectJ註解的Java類生成代理物件-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
Main.java
package com.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculatorImpl");
int result = arithmeticCalculator.add(1,2);
System.out.println("result:"+result);
result = arithmeticCalculator.div(1000,2);
System.out.println("result:"+result);
}
}
執行結果