Xml實現aop編程:spring
1) 引入jar文件 【aop 相關jar, 4個】express
2) 引入aop名稱空間編程
3)aop 配置ide
* 配置切面類 (重複執行代碼造成的類)spa
* aop配置代理
攔截哪些方法 / 攔截到方法後應用通知代碼xml
package loaderman.f_aop_xml; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; // 切面類 public class Aop { public void begin(){ System.out.println("開始事務/異常"); } public void after(){ System.out.println("提交事務/關閉"); } public void afterReturning() { System.out.println("afterReturning()"); } public void afterThrowing(){ System.out.println("afterThrowing()"); } public void around(ProceedingJoinPoint pjp) throws Throwable{ System.out.println("環繞前...."); pjp.proceed(); // 執行目標方法 System.out.println("環繞後...."); } }
package loaderman.f_aop_xml; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { ApplicationContext ac = new ClassPathXmlApplicationContext("cn/loaderman/f_aop_xml/bean.xml"); // 目標對象有實現接口,spring會自動選擇「JDK代理」 @Test public void testApp() { IUserDao userDao = (IUserDao) ac.getBean("userDao"); System.out.println(userDao.getClass());//$Proxy001 userDao.save(); } // 目標對象沒有實現接口, spring會用「cglib代理」 @Test public void testCglib() { OrderDao orderDao = (OrderDao) ac.getBean("orderDao"); System.out.println(orderDao.getClass()); orderDao.save(); } }
package loaderman.f_aop_xml; public interface IUserDao { void save(); }
package loaderman.f_aop_xml; /** * 目標對象 * */ public class OrderDao{ public void save() { System.out.println("-----核心業務:保存!!!------"); } }
package loaderman.f_aop_xml; /** * 目標對象 * */ public class UserDao implements IUserDao{ @Override public void save() { System.out.println("-----核心業務:保存!!!------"); } }
<?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:p="http://www.springframework.org/schema/p" 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"> <!-- dao 實例 --> <bean id="userDao" class="loaderman.f_aop_xml.UserDao"></bean> <bean id="orderDao" class="loaderman.f_aop_xml.OrderDao"></bean> <!-- 切面類 --> <bean id="aop" class="loaderman.f_aop_xml.Aop"></bean> <!-- Aop配置 --> <aop:config> <!-- 定義一個切入點表達式: 攔截哪些方法 --> <aop:pointcut expression="execution(* loaderman.f_aop_xml.*.*(..))" id="pt"/> <!-- 切面 --> <aop:aspect ref="aop"> <!-- 環繞通知 --> <aop:around method="around" pointcut-ref="pt"/> <!-- 前置通知: 在目標方法調用前執行 --> <aop:before method="begin" pointcut-ref="pt"/> <!-- 後置通知: --> <aop:after method="after" pointcut-ref="pt"/> <!-- 返回後通知 --> <aop:after-returning method="afterReturning" pointcut-ref="pt"/> <!-- 異常通知 --> <aop:after-throwing method="afterThrowing" pointcut-ref="pt"/> </aop:aspect> </aop:config> </beans>