javascript
(Spring)AOP-面向切面编程
文章目錄
- AOP-面向切面編程
- 1. 什么是AOP?
- 2. Aop在Spring中的作用
- 3. 使用Spring實現Aop
- 3.1 通過 Spring API 實現
- 3.2 自定義類來實現Aop
- 3.3 使用注解實現Aop
AOP-面向切面編程
1. 什么是AOP?
AOP(Aspect Oriented Programming)意為:面向切面編程,通過預編譯方式和運行期動態(tài)代理實現程序功能的統一維護的一種技術。AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生范型。利用AOP可以對業(yè)務邏輯的各個部分進行隔離,從而使得業(yè)務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。
2. Aop在Spring中的作用
提供聲明式事務;允許用戶自定義切面
AOP相關名詞概念
-
橫切關注點:跨越應用程序多個模塊的方法或功能。即是,與我們業(yè)務邏輯無關的,但是我們需要關注的部分,就是橫切關注點。如日志 , 安全 , 緩存 , 事務等等 …
-
切面(ASPECT):橫切關注點 被模塊化 的特殊對象。即,它是一個類。
-
通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。
-
目標(Target):被通知對象。
-
代理(Proxy):向目標對象應用通知之后創(chuàng)建的對象。
-
切入點(PointCut):切面通知 執(zhí)行的 “地點”的定義。
-
連接點(JointPoint):與切入點匹配的執(zhí)行點。
SpringAOP中,通過Advice定義橫切邏輯,Spring中支持5種類型的Advice:
1、Interception Around(實現MethodInterceptor接口)
2、Before
3、After Returning
4、Throw
5、Introduction
即 Aop 在 不改變原有代碼的情況下 , 去增加新的功能 .
3. 使用Spring實現Aop
3.1 通過 Spring API 實現
1.編寫業(yè)務接口和實現類
public interface UserService {void add();void delete();void update();void select();} public class UserServiceImpl implements UserService{public void add() {System.out.println("增加了一個用戶");}public void delete() {System.out.println("刪除了一個用戶");}public void update() {System.out.println("修改了一個用戶");}public void select() {System.out.println("查詢了一個用戶");} }2.然后寫增強類,一個前置增強,一個后置增強
public class Log implements MethodBeforeAdvice {//method : 要執(zhí)行的目標對象的方法//objects : 被調用的方法的參數//Object : 目標對象public void before(Method method, Object[] objects, Object o) throws Throwable {System.out.println(o.getClass().getName()+"的"+method.getName()+"方法被執(zhí)行了");} } public class AfterLog implements AfterReturningAdvice {//o 返回值//method被調用的方法//args 被調用的方法的對象的參數//o1 被調用的目標對象public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable {System.out.println("執(zhí)行了"+o1.getClass().getName()+"的"+method.getName()+"方法,"+"返回值"+o);} }3.最后去spring文件中注冊bean,并實現aop切入,注意導入aop約束
<?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.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--注冊bean--><bean id="userService" class="com.zh.service.UserServiceImpl"/><bean id="log" class="com.zh.log.Log"/><bean id="afterLog" class="com.zh.log.AfterLog"/><!--aop的配置--><aop:config><!--切入點 expression:表達式匹配要執(zhí)行的方法--><aop:pointcut id="pointcut" expression="execution(* com.zh.service.UserServiceImpl.*(..))"/><!--執(zhí)行環(huán)繞; advice-ref執(zhí)行方法 . pointcut-ref切入點--><aop:advisor advice-ref="log" pointcut-ref="pointcut"/><aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/></aop:config></beans>4.測試
public class MyTest {@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");UserService userService = context.getBean("userService", UserService.class);userService.select();} }3.2 自定義類來實現Aop
1.寫一個自定義切入類
public class DiyPointCut {public void before(){System.out.println("---------方法執(zhí)行前--------");}public void after(){System.out.println("---------方法執(zhí)行后--------");} }2.配置spring
<!--第二種方式自定義實現--><!--注冊bean--><bean id="diy" class="com.zh.diy.DiyPointCut"/><aop:config ><aop:aspect ref="diy"><aop:pointcut id="diyPointCut" expression="execution(* com.zh.service.UserServiceImpl.*(..))"/><aop:before method="before" pointcut-ref="diyPointCut"/><aop:after method="after" pointcut-ref="diyPointCut"/></aop:aspect></aop:config>3.測試
3.3 使用注解實現Aop
1.編寫一個注解實現的增強類
@Aspect public class AnnotationPointcut {@Before("execution(* com.zh.service.UserServiceImpl.*(..))")public void before(){System.out.println("---------方法執(zhí)行前--------");}@After("execution(* com.zh.service.UserServiceImpl.*(..))")public void after(){System.out.println("---------方法執(zhí)行后--------");}@Around("execution(* com.zh.service.UserServiceImpl.*(..))")public void around(ProceedingJoinPoint jp) throws Throwable{System.out.println("環(huán)繞前");System.out.println("簽名:"+jp.getSignature());//執(zhí)行目標方法proceedObject proceed = jp.proceed();System.out.println("環(huán)繞后");System.out.println(proceed);} }2.在Spring配置文件中,注冊bean,并增加支持注解的配置
<!--開啟注解支持--><aop:aspectj-autoproxy/><bean id="annotationPointcut" class="com.zh.diy.AnnotationPointcut"/>3.測試
@Testpublic void test(){ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml");UserService userService = context.getBean("userService", UserService.class);userService.select();}總結
以上是生活随笔為你收集整理的(Spring)AOP-面向切面编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (Spring)静态/动态代理模式(AO
- 下一篇: (Spring)整合mybatis