Spring AOP的五種通知方式代碼實(shí)例
這篇文章主要介紹了Spring AOP的五種通知方式代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
AOP的五種通知方式:
前置通知:在我們執(zhí)行目標(biāo)方法之前運(yùn)行(@Before)
后置通知:在我們目標(biāo)方法運(yùn)行結(jié)束之后,不管有沒有異常(@After)
返回通知:在我們的目標(biāo)方法正常返回值后運(yùn)行(@AfterReturning)
異常通知:在我們的目標(biāo)方法出現(xiàn)異常后運(yùn)行(@AfterThrowing)
環(huán)繞通知:目標(biāo)方法的調(diào)用由環(huán)繞通知決定,即你可以決定是否調(diào)用目標(biāo)方法,joinPoint.procced()就是執(zhí)行目標(biāo)方法的代碼 。環(huán)繞通知可以控制返回對象(@Around)
一、導(dǎo)jar包
- com.springsource.net.sf.cglib-2.2.0.jar
- com.springsource.org.aopalliance-1.0.0.jar
- com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
- commons-logging-1.1.3.jar
- spring-aop-4.0.0.RELEASE.jar
- spring-aspects-4.0.0.RELEASE.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
- spring-jdbc-4.0.0.RELEASE.jar
- spring-orm-4.0.0.RELEASE.jar
- spring-tx-4.0.0.RELEASE.jar
- spring-web-4.0.0.RELEASE.jar
- spring-webmvc-4.0.0.RELEASE.jar
二、在類路徑下建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-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--配置自動掃描的包-->
<context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan>
<!--配置自動為匹配aspectJ 注解的Java類生成代理對象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
三、接口
//接口
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);
}
四、實(shí)現(xiàn)類
package com.atguigu.spring.aop;
import org.springframework.stereotype.Component;
/**
* @Author 謝軍帥
* @Date2019/12/6 21:23
* @Description
*/
//實(shí)現(xiàn)類
@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {
@Override
public int add(int i, int j) {
int relust = i+j;
return relust;
}
@Override
public int sub(int i, int j) {
int relust = i-j;
return relust;
}
@Override
public int mul(int i, int j) {
int relust = i*j;
return relust;
}
@Override
public int div(int i, int j) {
int relust = i/j;
return relust;
}
}
五、定義切面類
package com.atguigu.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;
/**
* @Author 謝軍帥
* @Date2019/12/11 11:17
* @Description
*/
@Component
@Aspect
public class LoggingAspect {
/**
* 在每一個接口的實(shí)現(xiàn)類的每一個方法開始之前執(zhí)行一段代碼
*/
@Before("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
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("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName +" end......");
}
/**
* 返回通知
* 在方法正常結(jié)束后執(zhí)行的代碼
* 返回通知是可以訪問方法的返回值的!
* @param joinPoint*/
@AfterReturning(value = "execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))",
returning = "result")
public void afterReturning(JoinPoint joinPoint,Object result){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName +" end......result:"+result);
}
/**
* 在目標(biāo)方法出現(xiàn)異常時會執(zhí)行的代碼
* 可以訪問到異常對象,且可以指定在出現(xiàn)特定異常時在執(zhí)行通知代碼
* @param joinPoint
* @param ex*/
@AfterThrowing(value = "execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))", throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, Exception ex){
String methodName = joinPoint.getSignature().getName();
System.out.println("The method "+methodName +"occurs exception :" +ex);
}
/**
* 環(huán)繞通知需要攜帶 ProceedingJoinPoint 類型的參數(shù)
* 環(huán)繞通知類似于動態(tài)代理的全過程:ProceedingJoinPoint 類型的參數(shù)可以決定是否執(zhí)行目標(biāo)方法。
* 且環(huán)繞通知必須有返回值,返回值即為目標(biāo)方法的返回值
* @param proceedingJoinPoint
*/
/*@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.* (..))")
public Object aroundMethod(ProceedingJoinPoint proceedingJoinPoint){
Object result = null;
String methodName = proceedingJoinPoint.getSignature().getName();
try {
//前置通知
System.out.println("The method "+methodName+" begins with "+Arrays.asList(proceedingJoinPoint.getArgs()));
//執(zhí)行目標(biāo)方法
result = proceedingJoinPoint.proceed();
//返回通知
System.out.println("The method ends with "+result);
} catch (Throwable e) {
//異常通知
System.out.println("The method occurs exception:"+e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("The method "+methodName+" ends........");
return result;
}*/
}
六、測試
public class Test_aop {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) context.getBean("arithmeticCalculator");
System.out.println(arithmeticCalculator.getClass().getName());
int result = arithmeticCalculator.add(1,2);
System.out.println("result:"+result);
result = arithmeticCalculator.div(200,0);
System.out.println("result:"+result);
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MyBatis批量插入幾千條數(shù)據(jù)為何慎用foreach
這篇文章主要介紹了MyBatis批量插入幾千條數(shù)據(jù)為何慎用foreach問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
SpringBoot+EasyPoi實(shí)現(xiàn)excel導(dǎo)出功能
最新小編遇到這樣一個需求,根據(jù)檢索條件查詢列表并將結(jié)果導(dǎo)出到excel,實(shí)現(xiàn)過程也非常簡單,感興趣的朋友跟隨小編一起看看吧2021-09-09
Java語言簡介(動力節(jié)點(diǎn)Java學(xué)院整理)
Java是一門面向?qū)ο缶幊陶Z言,不僅吸收了C++語言的各種優(yōu)點(diǎn),還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強(qiáng)大和簡單易用兩個特征,下面通過本文給大家分享java語言的簡介,感興趣的朋友一起看看吧2017-03-03
解決Spring session(redis存儲方式)監(jiān)聽導(dǎo)致創(chuàng)建大量redisMessageListenerConta
這篇文章主要介紹了解決Spring session(redis存儲方式)監(jiān)聽導(dǎo)致創(chuàng)建大量redisMessageListenerContailner-X線程問題,需要的朋友可以參考下2018-08-08
Java如何根據(jù)前端返回的字段名進(jìn)行查詢數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Java如何根據(jù)前端返回的字段名進(jìn)行查詢數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
SpringSecurity配置HTTPS的實(shí)現(xiàn)
本文介紹了SpringBoot項(xiàng)目中配置HTTPS并集成SpringSecurity,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-01-01
Java Socket使用加密協(xié)議進(jìn)行傳輸對象的方法
這篇文章主要介紹了Java Socket使用加密協(xié)議進(jìn)行傳輸對象的方法,結(jié)合實(shí)例形式分析了java socket加密協(xié)議相關(guān)接口與類的調(diào)用方法,以及服務(wù)器、客戶端實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-06-06
SpringBoot項(xiàng)目打包成war包并部署在tomcat上運(yùn)行的操作步驟
我們開發(fā) SpringBoot 項(xiàng)目有時我們會需要打包成 war 包,放入外置的 Tomcat 中進(jìn)行運(yùn)行,或者使用工具idea直接啟動,便于開發(fā)調(diào)試,本文給大家分享SpringBoot項(xiàng)目打包成war包并部署在tomcat上運(yùn)行的操作步驟,感興趣的朋友一起看看吧2024-03-03

