javascript
Spring AOP注解方式实现
簡介
上文已經(jīng)提到了Spring AOP的概念以及簡單的靜態(tài)代理、動態(tài)代理簡單示例,鏈接地址:https://www.cnblogs.com/chenzhaoren/p/9959596.html
本文將介紹Spring AOP的常用注解以及注解形式實(shí)現(xiàn)動態(tài)代理的簡單示例。
常用注解
@aspect:定義切面
@pointcut:定義切點(diǎn)
@Before:前置通知,在方法執(zhí)行之前執(zhí)行
@After:后置通知,在方法執(zhí)行之后執(zhí)行?
@AfterRunning:返回通知,在方法返回結(jié)果之后執(zhí)行
@AfterThrowing:異常通知,在方法拋出異常之后執(zhí)行
@Around:環(huán)繞通知,圍繞著方法執(zhí)行
啟動Spring AOP注解自動代理
1. 在 classpath 下包含 AspectJ 類庫:aopalliance.jar、aspectj.weaver.jar 和 spring-aspects.jar
2. 將 aop Schema 添加到?Bean 配置文件?<beans> 根元素中。
3. 在 Bean 配置文件中定義一個(gè)空的 XML 元素 <aop:aspectj-autoproxy proxy-target-class="true"/>
(當(dāng) Spring IOC 容器檢測到 Bean配置文件中的<aop:aspectj-autoproxy proxy-target-class="true"/> 元素時(shí),會自動為與 AspectJ 切面匹配的 Bean 創(chuàng)建代理。)
代碼示例
1. 定義被代理類接口
packege com.czr.aop.model;public interface superMan{//自我介紹public void introduce(); }2. 定義被代理類
packege com.czr.aop.model;@Component("superMan") public class SuperManImpl implements SuperMan {@overridepublic void introduce(){System.out.println("我是內(nèi)褲外穿的超人!!!");}}
3. 定義切點(diǎn)類
package com.czr.aop;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut;@Component("annotationAop") @Aspect public class AnnotationAop {//定義切點(diǎn)@Pointcut("execution(* com.czr.aop.model.*(..))")public void pointCutName(){}@Before("pointCutName()")public void beforeAdvice(){System.out.println("*注解前置通知實(shí)現(xiàn)*");}//后置通知@After("pointCutName()")public void afterAdvice(){System.out.println("*注解后置通知實(shí)現(xiàn)*");}//環(huán)繞通知。ProceedingJoinPoint參數(shù)必須傳入。@Around("pointCutName()")public void aroudAdvice(ProceedingJoinPoint pjp) throws Throwable{System.out.println("*注解環(huán)繞通知實(shí)現(xiàn) - 環(huán)繞前*");pjp.proceed();//執(zhí)行方法System.out.println("*注解環(huán)繞通知實(shí)現(xiàn) - 環(huán)繞后*");} }4. 定義Spring文件aop.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:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.2.xsd"><!-- 開啟注解掃描 --><context:component-scan base-package="com.czr.aop,com.czr.aop.model"/><!-- 開啟aop注解方式 --><aop:aspectj-autoproxy/> </beans>5. 定義測試類
package com.czr.aop;public class Test {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml");SuperMan sm= (SuperMan)ctx.getBean("superMan");sm.introduce();}}?6. 輸出結(jié)果
*注解環(huán)繞通知實(shí)現(xiàn) - 環(huán)繞前* *注解前置通知實(shí)現(xiàn)* 我是內(nèi)褲外穿的超人!!! *注解環(huán)繞通知實(shí)現(xiàn) - 環(huán)繞后* *注解后置通知實(shí)現(xiàn)*?
轉(zhuǎn)載于:https://www.cnblogs.com/chenzhaoren/p/9961011.html
總結(jié)
以上是生活随笔為你收集整理的Spring AOP注解方式实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 芝麻信用负面记录多久消除?处理方式看这里
- 下一篇: 17种常用的JS正则表达式 非负浮点数