第8講_Spring AOP注解詳細介紹

這節(jié)內(nèi)容非常關(guān)鍵,我們會比較詳細地介紹Spring AOP注解的使用

  1. 要使用Spring AOP注解,必須滿足如下的事項
  • 導(dǎo)入Aspectj的jar、Spring3.0-AOP.jar、aopalliance.jar
  • 需要在配置文件中加入注解的配置,例如:bean-aop-annotiation.xml
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

如果使用Spring AOP注解,最好的用處就是減少在配置文件中的AOP內(nèi)容。但是如果要掌握好Spring的AOP還需要學(xué)習(xí)注解的語法,下面的內(nèi)容會給大家慢慢介紹

  1. 織入點語法
package com.spring.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
public class LogAop {

@Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))")
 public void logBefore() {
    System.out.println("方法執(zhí)行之前轉(zhuǎn)載日志");
 }
}

而execution(public void com.spring.dao.impl.StudentDaoImpl.*(..)),這個就是織入點的語法,它告訴AOP框架哪個類中方法需要進行AOP

  1. execution語法介紹
  • execution(public * *(..))
  • execution(* set*(..))
  • execution(* com.xyz.service.AccountService.*(..))
  • execution(* com.xyz.service...(..))
  • 上面只是舉例說明了execution的語法,下面是一個標準的語法定義
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
  1. Spring AOP注解例子
  • @Before前置建議,它是在執(zhí)行一個業(yè)務(wù)方法之前插入的切面
  • @AfterReturning,它是當一個方法正常運行后,執(zhí)行的切面
  • @After,它是當方法執(zhí)行成功或者出現(xiàn)異常的時候都會執(zhí)行切面
  • @Around,它相當于一個AOP鏈,如果當前AOP執(zhí)行后,就讓下一個AOP執(zhí)行
  • @AfterThrowing,如果在方法中有錯誤拋出,則執(zhí)行此建議
package com.spring.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
public class LogAop {

@Before("execution(public void com.spring.dao.impl.StudentDaoImpl.*(..))")
 public void logBefore() {
    System.out.println("方法執(zhí)行之前轉(zhuǎn)載日志");
 }

@AfterReturning("execution(public void com.spring.dao.impl.StudentDaoImpl.insert(..))")
public void logAfterReturning() {
    System.out.println("方法執(zhí)行返回后載入日志");
}

@After("execution(public * com.spring.dao.impl.StudentDaoImpl.*(..))")
public void logAfter() {
    System.out.println("Finally載入日志");
}

@Around("execution(public * com.spring.dao.impl.StudentDaoImpl.*(..))")
public Object doBasicProfiling(ProceedingJoinPointpjp) throws Throwable {
    System.out.println("===around建議載入日志===" + new Date());
    Object o = pjp.proceed();
     return o;
}

@AfterThrowing("execution(public * com.spring.dao.impl.StudentDaoImpl.*(..))")
public void logAfterThrowing() {
    System.out.println("===有參數(shù)異常載入日志===" + new Date());
}
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,555評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • 本章內(nèi)容: 面向切面編程的基本原理 通過POJO創(chuàng)建切面 使用@AspectJ注解 為AspectJ切面注入依賴 ...
    謝隨安閱讀 3,425評論 0 9
  • AOP實現(xiàn)可分為兩類(按AOP框架修改源代碼的時機): 靜態(tài)AOP實現(xiàn):AOP框架在編譯階段對程序進行修改,即實現(xiàn)...
    數(shù)獨題閱讀 2,401評論 0 22
  • 生活里很多東西都是會破滅的,但不妨礙我們依舊相信并熱愛生活。 你是自己人生的的編劇,何必把劇本寫得苦不堪言...
    mjmm閱讀 434評論 0 0

友情鏈接更多精彩內(nèi)容