javascript
Spring学习(八)AOP详解
本文借鑒:Spring學(xué)習
一、一個例子
?
在上面的例子中,包租婆的核心業(yè)務(wù)就是簽合同,收房租,那么這就夠了,灰色框起來的部分都是重復(fù)且邊緣的事,交給中介商就好了,這就是 AOP 的一個思想:讓關(guān)注點代碼與業(yè)務(wù)代碼分離!
例子解析
1.在 Package【service】下新建一個【LandlordService】類(核心業(yè)務(wù))
/*** 包租婆關(guān)心的業(yè)務(wù)~(模擬核心業(yè)務(wù))*/ @Component("landlord") public class LandlordService {public void service() {// 僅僅只是實現(xiàn)了核心的業(yè)務(wù)功能System.out.println("簽合同");System.out.println("收房租");} }2.在 Package【aspect】下新建一個中介商【BrokerAspect】類(周邊功能)
/*** 包租婆不關(guān)心~* 中介關(guān)心的業(yè)務(wù)~(模擬周邊功能)*/ @Component @Aspect class BrokerAspect {@Before("execution(* service.LandlordService.service())")public void before() {System.out.println("帶租客看房");System.out.println("談價格");}@After("execution(* service.LandlordService.service())")public void after() {System.out.println("交鑰匙");} }3.在?applicationContext.xml 中配置自動注入,并告訴 Spring IoC 容器去哪里掃描這兩個 Bean
<?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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><context:component-scan base-package="aspect"/><context:component-scan base-package="service"/><!--聲明自動為spring容器中那些配置@Aspect切面的bean創(chuàng)建代理,織入切面。--><aop:aspectj-autoproxy/></beans>4.編寫測試類
public class TestSpring {@Testpublic void test() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");LandlordService landlord = (LandlordService) context.getBean("landlord", LandlordService.class);landlord.service();} }5.查看執(zhí)行結(jié)果
二、使用注解來開發(fā) Spring AOP
第一步:選擇連接點
Spring 是方法級別的 AOP 框架,我們主要也是以某個類額某個方法作為連接點,另一種說法就是:選擇哪一個類的哪一方法用以增強功能。
....public void service() {// 僅僅只是實現(xiàn)了核心的業(yè)務(wù)功能System.out.println("簽合同");System.out.println("收房租");}....這里就選擇上述 LandlordService 類中的 service() 方法作為連接點。
第二步:創(chuàng)建切面
選擇好了連接點就可以創(chuàng)建切面了,我們可以把切面理解為一個攔截器,當程序運行到連接點的時候,被攔截下來,在開頭加入了初始化的方法,在結(jié)尾也加入了銷毀的方法而已,在 Spring 中只要使用 @Aspect 注解一個類,那么 Spring IoC 容器就會認為這是一個切面了:
/*** 包租婆不關(guān)心~* 中介關(guān)心的業(yè)務(wù)~(模擬周邊功能)*/ @Component @Aspect class BrokerAspect {@Before("execution(* service.LandlordService.service())")public void before() {System.out.println("帶租客看房");System.out.println("談價格");}@After("execution(* service.LandlordService.service())")public void after() {System.out.println("交鑰匙");} }PS: 被定義為切面的類仍然是一個 Bean ,需要 @Component 注解標注
第三步:定義切點
在上面的注解中定義了 execution 的正則表達式,Spring 通過這個正則表達式判斷具體要攔截的是哪一個類的哪一個方法:
execution(* service.LandlordService.service())依次對這個表達式作出分析:
- execution:代表執(zhí)行方法的時候會觸發(fā)
- * :代表任意返回類型的方法
- service.LandlordService:代表類的全限定名
- service():被攔截的方法名稱
第四步:測試 AOP
我們來探討一下環(huán)繞通知,這是 Spring AOP 中最強大的通知,因為它集成了前置通知和后置通知,它保留了連接點原有的方法的功能,所以它及強大又靈活,讓我們來看看:
/*** 包租婆不關(guān)心~* 中介關(guān)心的業(yè)務(wù)~(模擬周邊功能)*/ @Component @Aspect class BrokerAspect {// 注釋掉之前的 @Before 和 @After 注解以及對應(yīng)的方法 // @Before("execution(* service.LandlordService.service())") // public void before() { // System.out.println("帶租客看房"); // System.out.println("談價格"); // } // // @After("execution(* service.LandlordService.service())") // public void after() { // System.out.println("交鑰匙"); // }// 使用 @Around 注解來同時完成前置和后置通知@Around("execution(* service.LandlordService.service())")public void around(ProceedingJoinPoint joinPoint) {System.out.println("帶租客看房");System.out.println("談價格");try {joinPoint.proceed();} catch (Throwable throwable) {throwable.printStackTrace();}System.out.println("交鑰匙");} }運行測試代碼,結(jié)果仍然正確:
三、使用 XML 配置開發(fā) Spring AOP
注解是很強大的東西,但基于 XML 的開發(fā)我們?nèi)匀恍枰私?#xff0c;我們先來了解一下 AOP 中可以配置的元素:
有了之前通過注解來編寫的經(jīng)驗,并且有了上面的表,我們將上面的例子改寫成 XML 配置很容易(去掉所有的注解):
<!-- 裝配 Bean--> <bean name="landlord" class="pojo.Landlord"/> <bean id="broker" class="aspect.Broker"/><!-- 配置AOP --> <aop:config><!-- where:在哪些地方(包.類.方法)做增加 --><aop:pointcut id="landlordPoint"expression="execution(* pojo.Landlord.service())"/><!-- what:做什么增強 --><aop:aspect id="logAspect" ref="broker"><!-- when:在什么時機(方法前/后/前后) --><aop:around pointcut-ref="landlordPoint" method="around"/></aop:aspect> </aop:config>運行測試程序,看到正確結(jié)果:
?
轉(zhuǎn)載于:https://www.cnblogs.com/riches/p/11558164.html
總結(jié)
以上是生活随笔為你收集整理的Spring学习(八)AOP详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cannot be found on o
- 下一篇: Spring学习(九)Spring 和数