Spring AOP(5)-- 註解

applicationContext.xmljava

<?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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
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-4.0.xsd">spring

<!-- 配置自動掃描的包 -->
<context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>

<!-- 配置自動爲匹配 aspectJ 註解的 Java 類生成代理對象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>app

ArithmeticCalculator.javaide

package com.atguigu.spring.aop;ui

public interface ArithmeticCalculator {spa

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.javacomponent

package com.atguigu.spring.aop;xml

import org.springframework.stereotype.Component;對象

@Component("arithmeticCalculator")
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;
}

}

LoggingAspect.java

package com.atguigu.spring.aop;

import java.util.Arrays;

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;

/**
* 能夠使用 @Order 註解指定切面的優先級, 值越小優先級越高
*/
@Order(2)
@Aspect
@Component
public class LoggingAspect {

/**
* 定義一個方法, 用於聲明切入點表達式. 通常地, 該方法中再不須要添入其餘的代碼.
* 使用 @Pointcut 來聲明切入點表達式.
* 後面的其餘通知直接使用方法名來引用當前的切入點表達式.
*/
@Pointcut("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
public void declareJointPointExpression(){}

/**
* 在 com.atguigu.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));
}

/**
* 在方法執行以後執行的代碼. 不管該方法是否出現異常
*/
@After("declareJointPointExpression()")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " ends");
}

/**
* 在方法法正常結束受執行的代碼
* 返回通知是能夠訪問到方法的返回值的!
*/
@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);
}

/**
* 在目標方法出現異常時會執行的代碼.
* 能夠訪問到異常對象; 且能夠指定在出現特定異常時在執行通知代碼
*/
@AfterThrowing(value="declareJointPointExpression()",
throwing="e")
public void afterThrowing(JoinPoint joinPoint, Exception e){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method " + methodName + " occurs excetion:" + e);
}

/**
* 環繞通知須要攜帶 ProceedingJoinPoint 類型的參數.
* 環繞通知相似於動態代理的全過程: ProceedingJoinPoint 類型的參數能夠決定是否執行目標方法.
* 且環繞通知必須有返回值, 返回值即爲目標方法的返回值
*/
/*
@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")
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;
}
*/
}

VlidationAspect.java

package com.atguigu.spring.aop;

import java.util.Arrays;

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

@Order(1)
@Aspect
@Component
public class VlidationAspect {

@Before("com.atguigu.spring.aop.LoggingAspect.declareJointPointExpression()")
public void validateArgs(JoinPoint joinPoint){
System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs()));
}

}

Main.java

package com.atguigu.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("arithmeticCalculator"); System.out.println(arithmeticCalculator.getClass().getName()); int result = arithmeticCalculator.add(1, 2); System.out.println("result:" + result); result = arithmeticCalculator.div(1000, 10); System.out.println("result:" + result); } }

相關文章
相關標籤/搜索