【Spring】註解實現AOP
阿新 • • 發佈:2020-12-16
技術標籤:Spring學習筆記springaop
方式三:使用註解實現
<!-- 註冊bean -->
<bean id="userService" class="com.kudo.service.UserServiceImpl"/>
<!-- 方式三 -->
<bean id="annotationPointCut" class="com.kudo.diy.AnnotationPointCut"/>
<!--開啟註解支援-->
<aop:aspectj-autoproxy/>
//方法三:使用註解方式實現AOP
@Aspect //標註這個類是一個切面
public class AnnotationPointCut {
@Before("execution(* com.kudo.service.UserServiceImpl.*(..))")
public void before() {
System.out.println("===方法在執行前===");
}
@After("execution(* com.kudo.service.UserServiceImpl.*(..))" )
public void after() {
System.out.println("===方法在執行後===");
}
//在環繞增強中,我們可以給定一個引數,代表我們要處理切入的點
@Around("execution(* com.kudo.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("環繞前" );
//執行方法
Object proceed = jp.proceed();
System.out.println("環繞後");
// Signature signature = jp.getSignature();//獲得簽名
// System.out.println("signature:" + signature);
// System.out.println(proceed);
}
}
執行結果