完整代碼在這php
AOP的全稱是 Aspect Oriented Program(面向切面編程)java
按照個人理解,AOP的思想通俗易懂的來講就是將程序的功能分爲兩類,一類是核心功能,另外一類是輔助功能。spring
在非面向切面編程裏,核心功能和輔助功能老是耦合在一塊兒,好比說進行數據庫操做時,日誌記錄行爲的代碼老是緊挨着數據庫操做(不知作別的大神是什麼樣,反正我是這樣)。這樣一來,核心功能(數據庫操做)的代碼就和輔助功能(記錄日誌)的代碼耦合在一塊兒了。數據庫
而在AOP裏,核心功能和輔助功能的代碼是徹底分開的,它們能夠各自獨立修改,而徹底不干擾對方,這實際上應該是應用了Java設計模式裏的代理模式(改天記得複習一下)。express
要加哪些包?apache
spring的核心包就不用說了,除此以外還要加上和AOP有關的包,aspectJ,完整的maven配置文件以下:編程
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringLearn</groupId>
<artifactId>SpringDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.11.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.8</version>
<configuration>
<downloadSources>true</downloadSources>
</configuration>
</plugin>
</plugins>
</build>
</project>
複製代碼
要加哪些類?設計模式
LoggerAspectbash
ProductServiceeclipse
Category
Product
目錄結構以下:
建立一個核心功能的類 ProductService
package com.learn.service;
import org.springframework.stereotype.Component;
@Component("ps")
public class ProductService {
public void doSomeService() {
System.out.println("僞裝這裏是一些核心業務代碼!");
}
}
複製代碼
建立一個輔助功能的類 LoggerAspect
package com.learn.aspect;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
@Component("la")
public class LoggerAspect {
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
複製代碼
能夠看到這裏核心功能的代碼和輔助功能的代碼是徹底分開的,那麼怎麼把它們聯繫到一塊兒呢?
這就須要配置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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--掃描bean-->
<context:component-scan base-package="com.learn.service com.learn.springBasic com.learn.aspect"/>
<!--配置AOP-->
<aop:config>
<!--配置切點-->
<aop:pointcut id="loggerCutpoint" expression="execution(* com.learn.service.ProductService.*(..)) "/>
<!--配置織入對象-->
<aop:aspect id="logAspect" ref="la">
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
</aop:aspect>
</aop:config>
</beans>
複製代碼
配置文件中須要加入 <aop:config> 標籤。
<aop:pointcut> 標籤配置的是核心類,在這裏指定了要織入的對象是哪一個。
execution(* com.learn.service.ProductService.*(..))
複製代碼
上面這句表達式表示,全部ProductService下的方法都將被織入輔助功能,既在執行該方法時會調用輔助功能。 <aop:aspect> 配置的是輔助類
<aop:around pointcut-ref="loggerCutpoint" method="log"/>
複製代碼
上面標籤中的 pointcut-ref 很顯然就是要織入的核心類id, method 就是被織入的方法。
運行結果:
start log:doSomeService
僞裝這裏是一些核心業務代碼!
end log:doSomeService
複製代碼
運行結果呈現出這樣的效果的是由於輔助類中log方法中是這樣寫的:
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
複製代碼
log方法最終返回的是一個Object對象,就本例來講,返回的其實是ProductService 對象,這裏應該是用到了代理模式來實現。
註解方式配置AOP也很簡單,和註解方式配置bean是很是類似的。
首先,spring配置文件中把 <aop:config> 下的標籤都刪掉,換成 <aop:aspectj-autoproxy/> 標籤來啓用自動代理。
<?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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--掃描bean-->
<context:component-scan base-package="com.learn.service com.learn.springBasic com.learn.aspect"/>
<!--配置自動代理AOP-->
<aop:aspectj-autoproxy/>
</beans>
複製代碼
LoggerAspect 類中也要作一些改變,加上@Aspect 和 @Around註解。
package com.learn.aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.aspectj.lang.ProceedingJoinPoint;
@Aspect
@Component("la")
public class LoggerAspect {
@Around(value = "execution(* com.learn.service.ProductService.*(..))")
public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("start log:" + joinPoint.getSignature().getName());
Object object = joinPoint.proceed();
System.out.println("end log:" + joinPoint.getSignature().getName());
return object;
}
}
複製代碼
運行結果:
start log:doSomeService
僞裝這裏是一些核心業務代碼!
end log:doSomeService
複製代碼