spring學習(六)註解方式實現AOP
阿新 • • 發佈:2018-12-16
一、導包(匯入maven的依賴)
<?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>day2</groupId> <artifactId>day2</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.6.RELEASE</version> </dependency> </dependencies> </project>
二、書寫配置檔案(用的是IDEA,配置檔案applicationContext.xml要放到resources資料夾中,不然會報找不到該檔案異常)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd "> <!-- 準備工作: 匯入aop(約束)名稱空間 --> <!-- 1.配置目標物件 --> <bean name="userService" class="dyh.aop.service.UserServiceImpl" ></bean> <!-- 2.配置通知物件 --> <bean name="myAdvice" class="dyh.aop.springaop1.MyAdvice" ></bean> <!-- 3.開啟使用註解完成織入 --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
三、書寫要增強的介面和實現類
public interface UserService { void save(); void delete(); void update(); void find(); }
public class UserServiceImpl implements UserService { public void save() { System.out.println("儲存使用者!"); //int i = 1/0; } public void delete() { System.out.println("刪除使用者!"); } public void update() { System.out.println("更新使用者!"); } public void find() { System.out.println("查詢使用者!"); } }
四、書寫通知類
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; //通知類 @Aspect public class MyAdvice { @Pointcut("execution(* dyh.aop.service.*ServiceImpl.*(..))") public void pc(){} //前置通知 //指定該方法是前置通知,並制定切入點 @Before("MyAdvice.pc()") public void before(){ System.out.println("這是前置通知!!"); } //後置通知 @AfterReturning("execution(* dyh.aop.service.*ServiceImpl.*(..))") public void afterReturning(){ System.out.println("這是後置通知(如果出現異常不會呼叫)!!"); } //環繞通知 @Around("execution(* dyh.aop.service.*ServiceImpl.*(..))") public Object around(ProceedingJoinPoint pjp) throws Throwable { System.out.println("這是環繞通知之前的部分!!"); Object proceed = pjp.proceed();//呼叫目標方法 System.out.println("這是環繞通知之後的部分!!"); return proceed; } //異常通知 @AfterThrowing("execution(* dyh.aop.service.*ServiceImpl.*(..))") public void afterException(){ System.out.println("出事啦!出現異常了!!"); } //後置通知 @After("execution(* dyh.aop.service.*ServiceImpl.*(..))") public void after(){ System.out.println("這是後置通知(出現異常也會呼叫)!!"); } }
五、測試
package dyh.aop.springaop1; import dyh.aop.service.UserService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import javax.annotation.Resource; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:applicationContextAop.xml"}) public class Demo { @Resource(name = "userService") private UserService us; @Test public void funaop(){ us.save(); } }
測試結果: