<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] # AOP AOP 即 `Aspect Oriented Program` 面向切面編程 首先,在面向切面編程的思想里面,把功能分為**核心業務功能**,和**周邊功能**。 所謂的核心業務,比如登陸,增加數據,刪除數據都叫核心業務 所謂的周邊功能,比如性能統計,日志,事務管理等等 周邊功能在Spring的面向切面編程AOP思想里,被定義為**切面** 在面向切面編程AOP的思想里面,核心業務功能和切面功能分別**獨立進行開發** 然后把切面功能和核心業務功能 "**編織**" 在一起,這就叫AOP ## 步驟 1 : 先運行,看到效果,再學習 先將完整的 spring 項目(向老師要相關資料),配置運行起來,確認可用之后,再學習做了哪些步驟以達到這樣的效果。 ## 步驟 2 : 模仿和排錯 在確保可運行項目能夠正確無誤地運行之后,再嚴格照著教程的步驟,對代碼模仿一遍。 模仿過程難免代碼有出入,導致無法得到期望的運行結果,此時此刻通過比較**正確答案** ( 可運行項目 ) 和自己的代碼,來定位問題所在。 采用這種方式,**學習有效果,排錯有效率**,可以較為明顯地提升學習速度,跨過學習路上的各個檻。 ## 步驟 3 : 思路圖 1. 功能分兩大類,輔助功能和核心業務功能; 2. 輔助功能和核心業務功能**彼此獨立**進行開發; 3. 比如登陸功能,即便是沒有性能統計和日志輸出,也可以正常運行; 4. 如果有需要,就把"日志輸出" 功能和 "登陸" 功能 ****編織****在一起,這樣登陸的時候,就可以看到日志輸出了; 5. 輔助功能,又叫做**切面**,這種能夠**選擇性的**,**低耦合的**把切面和核心業務功能結合在一起的編程思想,就叫做切面編程。 ![](https://box.kancloud.cn/c6a8d939743fccee5a05b31a7f456890_960x609.png) ## 步驟 4 : 準備業務類 ProductService ProductService ~~~ package com.dodoke.service; public class ProductService { public void doSomeService() { System.out.println("doSomeService"); } } ~~~ ## 步驟 5 : 聲明業務對象 在applicationContext.xml中,添加ProductService在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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.dodoke.pojo" /> <bean name="s" class="com.dodoke.service.ProductService"/> </beans> ~~~ ## 步驟 6 : TestSpring 在引入切面之前,調用該業務類 ~~~ package com.dodoke.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dodoke.service.ProductService; public class TestSpring { public static void main(String[] args) { // 讀取applicationContext.xml配置文件 ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" }); ProductService s = (ProductService)context.getBean("s"); s.doSomeService(); } } ~~~ 運行結果: ![](https://box.kancloud.cn/cfc908c3664003ffc7893bd5a2057338_468x186.png) ## 步驟 7 : 準備日志切面 LoggerAspect 該日志切面的功能是 在調用核心功能之前和之后分別打印日志,切面就是原理圖中講的那些輔助功能。 ~~~ package com.dodoke.aspect; import org.aspectj.lang.ProceedingJoinPoint; public class LoggerAspect { public Object log(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("start log:" + joinPoint.getSignature().getName()); Object object = joinPoint.proceed(); System.out.println("end log:" + joinPoint.getSignature().getName()); return object; } } ~~~ > `getSignature()`: 獲取切點的簽名 > `proceed()` :執行切點本來的業務 ## 步驟 8 : applicationContext.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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.dodoke.pojo" /> <!-- 聲明業務對象 --> <bean name="s" class="com.dodoke.service.ProductService" /> <!-- 聲明日志切面 --> <bean id="loggerAspect" class="com.dodoke.aspect.LoggerAspect" /> <aop:config> <aop:pointcut expression="execution(* com.dodoke.service.ProductService.*(..))" id="loggerCutpoint" /> <aop:aspect id="logAspect" ref="loggerAspect"> <aop:around method="log" pointcut-ref="loggerCutpoint" /> </aop:aspect> </aop:config> </beans> ~~~ 1. 聲明日志切面 ~~~ <bean id="loggerAspect" class="com.dodoke.aspect.LoggerAspect" /> ~~~ 2. 結合思路圖,配置AOP 指定右邊的核心業務功能: ~~~ <aop:pointcut expression="execution(* com.dodoke.service.ProductService.*(..))" id="loggerCutpoint" /> ~~~ > 這一句是聲明切入點,切入點的 id 叫 loggerCutPoint ,用來標記這個切入點, > 這個expression表示:滿足expression中的方法調用之后,就會去進行切面操作,類似于觸發了切面: > `*` :返回任意類型 > `com.dodoke.service.ProductService.*`:包名以 `com.dodoke.service.ProductService` 開頭的類的任意方法 > `(..) `:參數是任意數量和類型 簡單說就是,只要`com.dodoke.service`這個包中的ProductService類的任意一個函數被調用,不管你的返回值是什么,都會觸發開關,我就會去執行切面,也就是輔助功能,但是輔助功能是什么呢,就是下面兩句: 指定左邊的輔助功能 ~~~ <aop:aspect id="logAspect" ref="loggerAspect"> <aop:around method="log" pointcut-ref="loggerCutpoint" /> </aop:aspect> ~~~ > 這兩句是定義了一個切面,上面說只要觸發開關,就會去執行切面,就是指的這里的切面,所謂切面,就是一個類中的方法而已。 > id代表這個切面的名字,ref就是指的方法所在的類,method代表的就是方法的名字,`pointcut-ref="loggerCutpoint" `這個就是表示我這個切面是和上面的切點關聯起來的(一個切點是可以關聯多個切面的,一個切面只能關聯一個方法),只要上面的切點被觸發,我就會到這里來執行一些輔助功能。 然后通過`aop:config`把業務對象與輔助功能編織在一起。 ## 步驟 9 : TestSpring TestSpring 代碼沒有發生任何變化,通過配置的方式,把切面和核心業務類編制在了一起。 運行測試,可以發現在編織之后,業務方法運行之前和之后分別會打印日志。 運行結果: ![](https://box.kancloud.cn/9ad28292422ccc7ab029b9b94a26c15f_426x179.png) ## 步驟 10 : 練習-性能統計切面 參考上述的日志切面,做一個性能統計切面,并編織在業務方法上面 注:在業務方法方法中,做循環,以增加耗時 參考: 核心業務里面就搞個費時的操作。 ProductService: ~~~ package com.dodoke.service; public class ProductService { public void doSomeService() { String str = ""; for (int i = 0; i < 1000; i++) { str += i; } System.out.println("doSomeService1:" + str); } } ~~~ LoggerAspect: ~~~ package com.dodoke.aspect; import org.aspectj.lang.ProceedingJoinPoint; public class LoggerAspect { public Object log(ProceedingJoinPoint joinPoint) throws Throwable { System.out.println("start log:" + joinPoint.getSignature().getName()); // 運行核心業務類之前的時間 long startTime = System.currentTimeMillis(); Object object = joinPoint.proceed(); System.out.println("end log:" + joinPoint.getSignature().getName()); // 運行核心業務類之后的時間 long endTime = System.currentTimeMillis(); // 總運行時間 long time = endTime - startTime; System.out.println("耗費時間:" + time); return object; } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看