當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring中AOP切面编程学习笔记
生活随笔
收集整理的這篇文章主要介紹了
Spring中AOP切面编程学习笔记
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
注解方式實現aop我們主要分為如下幾個步驟:
1.在切面類(為切點服務的類)前用@Aspect注釋修飾,聲明為一個切面類。
2.用@Pointcut注釋聲明一個切點,目的是為了告訴切面,誰是它的服務對象。(此注釋修飾的方法的方法體為空,不需要寫功能比如 public void say(){};就可以了,方法名可以被候命的具體服務功能所以引用,它可以被理解為切點對象的一個代理對象方法)
3.在對應的方法前用對應的通知類型注釋修飾,將對應的方法聲明稱一個切面功能,為了切點而服務
4.在spring配置文件中開啟aop注釋自動代理。如:<aop:aspectj-autoproxy/>
通知注解類型如下:
?
切點方法:
?
package aopdemo;import org.springframework.stereotype.Component;/*** @author coco.xu* @Date 2019-03-25*/ @Component("sayName") public class SayName {public void saying() {System.out.println("我是coco...(切點方法)");} }?
?切面類:
package aopdemo;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; 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;/*** @author coco.xu* @Date 2019-03-25*//*** 注解方式聲明aop* 1.用@Aspect注解將類聲明為切面(如果用@Component("")注解注釋為一個bean對象,那么就要在spring配置文件中開啟注解掃描,* <context:component-scan base-package="aopdemo"/>* 否則要在spring配置文件中聲明一個bean對象) * 2.在切面需要實現相應方法的前面加上相應的注釋,也就是通知類型。* 3.此處有環繞通知,環繞通知方法一定要有ProceedingJoinPoint類型的參數傳入,然后執行對應的proceed()方法,環繞才能實現。*/ @Component("annotationTest") @Aspect public class AnnotationTest {// 定義切點@Pointcut("execution(* *.saying(..))")public void sayings() {}/*** 前置通知(注解中的sayings()方法,其實就是上面定義pointcut切點注解所修飾的方法名,那只是個代理對象,不需要寫具體方法,* 相當于xml聲明切面的id名,如下,相當于id="embark",用于供其他通知類型引用)* <aop:config> <aop:aspect ref="mistrel"> <!-- 定義切點 -->* <aop:pointcut expression="execution(* *.saying(..))" id="embark"/> <!--* 聲明前置通知 (在切點方法被執行前調用) -->* <aop:before method="beforSay" pointcut-ref="embark"/> <!-- 聲明后置通知* (在切點方法被執行后調用) -->* <aop:after method="afterSay" pointcut-ref="embark"/> </aop:aspect>* </aop:config>*/@Before("sayings()")public void sayHello() {System.out.println("注解類型前置通知");}// 后置通知@After("sayings()")public void sayGoodbey() {System.out.println("注解類型后置通知");}// 環繞通知。注意要有ProceedingJoinPoint參數傳入。@Around("sayings()")public void sayAround(ProceedingJoinPoint pjp) throws Throwable {System.out.println("注解類型環繞通知..環繞前");pjp.proceed();// 執行方法System.out.println("注解類型環繞通知..環繞后");} }?
?Spring配置文件:
<?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"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"><!-- 開啟注解掃描 --><context:component-scan base-package="aopdemo"/><!-- 開啟aop注解方式,此步驟不能少,這樣java類中的aop注解才會生效 --><aop:aspectj-autoproxy/> </beans>?
測試類:
package aopdemo;import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** * @author coco.xu* @Date 2019-03-25*/ public class AopTest {public static void main(String[] args) {ApplicationContext ac = new ClassPathXmlApplicationContext("aopdemo/beans.xml");SayName sn = (SayName) ac.getBean("sayName");sn.saying();} }?
執行測試類,執行結果如下:
?
轉載于:https://www.cnblogs.com/cocoxu1992/p/10593154.html
總結
以上是生活随笔為你收集整理的Spring中AOP切面编程学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 卡方分箱算法_python
- 下一篇: Spring中抛出异常时,既要要返回错误