spring——AOP面向切面程式設計—— 基於 PointcutAdvisor 的 AOP 開發
阿新 • • 發佈:2022-12-11
專案依賴:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion><groupId>org.example</groupId> <artifactId>ssw</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.6</version> </dependency> </dependencies> </project>
建立OrderDao類:
package org.example.dao; public class OrderDao { public void add() { System.out.println("正在執行 UserDao 的 add() 方法……"); } public void adds() { System.out.println("正在執行 UserDao 的 adds() 方法……"); } public void delete() { System.out.println("正在執行 UserDao 的 delete() 方法……"); } public void modify() { System.out.println("正在執行 UserDao 的 modify() 方法……"); } public void get() { System.out.println("正在執行 UserDao 的 get() 方法……"); } }
建立環繞增強類,OrderDaoAroundAdvice :
package org.example.advice; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; /** * 增強程式碼 * 環繞增強 * * @author c語言中文網 c.biancheng.net */ public class OrderDaoAroundAdvice implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("環繞增強前********"); //執行被代理物件中的邏輯 Object result = methodInvocation.proceed(); System.out.println("環繞增強後********"); return result; } }
修改bean.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--帶切點的切面--> <!-- 定義目標(target)物件 --> <bean id="orderDao" class="org.example.dao.OrderDao"></bean> <!-- 定義增強 --> <bean id="aroundAdvice" class="org.example.advice.OrderDaoAroundAdvice"></bean> <!--定義切面--> <bean id="myPointCutAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <!--定義表示式,規定哪些方法進行攔截 .* 表示所有方法--> <!--<property name="pattern" value=".*"></property>--> <property name="patterns" value="org.example.dao.OrderDao.add.*,org.example.dao.OrderDao.delete.*"></property> <property name="advice" ref="aroundAdvice"></property> </bean> <!--Spring 通過配置生成代理--> <bean id="orderDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 配置目標 --> <property name="target" ref="orderDao"></property> <!-- 針對類的代理,該屬性預設取值為 false(可省略), 表示使用 JDK 動態代理;取值為 true,表示使用 CGlib 動態代理--> <property name="proxyTargetClass" value="true"></property> <!-- 在目標上應用增強 --> <property name="interceptorNames" value="myPointCutAdvisor"></property> </bean> </beans>
執行:
package org.example; import org.example.dao.OrderDao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //獲取 ApplicationContext 容器 ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml"); //獲取代理物件 OrderDao orderDao = context.getBean("orderDaoProxy", OrderDao.class); //呼叫 OrderDao 中的各個方法 orderDao.add(); orderDao.adds(); orderDao.delete(); orderDao.get(); orderDao.modify(); } }
==================================================================================================