日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AOP****

發(fā)布時間:2024/1/18 编程问答 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AOP**** 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AOPAspect Oriented Programming的縮寫,意為:面向切面編程通過預編譯方式和運行期間動態(tài)代理實現程序功能的統(tǒng)一維護的一種技術
AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生范型。利用AOP可以對業(yè)務邏輯的各個部分進行隔離,從而使得業(yè)務邏輯各部分之間的耦合度降低提高程序的可重用性,同時提高了開發(fā)的效率

主要功能

日志記錄
性能統(tǒng)計
安全控制
事務處理
異常處理

主要意圖

  • 將日志記錄,性能統(tǒng)計,安全控制,事務處理,異常處理等代碼從業(yè)務邏輯代碼中劃分出來,通過對這些行為的分離,我們希望可以將它們獨立到非指導業(yè)務邏輯的方法中,進而改變這些行為的時候不影響業(yè)務邏輯的代碼。

實現

提供聲明事務允許用戶自定義切面

  • AOP 是一個概念,一個規(guī)范,本身并沒有設定具體語言的實現,這實際上提供了非常廣闊的發(fā)展的空間。AspectJ是AOP的一個很悠久的實現,它能夠和 Java 配合起來使用。
  • Aspect(切點): Aspect 聲明類似于 Java 中的類聲明,在 Aspect 中會包含著一些 Pointcut 以及相應的 Advice。
  • Joint point(連接點):表示在程序中明確定義的點,典型的包括方法調用,對類成員的訪問以及異常處理程序塊的執(zhí)行等等,它自身還可以嵌套其它 joint point。
  • Pointcut(切入點):表示一組 joint point,這些 joint point 或是通過邏輯關系組合起來,或是通過通配、正則表達式等方式集中起來,它定義了相應的 Advice 將要發(fā)生的地方。
  • Advice(通知):Advice 定義了在 pointcut 里面定義的程序點具體要做的操作,它通過 before、after 和 around 來區(qū)別是在每個 joint point 之前、之后還是代替執(zhí)行的代碼。
    下面要討論的這些問題,也許正是接觸了 AOP 之后所困惑的。
    AOP 幫助我們解決了新的問題沒有?
    AOP 并沒有幫助我們解決任何新的問題,它只是提供了一種更好的辦法,能夠用更少的工作量來解決現有的一些問題,并且使得系統(tǒng)更加健壯,可維護性更好。同時,它讓我們在進行系統(tǒng)架構和模塊設計的時候多了新的選擇和新的思路。
    先導包
    ①使用原生Spring API接口實現AOP
<dependencies><dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.4</version></dependency></dependencies> public interface UserService {public void add();public void update();public void delete();public void query(); } public class UserServiceImpl implements UserService {public void add() {System.out.println("增加了一個用戶 ");}public void update() {System.out.println("更新了一個用戶 ");}public void delete() {System.out.println("刪除了一個用戶 ");}public void query() {System.out.println("查詢用戶");} } package com.xmm.log;import org.springframework.aop.AfterAdvice; import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;/*** @Description TODO* @Author Xm* @Date 2022/5/23 16:43*/public class AfterLog implements AfterReturningAdvice {//returnValue返回值public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println("執(zhí)行了"+method.getName()+"方法,返回結果為"+returnValue);} } package com.xmm.log;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class Log implements MethodBeforeAdvice {//method 要執(zhí)行的目標對象的方法//argS:參數//target:目標對象public void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName()+"的" +method.getName()+"被執(zhí)行了");} } <?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/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttps://www.springframework.org/schema/aop/spring-aop.xsd"><!-- -&#45;&#45;P命名空間注入,可以直接注入屬性的值:property--><!-- 注冊bean--><bean id="userService" class="com.xmm.service.UserServiceImpl"/><bean id="log" class="com.xmm.log.Log"/><bean id="afterLog" class="com.xmm.log.AfterLog"/> <!-- 使用原生Spring API接口--> <!-- 配置aop--><aop:config> <!-- 需要一個切入點 expression 表達式 - execution(要執(zhí)行的位置)--><aop:pointcut id="pointcut" expression="execution(* com.xmm.service.UserServiceImpl.*(..))"/><!-- 執(zhí)行環(huán)繞增加--><aop:advisor advice-ref="log" pointcut-ref="pointcut"/><aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/></aop:config> </beans>

②自定義類

public class DiyPointCut {public void before(){System.out.println("===============方法執(zhí)行前");}public void after(){System.out.println("===============方法執(zhí)行后");} } <bean id="diy" class="com.xmm.diy.DiyPointCut"/><aop:config> <!-- 自定義類切面 ref要引用的類--><aop:aspect ref="diy"> <!-- 切入點--><aop:pointcut id="point" expression="execution(* com.xmm.service.UserServiceImpl.*(..))"/> <!-- 通知--><aop:before method="before" pointcut-ref="point"/><aop:after method="after" pointcut-ref="point"/></aop:aspect></aop:config>

③使用注解實現aop

<bean id="userService" class="com.xmm.service.UserServiceImpl"/><bean id="log" class="com.xmm.log.Log"/><bean id="annotationPointCut" class="com.xmm.diy.AnnotationPointCut"/> <!-- 開啟注解支持--><aop:aspectj-autoproxy/> package com.xmm.diy;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before;//使用注解實現Aop@Aspect//標注這個類是一個切面 public class AnnotationPointCut {@Before("execution(* com.xmm.service.UserServiceImpl.*(..))")public void before() {System.out.println("方法執(zhí)行前");}@After("execution(* com.xmm.service.UserServiceImpl.*(..))")public void after() {System.out.println("方法執(zhí)行后");}//在環(huán)繞增強中,可以給定一個參數,代表我們要獲取處理切入的點:@Around("execution(* com.xmm.service.UserServiceImpl.*(..))")public void around(ProceedingJoinPoint jp) throws Throwable {//ProceedingJoinPoint連接點System.out.println("環(huán)繞前");Signature signature = jp.getSignature();//獲得簽名System.out.println("signature :"+signature);//執(zhí)行方法Object proceed = jp.proceed();System.out.println("環(huán)繞后");System.out.println(proceed);} }

先執(zhí)行環(huán)繞前
在執(zhí)行執(zhí)行前
執(zhí)行方法
執(zhí)行環(huán)繞后
方法執(zhí)行后

==JDK(默認)proxy-target-class=“false” ==
cglib(proxy-target-class=“true”)

<!-- 開啟注解支持 JDK(默認)proxy-target-class="false" cglib(proxy-target-class="true")--><aop:aspectj-autoproxy proxy-target-class="false"/>

總結

以上是生活随笔為你收集整理的AOP****的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。