Java框架學習_Spring(五)AOP註解的配置與簡單測試、註解的通知型別總結、切入點的註解
阿新 • • 發佈:2019-01-14
前一篇已經對AOP_xml的配置作了學習,這次採用註解的方式,其實大同小異,改一下xml配置和切面類就好了
1、AOP註解的配置與測試:
- 導包:Spring_AOP開發jar包
- 編寫xml配置(預設名applicationContext.xml)
<?xml version="1.0" encoding="utf-8"?>
<beans 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 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 在配置檔案中開啟註解的AOP的開發============ -->
<aop:aspectj-autoproxy/>
<!-- 配置目標類================ -->
<bean class="cn.nupt.aopDemo.AopDemo" id="AopDemo"> </bean>
<!-- 配置切面類================ -->
<bean class="cn.nupt.aopClass.AopInsert" id="AopInsert"> </bean>
</beans>
- 編寫目標類:這裡的目標類和前篇沒什麼區別
package cn.nupt.aopDemo;
public class AopDemo {
public void haha() {
System.out.println("haha");
}
public void hehe() {
System.out.println("hehe");
}
public void xixi() {
System.out.println("xixi");
}
}
- 編寫切面類(注意,這裡加了兩個註釋@Aspect和@Before,分別代表這是一個切面類和前置增強)
package cn.nupt.aopClass;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
//編寫切面類
@Aspect
public class AopInsert {
@Before("execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
public void check() {
System.out.println("這是採用註解方式許可權校驗中。。。");
}
}
- 編寫測試類:這裡的測試類和前篇沒什麼區別
package cn.nupt.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import cn.nupt.aopDemo.AopDemo;
//junit和aop整合
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Resource(name="AopDemo")
private AopDemo aopDemo;
@Test
public void test() {
aopDemo.haha();
aopDemo.hehe();
aopDemo.xixi();
}
}
輸出:
haha
hehe
這是採用註解方式許可權校驗中。。。
xixi
2、註解的通知型別總結:(遇到哪一種按照下面的格式寫就行了,比xml配置方便多了)
package cn.nupt.aopClass;
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;
//編寫切面類
@Aspect
public class AopInsert {
@Before("execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
public void check1() {
System.out.println("前置增強====================");
}
@AfterReturning(value="execution(* cn.nupt.aopDemo.AopDemo.xixi(..))",returning="result")
public void check2(Object result) {
//後置增強可以得到目標函式的返回值
System.out.println("後置增強====================" + result);
}
@Around("execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
public Object check3(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("環繞前增強====================");
Object object = joinPoint.proceed();
System.out.println("環繞後增強====================");
return object;
}
@AfterThrowing(value="execution(* cn.nupt.aopDemo.AopDemo.xixi(..))",throwing="e")
public void check4(Throwable e) {
System.out.println("異常丟擲增強====================" + e.getMessage());
}
@After(value="execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
public void check5(Throwable e) {
System.out.println("最終增強====================" + e.getMessage());
}
}
3 、切入點的註解:
上面切入點的程式碼太長了,寫個註解來給它取個別名
//添加註解取別名為pointcut1
@Pointcut(value="execution(* cn.nupt.aopDemo.AopDemo.xixi(..))")
private void pointcut1(){}
//這個切入點就可以表示為AopInsert.pointcut1()了
@Before(value="AopInsert.pointcut1()")
public void check1() {
System.out.println("前置增強====================");
}