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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring_02_AOP初级总结

發(fā)布時間:2023/12/3 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring_02_AOP初级总结 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?1.AOP簡介

是對OOP編程方式的一種補(bǔ)充。翻譯過來為“面向切面編程”。

可以理解為一個攔截器框架,但是這個攔截器會非常武斷,如果它攔截一個類,那么它就會攔截這個類中的所有方法。如對一個目標(biāo)列的代理,增強(qiáng)了目標(biāo)類的所有方法。

兩個解決辦法:

1.不優(yōu)雅的做法:

在添加增強(qiáng)時,根據(jù)方法名去判斷,是否添加增強(qiáng),但是這樣就得一直去維護(hù)這個增強(qiáng)類。

2.面向切面:

將增強(qiáng)類和攔截條件組合在一起,然后將這個切面配置到 ProxyFactory 中,從而生成代理。

二、AOP 和 切面的關(guān)系

1.類比于 OOP 和 對象,AOP 和 切面就是這樣的一種關(guān)系。

2.也可以將 切面 看成是 AOP 的一個工具。

三、幾個概念

切面(Advisor):是AOP中的一個術(shù)語,表示從業(yè)務(wù)邏輯中分離出來的橫切邏輯,比如性能監(jiān)控,日志記錄,權(quán)限控制等。

這些功能都可以從核心的業(yè)務(wù)邏輯中抽離出去。可以解決代碼耦合問題,職責(zé)更加單一。封裝了增強(qiáng)和切點(diǎn)。

增強(qiáng)(Advice):增強(qiáng)代碼的功能的類,橫切到代碼中。

目標(biāo):目標(biāo)方法(JDK代理)或目標(biāo)類(CGLIB代理)

代理:JDK代理,CGLIB代理。或是通過 ProxyFactory 類生產(chǎn)。

切點(diǎn):通過一個條件來匹配要攔截的類,這個條件稱為切點(diǎn)。如攔截所有帶 Controller 注解的類。增強(qiáng)的條件。

連接點(diǎn):作為增強(qiáng)方法的入?yún)?#xff0c;可以獲取到目標(biāo)方法的信息。

四、概括為一張圖

五、增強(qiáng)

1.Weaving(織入):對方法進(jìn)行增強(qiáng)

(1)前置增強(qiáng)(BeforeAdvice):在目標(biāo)方法前調(diào)用。

(2)后置增強(qiáng)(AfterAdvice):在目標(biāo)方法后調(diào)用。

(3)環(huán)繞增強(qiáng)(AroundAdvice):將 Before 和 After ,甚至拋出增強(qiáng)和返回增強(qiáng)合到一起。

(4)返回增強(qiáng)(AfterReturningAdvice):在方法返回結(jié)果后執(zhí)行,該增強(qiáng)可以接收到目標(biāo)方法返回結(jié)果。

(5)拋出增強(qiáng)(AfterThrowingAdvice):在目標(biāo)方法拋出對應(yīng)的類型后執(zhí)行,可以接收到對應(yīng)的異常信息。

2.Introduction(引入):對類進(jìn)行增強(qiáng)

(1)引入增強(qiáng)(DeclareParentsAdvice):想讓程序在運(yùn)行的時候動態(tài)去實(shí)現(xiàn)某個接口,需要引入增強(qiáng)。

2.代碼實(shí)現(xiàn)?

2.1?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"><!-- xmlns:aop="http://www.springframework.org/schema/aop" 聲明要用aop的標(biāo)簽xsi:schemaLocation= aop的標(biāo)簽地址"http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" --><bean id="studentDao" class="org.jsoft.dao.impl.StudentDaoImpl"></bean><bean id="studentDaoByMybatis" lazy-init="true"class="org.jsoft.dao.impl.StudentDaoByMybatisImpl"></bean><bean id="studentService" class="org.jsoft.service.impl.StudentServiceImpl"scope="singleton" autowire="byName"></bean><!-- AOP面向切面的配置 --><!-- 切面對應(yīng)的類,即切面類 --><bean id="log" class="org.jsoft.log.Log" /> <!-- --> <!-- --><aop:config><aop:aspect id="myAspect" ref="log"><!-- 切面標(biāo)簽,id標(biāo)記名稱,ref對應(yīng)的切面類bean的id --><!-- 切點(diǎn)標(biāo)簽,id標(biāo)記名稱 --><aop:pointcut id="businessService"expression="execution(* org.jsoft.service..*.*(..))" /><!-- 在該范圍的類的所有方法下,執(zhí)行AOP方法 --><!-- 程序執(zhí)行前,要執(zhí)行的方法"before" --><aop:before pointcut-ref="businessService" method="before"/><!-- --><!-- 程序執(zhí)行后,要執(zhí)行的方法"after" --> <aop:after pointcut-ref="businessService" method="after"/><!-- 程序整個過程,要執(zhí)行的方法"around" --><aop:around pointcut-ref="businessService"method="around" /></aop:aspect></aop:config></beans>

2.2 對應(yīng)切面類Log

package org.jsoft.log;import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method;import org.aspectj.lang.ProceedingJoinPoint; //這是一個切面類,已放入IoC容器里. public class Log implements InvocationHandler{private Object obj;public void before(){//(1)前置增強(qiáng),需要實(shí)現(xiàn):MethodBeforeAdvice 接口System.out.println("Log.before()");}public void after(){//(2)后置增強(qiáng):實(shí)現(xiàn) AfterReturningAdvice 接口System.out.println("Log.after()");}//(3)環(huán)繞增強(qiáng):實(shí)現(xiàn) org.aopalliance.intercept.MethodInterceptor 接口,//使用 Object result = methodInvocation.proceed(); 調(diào)用目標(biāo)方法。在目標(biāo)方法前后添加增強(qiáng)public Object around(ProceedingJoinPoint pjp) throws Throwable{long l = System.currentTimeMillis();Object retVal = pjp.proceed();//new Thread().sleep(100);System.out.println(System.currentTimeMillis() - l);System.out.println("Log.around()");return retVal;}@Overridepublic Object invoke(Object proxy, Method method, Object[] args)throws Throwable {before();Object result = method.invoke(obj, args);after();return result;}public Object getObj() {return obj;}public void setObj(Object obj) {this.obj = obj;} }

2.3 main測試

import org.springframework.context.support.ClassPathXmlApplicationContext;/**ApplicationContext 是接口,下面的ClassPathXmlApplicationContext也可以用,后者可以用.close()方法*/ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"spring/spring.xml"}); Student s = new Student(); s.setName("Joy"); Log log = new Log(); log.setObj(studentService); //動態(tài)代理 //參數(shù)1 classLoader類加載器 //interfaces造出來的類要實(shí)現(xiàn)那些接口 IStudentService studentServiceProxy = (IStudentService)Proxy.newProxyInstance(Main.class.getClassLoader(), studentService.getClass().getInterfaces(), log);studentServiceProxy.add(s); context.close();

?

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結(jié)

以上是生活随笔為你收集整理的Spring_02_AOP初级总结的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。