AOP核心概念

阿克陶ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
1、橫切關(guān)注點(diǎn)
對(duì)哪些方法進(jìn)行攔截,攔截后怎么處理,這些關(guān)注點(diǎn)稱之為橫切關(guān)注點(diǎn)
2、切面(aspect)
類是對(duì)物體特征的抽象,切面就是對(duì)橫切關(guān)注點(diǎn)的抽象
3、連接點(diǎn)(joinpoint)
被攔截到的點(diǎn),因?yàn)閟pring只支持方法類型的連接點(diǎn),所以在Spring中連接點(diǎn)指的就是被攔截到的方法,實(shí)際上連接點(diǎn)還可以是字段或者構(gòu)造器
4、切入點(diǎn)(pointcut)
對(duì)連接點(diǎn)進(jìn)行攔截的定義
5、通知(advice)
所謂通知指的就是指攔截到連接點(diǎn)之后要執(zhí)行的代碼,通知分為前置、后置、異常、最終、環(huán)繞通知五類
6、目標(biāo)對(duì)象
代理的目標(biāo)對(duì)象
7、織入(weave)
將切面應(yīng)用到目標(biāo)對(duì)象并導(dǎo)致代理對(duì)象創(chuàng)建的過程
8、引入(introduction)
在不修改代碼的前提下,引入可以在運(yùn)行期為類動(dòng)態(tài)地添加一些方法或字段
Spring 實(shí)現(xiàn)AOP所需要的包:
1、Spring提供的jar包
2、aopalliance.jar
3、aspectjweaver.jar
Spring 實(shí)現(xiàn)AOP的方式:
1、Java動(dòng)態(tài)代理
該方法針對(duì)接口的實(shí)例創(chuàng)建代理
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="concreteImplementor" class="com.marving.aop.ConcreteImplementor" />
<bean id="interceptorHandler" class="com.marving.aop.InterceptorHandler" />
<aop:config>
<aop:aspect id="interceptor" ref="interceptorHandler">
<aop:pointcut id="addAllMethod" expression="execution(* com.marving.aop.Abstration.*(..))" />
<aop:before method="doSomething" pointcut-ref="addAllMethod" />
<aop:after method="doSomething" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
</beans> 其中Abstration為接口,ConcreteImplementor為實(shí)現(xiàn)類,InterceptorHandler為代理攔截類。
public interface <span >Abstration</span> {
public void operation()
} //具體實(shí)現(xiàn)化角色
public class ConcreteImplementor implements Implementor{
@Override
public void operation() {
System.out.println("ConcreteImplementor");
}
} public class InterceptorHandler{
public void printTime(){
System.out.println("CurrentTime = " + System.currentTimeMillis());
}
} 2、CGLIB生成代理
CGLIB針對(duì)代理對(duì)象為類的情況使用。
通過實(shí)現(xiàn)MethodInterceptor接口,并實(shí)現(xiàn) public Object intercept(Object obj, Method m, Object[] args,MethodProxy proxy) throws Throwable方法生成代理。
3、BeanNameAutoProxyCreator實(shí)現(xiàn)AOP
Spring為我們提供了自動(dòng)代理機(jī)制,讓容器為我們自動(dòng)生成代理,把我們從煩瑣的配置工作中解放出來,在內(nèi)部,Spring 使用BeanPostProcessor自動(dòng)地完成這項(xiàng)工作。
具體配置如下:
<bean id="MyInterceptor" class="com.yesjpt.interceptor. MyInterceptor"></bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>MyInterceptor</value>
</list>
</property>
</bean>
其中*Service 為需要攔截代理的bean,以Service結(jié)尾的都 被攔截,并使用MyInterceptor 進(jìn)行攔截,可配置多個(gè)攔截器,按順序執(zhí)行。
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* @author
*
*/
public class MyInterceptor implements MethodInterceptor{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();//獲取被攔截的方法
Object[] arguments = invocation.getArguments();//獲取攔截方法的參數(shù)
/*
* 特殊,某些權(quán)限需要做特殊處理
* 比如用戶信息權(quán)限,在方法執(zhí)行完畢返回的時(shí)候,要將電話號(hào)碼與郵箱抹除
*/
//環(huán)繞通知前置特殊處理
this.beforeReslove();
Object proceed = invocation.proceed();//調(diào)用目標(biāo)方法
//環(huán)繞通知后置特殊處理
proceed = this.afterReslove();
return proceed;
}
private Object afterReslove() {
System.out.println("CurrentTime = " + System.currentTimeMillis());
return null;
}
private void beforeReslove() {
System.out.println("CurrentTime = " + System.currentTimeMillis());
}
}
4、使用注解AspectJ實(shí)現(xiàn)AOP
ApplicationContext.xml 加入
<aop:aspectj-autoproxy/>
創(chuàng)建切面處理類
package com.marving.aop;
import java.util.Arrays;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class AspectHandler {
@Pointcut("execution(* com.marving.service.BaseServ+.*(..))")
private void doMethod() {
}
/**
* This is the method which I would like to execute before a selected method
* execution.
*/
@Before("doMethod()")
public void beforeAdvice() {
System.out.println("before method invoked.");
}
/**
* This is the method which I would like to execute after a selected method
* execution.
*/
@After("doMethod()")
public void afterAdvice() {
System.out.println("after method invoked.");
}
// 配置controller環(huán)繞通知,使用在方法aspect()上注冊(cè)的切入點(diǎn)
@Around("doMethod()")
public Object around(ProceedingJoinPoint pjp) throws Throwable{
Object result = null;
String methodName = pjp.getSignature().getName();
try {
System.out.println("The method [" + methodName + "] begins with " + Arrays.asList(pjp.getArgs()));
result = pjp.proceed();
} catch (Throwable e) {
System.out.println("The method [" + methodName + "] occurs expection : " + e);
throw new RuntimeException(e);
}
System.out.println("The method [" + methodName + "] ends");
return result;
}
}
通過表達(dá)式execution(* com.marving.service.BaseServ+.*(..)) 匹配切入點(diǎn)函數(shù),并使用@Before@After@Around 對(duì)所攔截方法執(zhí)行前、中、后進(jìn)行攔截并執(zhí)行處理函數(shù)。
@Around @Before @After三個(gè)注解的區(qū)別@Before是在所攔截方法執(zhí)行之前執(zhí)行一段邏輯。@After 是在所攔截方法執(zhí)行之后執(zhí)行一段邏輯。@Around是可以同時(shí)在所攔截方法的前后執(zhí)行一段邏輯。
值得注意的是,Around在攔截方法后,需要返回一個(gè)方法執(zhí)行結(jié)果,否則,原方法不能正常執(zhí)行。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
文章題目:SpringAOP的幾種實(shí)現(xiàn)方式總結(jié)
本文路徑:http://www.chinadenli.net/article34/peeese.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、虛擬主機(jī)、電子商務(wù)、網(wǎng)站設(shè)計(jì)公司、定制開發(fā)、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)