哈嘍,大家好,我是了不起。
AOP就是面向切面編程,或者叫面向方面編程,或者開玩笑的說叫面向方便面編程,如果粗俗的理解,就是可以自定義注解,然后通過自己定義的方式定義注解的作用。
SpringAOP的全稱是(Aspect Oriented Programming)中文翻譯過來是面向切面編程,AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生范型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
圖片
圖片
提供聲明式事允許用戶自定義切面:
圖片
使用AOP織入,需要導入一個依賴包:
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.4</version></dependency>
applicationContext.xml:
<?xml versinotallow="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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--注冊bean--> <bean id="userService" class="service.UserServiceImpl"/> <bean id="log" class="log.Log"/> <bean id="afterLog" class="log.AfterLog"/> <!--配置aop:需要導入aop的約束--> <aop:config> <!--切入點:expression:表達式,execution(要執行的位置! * * * *)--> <aop:pointcut id="pointcut" expression="execution(* service.UserServiceImpl.*(..))"></aop:pointcut> <!--執行環繞增加--> <aop:advisor advice-ref="log" pointcut-ref="pointcut"/> <aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/> </aop:config></beans>
UserService接口:
public interface UserService { public void add(); public void delete(); public void update(); public void select();}
UserServiceImpl實現類(切入點):
public class UserServiceImpl implements UserService{ @Override public void add() { System.out.println("增加了一個用戶"); } @Override public void delete() { System.out.println("刪除了一個用戶"); } @Override public void update() { System.out.println("更新了一個用戶"); } @Override public void select() { System.out.println("查詢了一個用戶"); }}
前置通知:
import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;public class Log implements MethodBeforeAdvice { //method:要執行的目標對象的方法 //args:參數 //target:目標參數 @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println(target.getClass().getName()+"的"+method.getName()+"被執行了"); }}
后置通知:
import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;public class AfterLog implements AfterReturningAdvice { //returnValue:返回值 @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("執行了"+method.getName()+"方法返回結果為:"+ returnValue); }}
測試類:
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import service.UserService;import service.UserServiceImpl;import java.lang.annotation.Annotation;public class MyTest { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //動態代理的是接口 UserService userService = (UserService) context.getBean("userService"); userService.select(); }}
通過本文的講解,我們深入了解了切面編程的核心概念、動態代理的實現原理,并通過一個實際的例子展示了使用Java AOP的完整過程。AOP可以幫助我們將橫切關注點(例如日志記錄、事務管理等)從核心業務邏輯中解耦出來,提高代碼的可維護性和重用性。同時,AOP也是實現設計模式和架構思想的重要手段之一,我們在開發中可以靈活運用AOP來優化代碼結構并提高系統的整體性能。
本文鏈接:http://www.tebozhan.com/showinfo-26-89717-0.htmlJava AOP實踐指南:切面編程詳解
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: DotNet開發之反射技術詳解