第8講_Spring AOP注解詳細(xì)介紹

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

  1. 要使用Spring AOP注解,必須滿足如下的事項(xiàng)
  • 導(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í)注解的語(yǔ)法,下面的內(nèi)容會(huì)給大家慢慢介紹

  1. 織入點(diǎn)語(yǔ)法
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.*(..)),這個(gè)就是織入點(diǎn)的語(yǔ)法,它告訴AOP框架哪個(gè)類中方法需要進(jìn)行AOP

  1. execution語(yǔ)法介紹
  • execution(public * *(..))
  • execution(* set*(..))
  • execution(* com.xyz.service.AccountService.*(..))
  • execution(* com.xyz.service...(..))
  • 上面只是舉例說(shuō)明了execution的語(yǔ)法,下面是一個(gè)標(biāo)準(zhǔn)的語(yǔ)法定義
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)
  1. Spring AOP注解例子
  • @Before前置建議,它是在執(zhí)行一個(gè)業(yè)務(wù)方法之前插入的切面
  • @AfterReturning,它是當(dāng)一個(gè)方法正常運(yùn)行后,執(zhí)行的切面
  • @After,它是當(dāng)方法執(zhí)行成功或者出現(xiàn)異常的時(shí)候都會(huì)執(zhí)行切面
  • @Around,它相當(dāng)于一個(gè)AOP鏈,如果當(dāng)前AOP執(zhí)行后,就讓下一個(gè)AOP執(zhí)行
  • @AfterThrowing,如果在方法中有錯(cuò)誤拋出,則執(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)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

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