AOP基本概念
- 切面(
Aspect
) :通知和切入點(diǎn)共同組成了切面粘昨,時(shí)間、地點(diǎn)和要發(fā)生的“故事”。 - 連接點(diǎn)(
Joinpoint
) :程序能夠應(yīng)用通知的一個(gè)“時(shí)機(jī)”,這些“時(shí)機(jī)”就是連接點(diǎn)音比,例如方法被調(diào)用時(shí)、異常被拋出時(shí)等等氢惋。 - 通知(
Advice
) :通知定義了切面是什么以及何時(shí)使用。描述了切面要完成的工作和何時(shí)需要執(zhí)行這個(gè)工作稽犁。 - 切入點(diǎn)(
Pointcut
) :通知定義了切面要發(fā)生的“故事”和時(shí)間焰望,那么切入點(diǎn)就定義了“故事”發(fā)生的地點(diǎn),例如某個(gè)類或方法的名稱已亥。 - 目標(biāo)對象(
Target Object
) :即被通知的對象熊赖。
AOP代理(AOP Proxy) 在Spring AOP中有兩種代理方式,JDK動態(tài)代理和CGLIB代理虑椎。默認(rèn)情況下震鹉,TargetObject實(shí)現(xiàn)了接口時(shí),則采用JDK動態(tài)代理捆姜;反之传趾,采用CGLIB代理。
織入(Weaving)把切面應(yīng)用到目標(biāo)對象來創(chuàng)建新的代理對象的過程泥技,織入一般發(fā)生在如下幾個(gè)時(shí)機(jī):
1)編譯時(shí):當(dāng)一個(gè)類文件被編譯時(shí)進(jìn)行織入浆兰,這需要特殊的編譯器才能做到,例如AspectJ的織入編譯器珊豹;
2)類加載時(shí):使用特殊的ClassLoader在目標(biāo)類被加載到程序之前增強(qiáng)類的字節(jié)代碼簸呈;
3)運(yùn)行時(shí):切面在運(yùn)行的某個(gè)時(shí)刻被織入,SpringAOP就是以這種方式織入切面的店茶,原理是使用了JDK的動態(tài)代理蜕便。
AOP通知類型
@Before
前置通知(Before advice) :在某連接點(diǎn)(JoinPoint
)之前執(zhí)行的通知,但這個(gè)通知不能阻止連接點(diǎn)前的執(zhí)行贩幻。
@After
后通知(After advice) :當(dāng)某連接點(diǎn)退出的時(shí)候執(zhí)行的通知(不論是正常返回還是異常退出)轿腺。
@AfterReturning
返回后通知(After return advice) :在某連接點(diǎn)正常完成后執(zhí)行的通知,不包括拋出異常的情況丛楚。
@Around
環(huán)繞通知(Around advice) :包圍一個(gè)連接點(diǎn)的通知吃溅,類似Web中Servlet規(guī)范中的Filter的doFilter方法⊙烀剩可以在方法的調(diào)用前后完成自定義的行為决侈,也可以選擇不執(zhí)行。
@AfterThrowing
拋出異常后通知(After throwing advice) : 在方法拋出異常退出時(shí)執(zhí)行的通知。
自定義Spring注解實(shí)現(xiàn)
定義注解有好幾種實(shí)現(xiàn)方式赖歌,有使用aop實(shí)現(xiàn)的枉圃,也有使用攔截器實(shí)現(xiàn)的,實(shí)現(xiàn)的話有可能各自有的優(yōu)缺點(diǎn)庐冯,不過總的來說孽亲,還是推薦使用aop方式實(shí)現(xiàn)。
定義注解類
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.itaem.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 說明:
* 定義日志注解
*
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FileLog {
String value() default "日志記錄開始";
}
定義注解的處理程序
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.itaem.aspect;
import net.itaem.annotation.FileLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
* 說明:
* 日志的切面處理程序
*
*/
@Aspect
@Component
public class LogAspect {
@AfterReturning("within(net.itaem..*) && @annotation(fl)")
public void addSuccessLog(JoinPoint jp, FileLog fl){
Object[] parames = jp.getArgs();
//獲取目標(biāo)方法體參數(shù)
String className = jp.getTarget().getClass().toString();
//獲取目標(biāo)類名
String signature = jp.getSignature().toString();
//獲取目標(biāo)方法簽名
String methodName = signature.substring(signature.lastIndexOf(".")+1, signature.indexOf("("));
//獲取注解值
String desc=fl.value();
//把調(diào)用的信息寫到日常記錄信息里面去...
}
//標(biāo)注該方法體為異常通知展父,當(dāng)目標(biāo)方法出現(xiàn)異常時(shí)返劲,執(zhí)行該方法體
@AfterThrowing(pointcut="within(net.itaem..*) && @annotation(fl)", throwing="e")
public void addExceptionLog(JoinPoint jp, FileLog fl, Exception e){
//把錯(cuò)誤信息寫到錯(cuò)誤日志文件里面去...
}
}
編寫測試用例
配置spring啟動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/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">
<aop:aspectj-autoproxy/>
<bean name="purchaseService" class="net.itaem.test.PurchaseService" />
<bean class="net.itaem.aspect.LogAspect"/>
</beans>
編寫service類
public class PurchaseService {
public void purchaseProduct(String productName,int price,String type) {
System.out.println("購買商品栖茉。篮绿。。");
}
}
編寫測試代碼
package net.itaem.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
@org.junit.Test
public void test() {
ApplicationContext context=new ClassPathXmlApplicationContext("net/itaem/source/test.xml");
PurchaseService service=(PurchaseService) context.getBean("purchaseService");
service.purchaseProduct("電風(fēng)扇", 98, "日用品");
}
}
結(jié)果展示
總結(jié)
個(gè)人覺得這個(gè)實(shí)現(xiàn)起來比較簡答吕漂, 也比較容易理解亲配,當(dāng)然其他的更深入的擴(kuò)展,還是要慢慢的去學(xué)習(xí)惶凝,探索吼虎,這里只是簡單的做了一個(gè)實(shí)現(xiàn)注解的方式介紹。其實(shí)學(xué)習(xí)的這種狀態(tài)挺快樂的苍鲜,無形中有一種自己又升華了的感覺思灰,好好加油吧~