Spring實現(xiàn)AOP的三種方式,你掌握了幾種?

①導(dǎo)入Jar
<dependencies><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.4</version></dependency></dependencies>
②配置文件開啟aop文件頭引用約束
<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"></beans>
③抽象接口
public interface UserService {void add();void delete();void update();void selete();}
④實現(xiàn)類
public class UserServiceImpl implements UserService {@Overridepublic void add() {System.out.println("增加用戶");}@Overridepublic void delete() {System.out.println("刪除用戶");}@Overridepublic void update() {System.out.println("更新用戶");}@Overridepublic void selete() {System.out.println("查詢用戶");}}
一、通過 Spring API 實現(xiàn)
①添加前置后置方法
public class AopBeforeConfigLog implements MethodBeforeAdvice {/*** method : 要執(zhí)行的目標(biāo)對象的方法* args : 被調(diào)用的方法的參數(shù)* target : 目標(biāo)對象*/@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass()+"===="+method.getName()+"被執(zhí)行了");}}
public class AopAfterConfigLog implements AfterReturningAdvice {/*** method : 要執(zhí)行的目標(biāo)對象的方法* args : 被調(diào)用的方法的參數(shù)* result : 返回值* target : 被調(diào)用的目標(biāo)對象*/@Overridepublic void afterReturning(Object result, Method method, Object[] args, Object target){System.out.println(target.getClass()+"===="+method.getName()+"被執(zhí)行了"+"===返回值"+result);}}
②beans.xml
<!--注冊bean--><bean id="userService" class="service.UserServiceImpl"/><bean id="afterLog" class="aoptest.AopAfterConfigLog"/><bean id="beforeAop" class="aoptest.AopBeforeConfigLog"/><aop:config><!--切入點 expression:表達(dá)式匹配要執(zhí)行的方法--><aop:pointcut id="cut" expression="execution(* service.UserServiceImpl.*(..))"/><!--執(zhí)行環(huán)繞; advice-ref執(zhí)行方法 . pointcut-ref切入點--><aop:advisor advice-ref="afterLog" pointcut-ref="cut"/><aop:advisor advice-ref="beforeAop" pointcut-ref="cut"/></aop:config>
public class TestAop {public static void main(String[] args) {ApplicationContext Context = new ClassPathXmlApplicationContext("ContextAplication.xml");UserService userService = (UserService) Context.getBean("userService");userService.add();userService.selete();userService.update();userService.delete();}}
⑤總結(jié):
切面:橫切的點
通知:切面完成的工作,其實就是一個方法
目標(biāo):被通知的對象
expression="execution(* service.UserServiceImpl.*(..))" 這是一套固定的公式 *代表所有 這句話的意思就是service.UserServiceImpl下的所有方法都被切面了!
二、自定義類來實現(xiàn)
①自定義被切入類
/** 自定義類* */public class MyDIYAopCut {public void before(){System.out.println("方法執(zhí)行前前前前前前前前前前");}public void after(){System.out.println("方法執(zhí)行后后后后后后后后后后");}}
②beans.xml
<!--注冊bean--><bean id="userService" class="service.UserServiceImpl"/><bean id="Mydiycut" class="diyaop.MyDIYAopCut"/><aop:config><!--這里的ref指定被 切入 的類是哪一個--><aop:aspect ref="Mydiycut"><!--切入點--><aop:pointcut id="cut" expression="execution(* service.UserServiceImpl.*(..))"/><!--切入點之前執(zhí)行,這里的方法名即是我們自定義類中的方法名--><aop:before method="before" pointcut-ref="cut"/><!--切入點之后執(zhí)行,這里的方法名即是我們自定義類中的方法名--><aop:before method="after" pointcut-ref="cut"/></aop:aspect></aop:config>
③測試類不變,執(zhí)行結(jié)果

④總結(jié):
測試類的方法即是xml中method的方法名

其他見xml中的注釋!
三、使用注解實現(xiàn)
①自定義增強(qiáng)注解實現(xiàn)類
public class MyAnnotationAopCut {("execution(* service.UserServiceImpl.*(..))")public void before(){System.out.println("方法執(zhí)行前");}("execution(* service.UserServiceImpl.*(..))")public void after(){System.out.println("方法執(zhí)行后");}("execution(* service.UserServiceImpl.*(..))")public void around(ProceedingJoinPoint point) throws Throwable {System.out.println("環(huán)繞前=="+point.getSignature());//執(zhí)行Object proceed = point.proceed();System.out.println("環(huán)繞后=="+point.getSignature());}}
②xml
<!--注冊bean--><bean id="userService" class="service.UserServiceImpl"/><bean id="myAnnotationAopCut" class="diyaop.MyAnnotationAopCut"/><!--聲明自動為spring容器中那些配置@aspectJ切面的bean創(chuàng)建代理,織入切面--><aop:aspectj-autoproxy proxy-target-class="false"/>
③測試類一致,執(zhí)行結(jié)果

-- end --
關(guān)注 Stephen,一起學(xué)習(xí),一起成長。
評論
圖片
表情
