AOP為Aspect Oriented Programming的縮寫卵牍,意為:面向切面編程闯团,通過預(yù)編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術(shù).AOP是OOP的延續(xù)摩瞎,是軟件開發(fā)中的一個熱點身弊,也是Spring框架中的一個重要內(nèi)容丸边,是函數(shù)式編程的一種衍生范型儿普。利用AOP可以對業(yè)務(wù)邏輯的各個部分進行隔離贮竟,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低丽焊,提高程序的可重用性,同時提高了開發(fā)的效率咕别。
在spring AOP中業(yè)務(wù)邏輯僅僅只關(guān)注業(yè)務(wù)本身技健,將日志記錄,性能統(tǒng)計惰拱,安全控制凫乖,事務(wù)處理,異常處理等代碼從業(yè)務(wù)邏輯代碼中劃分出來弓颈,通過對這些行為的分離帽芽,我們希望可以將它們獨立到非指導業(yè)務(wù)邏輯的方法中,進而改變這些行為的時候不影響業(yè)務(wù)邏輯的代碼翔冀。
相關(guān)注解介紹:
@Aspect:作用是把當前類標識為一個切面供容器讀取
@Pointcut:Pointcut是植入Advice的觸發(fā)條件导街。每個Pointcut的定義包括2部分,一是表達式纤子,二是方法簽名搬瑰。方法簽名必須是 public及void型款票。可以將Pointcut中的方法看作是一個被Advice引用的助記符泽论,因為表達式不直觀艾少,因此我們可以通過方法簽名的方式為 此表達式命名。因此Pointcut中的方法只需要方法簽名翼悴,而不需要在方法體內(nèi)編寫實際代碼缚够。
@Around:環(huán)繞增強,相當于MethodInterceptor
@AfterReturning:后置增強鹦赎,相當于AfterReturningAdvice谍椅,方法正常退出時執(zhí)行
@Before:標識一個前置增強方法,相當于BeforeAdvice的功能古话,相似功能的還有
@AfterThrowing:異常拋出增強雏吭,相當于ThrowsAdvice
@After: final增強,不管是拋出異撑悴龋或者正常退出都會執(zhí)行
使用pointcut代碼:
package com.aspectj.test.advice;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AdviceTest {
@Around("execution(* com.abc.service..many(..))")
public Object process(ProceedingJoinPoint point) throws Throwable {
System.out.println("@Around:執(zhí)行目標方法之前...");
//訪問目標方法的參數(shù):
Object[] args = point.getArgs();
if (args != null && args.length > 0 && args[0].getClass() == String.class) {
args[0] = "改變后的參數(shù)1";
}
//用改變后的參數(shù)執(zhí)行目標方法
Object returnValue = point.proceed(args);
System.out.println("@Around:執(zhí)行目標方法之后...");
System.out.println("@Around:被織入的目標對象為:" + point.getTarget());
return "原返回值:" + returnValue + "杖们,這是返回結(jié)果的后綴";
}
@Before("execution(* com.abc.service.*.many*(..))")
public void permissionCheck(JoinPoint point) {
System.out.println("@Before:模擬權(quán)限檢查...");
System.out.println("@Before:目標方法為:" +
point.getSignature().getDeclaringTypeName() +
"." + point.getSignature().getName());
System.out.println("@Before:參數(shù)為:" + Arrays.toString(point.getArgs()));
System.out.println("@Before:被織入的目標對象為:" + point.getTarget());
}
@AfterReturning(pointcut="execution(* com.abc.service.*.many*(..))",
returning="returnValue")
public void log(JoinPoint point, Object returnValue) {
System.out.println("@AfterReturning:模擬日志記錄功能...");
System.out.println("@AfterReturning:目標方法為:" +
point.getSignature().getDeclaringTypeName() +
"." + point.getSignature().getName());
System.out.println("@AfterReturning:參數(shù)為:" +
Arrays.toString(point.getArgs()));
System.out.println("@AfterReturning:返回值為:" + returnValue);
System.out.println("@AfterReturning:被織入的目標對象為:" + point.getTarget());
}
@After("execution(* com.abc.service.*.many*(..))")
public void releaseResource(JoinPoint point) {
System.out.println("@After:模擬釋放資源...");
System.out.println("@After:目標方法為:" +
point.getSignature().getDeclaringTypeName() +
"." + point.getSignature().getName());
System.out.println("@After:參數(shù)為:" + Arrays.toString(point.getArgs()));
System.out.println("@After:被織入的目標對象為:" + point.getTarget());
}
}
使用annotation代碼:
//注解實體類
package com.trip.demo;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface SMSAndMailSender {
/短信模板String格式化串/
String value() default "";
String smsContent() default "";
String mailContent() default "";
/*是否激活發(fā)送功能*/
boolean isActive() default true;
/*主題*/
String subject() default "";
}
//切面類
@Aspect
@Component("smsAndMailSenderMonitor")
public class SMSAndMailSenderMonitor {
private Logger logger = LoggerFactory.getLogger(SMSAndMailSenderMonitor.class);
/**
* 在所有標記了@SMSAndMailSender的方法中切入
* @param joinPoint
* @param result
*/
@AfterReturning(value="@annotation(com.trip.demo.SMSAndMailSender)", returning="result")//有注解標記的方法,執(zhí)行該后置返回
public void afterReturning(JoinPoint joinPoint , Object result//注解標注的方法返回值) {
MethodSignature ms = (MethodSignature) joinPoint.getSignature();
Method method = ms.getMethod();
boolean active = method.getAnnotation(SMSAndMailSender.class).isActive();
if (!active) {
return;
}
String smsContent = method.getAnnotation(SMSAndMailSender.class).smsContent();
String mailContent = method.getAnnotation(SMSAndMailSender.class).mailContent();
String subject = method.getAnnotation(SMSAndMailSender.class).subject();
}
/**
* 在拋出異常時使用
* @param joinPoint
* @param ex
*/
@AfterThrowing(value="@annotation(com.trip.order.monitor.SMSAndMailSender)",throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, Throwable ex//注解標注的方法拋出的異常) {
MethodSignature ms = (MethodSignature) joinPoint.getSignature();
Method method = ms.getMethod();
String subject = method.getAnnotation(SMSAndMailSender.class).subject();
}
}
//實體類中使用該注解標注方法
@Service("testService ")
public class TestService {
@Override
@SMSAndMailSender(smsContent = "MODEL_SUBMIT_SMS", mailContent =
"MODEL_SUPPLIER_EMAIL", subject = "MODEL_SUBJECT_EMAIL")
public String test(String param) {
return "success";
}
}
注意肩狂,記得在配置文件中加上:
<aop:aspectj-autoproxy proxy-target-class="true"/>
原文鏈接:https://blog.csdn.net/fz13768884254/article/details/83538709