国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

SpringAOP 設置注入的實(shí)現步驟

發(fā)布時(shí)間:2021-07-17 21:51 來(lái)源:腳本之家 閱讀:0 作者:Lachlan_Yang 欄目: 編程語(yǔ)言 歡迎投稿:712375056

目錄

      AOP_面向切面編程初步了解

      讓我們先想象一個(gè)場(chǎng)景,你正在編寫(xiě)一個(gè)項目,在開(kāi)發(fā)過(guò)程中的多個(gè)模塊都有某段重復的代碼,于是你選擇將其抽象成一個(gè)方法,然后在需要的地方調用這個(gè)方法,當需要修改這段代碼時(shí)只需要修改這個(gè)方法就行。有一天,你的Boss給了新的需求,需要再抽象出一個(gè)方法,然后再在各個(gè)需要這個(gè)方法的模塊調用這個(gè)方法,這可能就讓你頭疼了,需要修改大量的代碼,于是會(huì )想,能不能不修改源代碼為系統業(yè)務(wù)添加某種功能呢?幸運的是,AOP可以很好的解決這個(gè)問(wèn)題。

      簡(jiǎn)單介紹

      AOP:保證開(kāi)發(fā)者不修改源代碼的前提下,去為系統中的業(yè)務(wù)組件添加某種通用功能,本質(zhì)是由AOP框架修改業(yè)務(wù)組件的多個(gè)方法的源代碼,我們將其分為兩類(lèi):

      • 靜態(tài)AOP

      AOP 框架在編譯階段對程序源代碼進(jìn)行修改,生成了靜態(tài)的 AOP 代理類(lèi)(生成的*.class文件已經(jīng)被改掉了,需要使用特定的編譯器),比如 AspectJ。

      • 動(dòng)態(tài)AOP:

      AOP 框架在運行階段對動(dòng)態(tài)生成代理對象(在內存中以 JDK 動(dòng)態(tài)代理,或 CGlib 動(dòng)態(tài)地生成 AOP 代理類(lèi)),如 SpringAOP。

      詳細說(shuō)明

      Spring 的通知類(lèi)型

      實(shí)戰演練

      導入依賴(lài)包

          <dependencies>
              <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-context</artifactId>
                  <version>5.3.5</version>
              </dependency>
              <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
              <dependency>
                  <groupId>org.springframework</groupId>
                  <artifactId>spring-aspects</artifactId>
                  <version>5.3.5</version>
              </dependency>
              <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
              <dependency>
                  <groupId>org.aspectj</groupId>
                  <artifactId>aspectjweaver</artifactId>
                  <version>1.9.6</version>
              </dependency>
              <!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
              <dependency>
                  <groupId>aopalliance</groupId>
                  <artifactId>aopalliance</artifactId>
                  <version>1.0</version>
              </dependency>
              <dependency>
                  <groupId>junit</groupId>
                  <artifactId>junit</artifactId>
                  <version>4.12</version>
                  <scope>test</scope>
              </dependency>
          </dependencies>
      

      創(chuàng )建一個(gè)增強類(lèi)以及其接口

      增強類(lèi)接口:

      public interface VisitService {
          //用于實(shí)現前置通知,后置通知,異常通知,最終通知
          void visit(String str) throws Exception;
      
          //用于實(shí)現環(huán)繞通知
          void around();
      }
      

      增強類(lèi):

      public class VisitServiceImpl implements VisitService {
          //前置,后置,最終,異常通知的增強類(lèi)
          public void visit(String str) throws Exception{
              System.out.println(str);
              if(!str.equalsIgnoreCase("agree")){
                  throw new Exception("非法訪(fǎng)問(wèn)");
              }
          }
          //環(huán)繞通知的增強類(lèi)
          public void around() {
              System.out.println("環(huán)繞通知");
          }
      }

      創(chuàng )建一個(gè)切面類(lèi)

      public class VisitAspect {
          //前置通知
          public void visitBefore(JoinPoint joinPoint){
              System.out.println("口令:");
          }
          //最終通知,無(wú)論是否報錯,都執行
          public void visitAfter(JoinPoint joinPoint){
              System.out.println("輸入完成");
          }
          //后置通知報錯不執行
          public void visitSuccess(JoinPoint joinPoint){
              System.out.println("請求成功,歡迎");
          }
          //異常通知,報錯后執行
          public void visitThrow(JoinPoint joinPoint, Throwable ex){
              System.out.println("請求失敗,拒絕");
          }
          //環(huán)繞通知,如果報錯只執行前一句
          public Object visitAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
              System.out.println("-------環(huán)繞-------");
              Object obj = proceedingJoinPoint.proceed();
              System.out.println("-------環(huán)繞-------");
              return obj;
          }
      }

      配置xml文件,需要添加第三方約束

          <bean id="userDao" class="Spring_AOP.service.impl.VisitServiceImpl"></bean>
          <bean id="aspect" class="Spring_AOP.service.VisitAspect"></bean>
          
          <aop:config>
              <aop:pointcut id="pointcut" expression="execution(* Spring_AOP.service.impl.VisitServiceImpl.visit(..))"/>
              <aop:pointcut id="pointcut1" expression="execution(* Spring_AOP.service.impl.VisitServiceImpl.around())"/>
              <aop:aspect ref="aspect">
                   <aop:before method="visitBefore" pointcut-ref="pointcut"></aop:before>
                   <aop:after method="visitAfter" pointcut-ref="pointcut"></aop:after>
                   <aop:after-returning method="visitSuccess" pointcut-ref="pointcut"></aop:after-returning>
                   <aop:around method="visitAround" pointcut-ref="pointcut1"></aop:around>
                   <!-- 報錯后執行aop:after-throwing -->
                   <aop:after-throwing method="visitThrow" pointcut-ref="pointcut" throwing="ex"></aop:after-throwing>
               </aop:aspect>
          </aop:config>

      注,對于execution()
      1、execution(): 表達式主體 (必須加上execution)。
      2、第一個(gè)* 號:表示返回值類(lèi)型,* 號表示所有的類(lèi)型。
      3、包名:表示需要攔截的包名,后面的兩個(gè)句點(diǎn)表示當前包和當前包的所有子包,cn.smd.service.impl包、子孫包下所有類(lèi)的方法。
      4、第二個(gè)* 號:表示類(lèi)名,* 號表示所有的類(lèi)。
      5、* (..):最后這個(gè)星號表示方法名,* 號表示所有的方法,后面括弧里面表示方法的參數,兩個(gè)句點(diǎn)表示任何參數。
      書(shū)寫(xiě)的注意事項:execution(* cn.smd.service.impl..(..))

      創(chuàng )建一個(gè)測試類(lèi)

       public class visitTest {
          @Test
          public void VisitTest(){
              ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext_AOP.xml");
              VisitService visitService = app.getBean(VisitService.class);
              try {
                  visitService.visit("agree");
              } catch (Exception e) {
                  e.printStackTrace();
              }
              try {
                  visitService.visit("ok");
              } catch (Exception e) {
                  e.printStackTrace();
              }
              visitService.around();
          }
      }

      測試運行

      口令:
      agree
      請求成功,歡迎
      輸入完成
      口令:
      ok
      請求失敗,拒絕
      輸入完成
      -------環(huán)繞-------
      環(huán)繞通知
      -------環(huán)繞-------

      總結

      • SpringAOP進(jìn)一步降低組件的耦合,實(shí)現解耦合
      • 可以更好的監控程序,進(jìn)行權限攔截
      • 注:學(xué)習AOP設置注入時(shí)需要注意出現報錯時(shí)各個(gè)通知的狀態(tài)

      以上就是SpringAOP 設置注入的實(shí)現步驟的詳細內容,更多關(guān)于SpringAOP 設置注入的資料請關(guān)注腳本之家其它相關(guān)文章!

      免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。

      久久国产免费观看精品3| 亚洲GV白嫩小受在线观看| 天天天天做夜夜夜夜做无码| 亚洲午夜未满十八勿入网站2| 国产高潮流白浆视频| 久久久久国产一区二区|