1. 程式人生 > >Spring AOP(五)—— Spring的基於AspectJ的AOP開發

Spring AOP(五)—— Spring的基於AspectJ的AOP開發

Spring的基於AspectJ的AOP開發

之前我們已經講過了實現AOP的兩種方式,那麼下面我們講一下Spring中的AOP開發。與Spring中的IOC控制反轉類似,我們可以在xml中實現和使用註解實現,Spring提供了兩種切面宣告方式:

  • 基於XML配置方式宣告切面。
  • 基於註解方式宣告切面。 為了使用Spring的註解方式進行面向切面程式設計,需要在專案中加入與AOP相關的jar包: 在這裡插入圖片描述 沒有jar的朋友可以來這裡下載:https://download.csdn.net/download/qq_34598667/10750269 要進行AOP程式設計,我們接著要在Spring的配置檔案——beans.xml中引入aop名稱空間:
xmlns:aop="http://www.springframework.org/schema/aop"

http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"

新增完之後的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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"
>
</beans>

下面我們基於此章講解Spring的基於AspectJ的AOP開發的兩種方式,先從基於XML配置方式開始,請點選連結: