一、AOP的概念
1、AOP:面向切面編程,利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各個部分的耦合度降低,提高程式的可重用性,同時提高了開發效率
2、AOP通俗描述:不通過修改源代碼的方式進行功能增加,
3、AOP的底層原理:使用JDK的動態代理和CGLIB動態代理
(1)在有介面的情況下 使用JDK動態代理 創建介面的實作類代理物件增強類的方法
(2)在沒有介面的情況下 使用CGLIB動態代理 創建子類代理物件增強類的方法
4、AOP術語
(1)連接點 :類里那些方法可以被增強,這些方法被稱為連接點
(2)切入點 :類里實際被增強的方法,這些方法被稱為切入點
(3)通知(增強):實際增強的邏輯部分稱為通知(增強)
通知分5種型別:1、前置通知:方法執行前,2、后置通知:方法執行后,3、環繞通知:方法執行前后,4、例外通知:方法執行例外時,5、最終通知:如finally
(4)切面:把通知應用到切入點的動作
二、AOP的具體使用
1、創建介面UserDao
public interface UserDAO { public int add(int a, int b); public String upadate(String name); }
2、創建實作類
public class UserDaoImpl implements UserDAO { @Override public int add(int a, int b) { return a + b; } @Override public String upadate(String name) { return name; } }
3、創建JDKProxy
public class JDKProxy { public static void main(String[] args) { Class[] interfaces = {UserDAO.class}; /* 可以使用匿名物件的方式 也可以使用下面新建實作類的方式 Proxy.newProxyInstance(JDKProxy.class.getClassLoader(),interfaces,new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return null; } });*/ UserDaoImpl userDao = new UserDaoImpl(); UserDAO o = (UserDAO)Proxy.newProxyInstance(JDKProxy.class.getClassLoader(), interfaces, new UserDaoProxy(userDao)); int result = o.add(1, 2); System.out.println(result); } } class UserDaoProxy implements InvocationHandler{ //把創建的代理物件傳遞過來 private Object obj; public UserDaoProxy(Object obj){//使用構造器進行有參傳遞 this.obj=obj; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //方式執行前的操作 System.out.println("方法執行之前" + method.getName() + ":傳遞引數:" + Arrays.toString(args)); //被增強的方法執行 Object invoke = method.invoke(obj,args); //方法執行之后的操作 System.out.println("方法之后"+obj); return invoke; } }
三、AOP操作
1、Spring框架一般都是基于AspectJ實作AOP操作
(1)AspectJ不是Spring的組成部分 是獨立的AOP框架,一般把AspectJ和Spring一起使用,進行AOP操作
2、AspectJ實作AOP操作
(1)基于xml組態檔實作
(2)基于注解方式實作
3、切入點運算式
(1)切入點運算式作用:知道對哪個類里面的哪個方法進行增強
(2)語法結構: execution([權限修飾符] [回傳型別] [類全路徑] [方法名稱]([引數串列]) )
舉例1:對com.lianyou.dao.BookDao類里面的add進行增強
execution(* com.lianyou.dao.BookDao.add(..))
舉例2:對com.lianyou.dao.BookDao類里面的所有的方法進行增強
execution(* com.lianyou.dao.BookDao.* (..))
舉例3:對com.atguigu.dao包里面所有類,類里面所有方法進行增強
execution(* com.atguigu.dao.*.* (..))
4、AspectJ 注解方式
(1)創建類,定義方法
@Component public class User { public void add(){ System.out.println("add......"); } }
(2)創建增強類(撰寫增強邏輯)
@Component @Aspect public class UserProxy { //前置通知 @Before("execution(* com.lianyou.spring5.Proxy.User.add(..))") public void before(){ System.out.println("before......"); } //后置通知 @After("execution(* com.lianyou.spring5.Proxy.User.add(..))") public void after(){ System.out.println("after......"); } //后置通知(回傳通知) @AfterReturning("execution(* com.lianyou.spring5.Proxy.User.add(..))") public void afterReturning(){ System.out.println("afterReturning......"); } //例外通知 @AfterThrowing("execution(* com.lianyou.spring5.Proxy.User.add(..))") public void afterThrowing(){ System.out.println("afterThrowing......"); } //環繞通知 @Around("execution(* com.lianyou.spring5.Proxy.User.add(..))") public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { System.out.println("around...前..."); proceedingJoinPoint.proceed(); System.out.println("around.....后."); } }
(3)組態檔撰寫
<?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/beans http://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="com.lianyou.spring5.Proxy"></context:component-scan> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
(4)測驗類的撰寫
@Test public void testAop(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml"); User user = context.getBean("user", User.class); user.add(); System.out.println(user); }
(5)相同切入點設定
@Pointcut(value = "https://www.cnblogs.com/wuzkkk/archive/2022/02/20/execution(* com.lianyou.spring5.Proxy.User.add(..))") public void pointCut(){ } //前置通知 @Before("pointCut()") public void before(){ System.out.println("before......"); }
(6)設定多個增強類對同一個類進行增強的優先級 使用注解@Order(1) 括號里面的值越大則優先級越小
@Component @Aspect @Order(1) public class UserProxy { }
(7)完全注解開發
@Configuration @ComponentScan(basePackages = {"com.lianyou"})
@EnableAspectJAutoProxy(proxyTargetClass = true) public class ConfigAop { }
5、AspectJ 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:context="http://www.springframework.org/schema/context" 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/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"> <bean id="book" class="com.lianyou.spring5.Book.Book"></bean> <bean id="bookProxy" class="com.lianyou.spring5.Book.BookProxy"></bean> <aop:config> <!--配置切入點--> <aop:pointcut id="p" expression="execution(* com.lianyou.spring5.Book.Book.read())"/> <!--配置切面--> <aop:aspect ref="bookProxy"> <aop:before method="sleep" pointcut-ref="p"></aop:before> </aop:aspect> </aop:config> </beans> public class BookProxy { public void sleep(){ System.out.println("sleep........"); } } public class Book { public void read(){ System.out.println("read,,,,,"); } }
public void testXML(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
Book book = context.getBean("book", Book.class);
book.read();
System.out.println(book);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/429255.html
標籤:其他
上一篇:Spring系列18:Resource介面及內置實作
下一篇:Java檔案注釋(拓展)
