Spring AOP

AOP使用

在不影響業(yè)務(wù)層邏輯的情況下,植入其它代碼,比如事務(wù)、日志、埋點(diǎn)統(tǒng)計(jì)之類的。另外需要引用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar

使用注解形式

1.使用注解@Aspect來(lái)定義一個(gè)切面,在切面中定義切入點(diǎn)(@Pointcut),通知類型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
2.開(kāi)發(fā)需要被攔截的類。
3.將切面配置到xml中,當(dāng)然,我們也可以使用自動(dòng)掃描Bean的方式。這樣的話,那就交由Spring AOP容器管理。

@Aspect
@Component
public class Log {
@Pointcut("execution(* com.story.*.service..*Service.*(..))")
public void method(){};

/*@Before("execution(* com.story.*.service..*Service.*(..)))")*/
@Before("method()")
public void before() {
    System.out.println("method start");
} 
@After("method()")
public void after() {
    System.out.println("method after");
} 
@AfterReturning("execution(* com.story.*.service..*Service.*(..))")
public void AfterReturning() {
    System.out.println("method AfterReturning");
} 
@AfterThrowing("execution(* com.story.*.service..*Service.*(..))")
public void AfterThrowing() {
    System.out.println("method AfterThrowing");
} 

}
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
 default-lazy-init="true">
   
<!-- enable autowire -->
<!-- Activates scanning of @Autowired -->
<context:annotation-config />

<!-- enable component scanning (beware that this does not enable mapper scanning!) -->
<context:component-scan base-package="com.story">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

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

自動(dòng)注入

這個(gè)不在文件中加注解,個(gè)人比較喜歡

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
 default-lazy-init="true">
   
<!-- enable autowire -->
<!-- Activates scanning of @Autowired -->
<context:annotation-config />

<!-- enable component scanning (beware that this does not enable mapper scanning!) -->
<context:component-scan base-package="com.yoxnet">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

<aop:aspectj-autoproxy/>

<bean id="logsAop" class="com.story.aop.log"></bean>
<aop:config>
    <aop:pointcut expression="execution(* com.story.*.service.*Service*.*(..))" 
    id="servicePointcut"/>
    <aop:aspect id="logAspect" ref="logsAop">
        <aop:before method="before"  pointcut-ref="servicePointcut" />
    </aop:aspect>

</aop:config>
</beans>

這種方式不需要在類中加任何注解,只需要正確指定bean的類名即可。

需要注意的問(wèn)題

1、AOP原理是基于java的方法代理實(shí)現(xiàn)的,類似于OC語(yǔ)言中的通知,就是在指定類的執(zhí)行前、后或者返回、異常的情況下發(fā)出通知,去通知到指定類的指定方法。
2、如果定義的AOP是多個(gè),則會(huì)根據(jù)定義在xml文件中順序去通知。
3、如果工程框架中是多個(gè)context,就是大家說(shuō)的雙親上下文,在掃描完service后,又去掃描servlet,則需要分別指定需要掃描的注解,即在掃描Controller時(shí),不要掃描Service,否則會(huì)覆蓋掉service中的aop,使用其不生效?;蛘邔op配置放到Controller文件中。
4、通知只有在類調(diào)用其它類的public方法才會(huì)發(fā)出,調(diào)用自己的方法不會(huì)發(fā)出通知。所以可以將方法私有化來(lái)避免類中某些方法被代理。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 什么是AOP AOP(Aspect-Oriented Programming,面向切面編程),可以說(shuō)是OOP(Ob...
    FX_SKY閱讀 20,548評(píng)論 1 32
  • title: Spring_AOP源碼分析date: 2016-11-03 01:15:11categories:...
    raincoffee閱讀 1,829評(píng)論 2 36
  • Bean注解配置光速入門(mén) 步驟一: 創(chuàng)建 web 項(xiàng)目,引入 Spring 的開(kāi)發(fā)包在 Spring 的注解的...
    日落perfe閱讀 10,272評(píng)論 1 8
  • 基本知識(shí) 其實(shí), 接觸了這么久的 AOP, 我感覺(jué), AOP 給人難以理解的一個(gè)關(guān)鍵點(diǎn)是它的概念比較多, 而且坑爹...
    永順閱讀 8,655評(píng)論 5 114
  • 人沒(méi)有理想是活不下去的,它是一種自然而然的沖動(dòng),是讓你仍舊會(huì)期待明天的理由。在舊石器時(shí)代,人類最大的理想就是活下去...
    從黃昏到黎明閱讀 351評(píng)論 0 0

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