Spring AOP註解形式簡單實現
實現步驟:
1:導入類掃描的註解解析器
命名空間:xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
xml配置文件如下配置:<context:component-scan base-package="com.lxk.spring.aop.annotation"/>
2:導入springAOP的註解解析器
命名空間:xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
xml配置文件如下配置:<aop:aspectj-autoproxy/> //開啟aop註釋自動代理
3:首先定義我們的類,實現項目中的某個功能。
1import org.springframework.stereotype.Component; 2 3 @Component("aopFun") //這裏將此類加載到bean裏面,這裏是使用的註解形式,也可以通過spring.xml文件中配置<bean id="aopFun" class="com.zs.aop"></bean>進和依賴註入 4 public class AopFun{ 5 public void printfStr(){ 6 System.out.println("執行切點方法"); 7 }8 }
4:配置切面類,也就是配置在什麽情況下我們去執行切點:
1 /** 2 * 註解方式聲明aop 3 * 1.用@Aspect註解將類聲明為切面(如果用@Component("")註解註釋為一個bean對象,那麽就要在spring配置文件中開啟註解掃描,<context:component-scan base-package="com.cjh.aop2"/> 4 * 否則要在spring配置文件中聲明一個bean對象) 5 * 2.在切面需要實現相應方法的前面加上相應的註釋,也就是通知類型。 6 * 3.此處有環繞通知,環繞通知方法一定要有ProceedingJoinPoint類型的參數傳入,然後執行對應的proceed()方法,環繞才能實現。 7 */ 8 @Component("aopTest") 9 @Aspect 10 public class AopTest{ 11 //定義切點 12 @Pointcut("execution(* *.printfStr(..))") 13 public void printfString(){} 14 /** 15 * 前置通知(註解中的printfString()方法,其實就是上面定義pointcut切點註解所修飾的方法名,那只是個代理對象,不需要寫具體方法, 16 * 相當於xml聲明切面的id名,如下,相當於id="embark",用於供其他通知類型引用) 17 * <aop:config> 18 <aop:aspect ref="mistrel"> 19 <!-- 定義切點 --> 20 <aop:pointcut expression="execution(* *.printfStr(..))" id="embark"/> 21 <!-- 聲明前置通知 (在切點方法被執行前調用) --> 22 <aop:before method="beforSay" pointcut-ref="embark"/> 23 <!-- 聲明後置通知 (在切點方法被執行後調用) --> 24 <aop:after method="afterSay" pointcut-ref="embark"/> 25 </aop:aspect> 26 </aop:config> 27 */ 28 @Before("printfString()") //before:在切點執行之前執行 29 public void sayHello(){ 30 System.out.println("註解類型前置通知"); 31 } 32 //後置通知 33 @After("printfString()") //After:在切點執行之後執行 34 public void sayGoodbey(){ 35 System.out.println("註解類型後置通知"); 36 } 37 //環繞通知。註意要有ProceedingJoinPoint參數傳入。 38 @Around("printfString()") 39 public void sayAround(ProceedingJoinPoint pjp) throws Throwable{ 40 System.out.println("註解類型環繞通知..環繞前"); 41 pjp.proceed();//執行方法 42 System.out.println("註解類型環繞通知..環繞後"); 43 } 44 }
5:spring.xml文件配置
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> 9 <!-- 開啟註解掃描 --> 10 <context:component-scan base-package="com.zs.aop"/> 11 <!-- 開啟aop註解方式,此步驟s不能少,這樣java類中的aop註解才會生效 --> 12 <aop:aspectj-autoproxy/> 13 </beans>
6:測試類
1 package com.cjh.aop2; 2 3 import org.springframework.context.ApplicationContext; 4 import org.springframework.context.support.ClassPathXmlApplicationContext; 5 6 /** 7 * 8 * @author Caijh 9 * email:[email protected] 10 * 2017年7月11日 下午6:27:06 11 */ 12 public class Test { 13 public static void main(String[] args) { 14 ApplicationContext ac = new ClassPathXmlApplicationContext("com/zs/aop/spring.xml"); //獲取applicationContext對象 15 AopFun bean1= (AopFun) ac.getBean("aopFun"); //通過spring的bean對象獲取AopFun對象,並對對象進行依賴註入 16 bean1.printfStr(); //執行功能方法 17 } 18 }
7:最後打印的結果為
註解類型環繞通知..環繞前
註解類型前置通知
執行切點方法
註解類型環繞通知..環繞後
註解類型後置通知
通知類型有:
1:@Before:通知這個方法會在功能之前執行
2:@After:通知這個方法會在功能之後執行
3:@AfterThrow:通知這個方法會在功能拋出異常時執行
4:@AfterReturning:通知這個方法會在功能return後執行
5:@Around:通知這個方法會將功能封裝起來
在最後也簡單說一下通過spring.xml文件配置aop
1 <!-- 使用xml配置aop --> 2 <!-- 強制使用cglib代理,如果不設置,將默認使用jdk的代理,但是jdk的代理是基於接口的 --> 3 <aop:config proxy-target-class="true" /> 4 <aop:config> 5 <!--定義切面--> 6 <aop:aspect id="logAspect" ref="logInterceptor"> 7 <!-- 定義切入點 (配置的的有printfStr方法在調用之前都會被攔截)--> 8 <aop:pointcut expression="execution(execution(* *.printfStr(..)))" id="logPointCut"/>
9 <!--方法執行之前被調用執行的-->
10 <aop:before method="before" pointcut-ref="logPointCut"/><!--一個切入點的引用-->
11 <aop:after method="after" pointcut-ref="logPointCut"/><!--一個切入點的引用-->
12 </aop:aspect>
13 </aop:config>
Spring AOP註解形式簡單實現