1、 什麼是AOP
AOP(Aspect Oriented Programming)意爲:面向切面編程,通過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。AOP是OOP的延續,是軟件開發中的一個熱點,也是Spring框架中的一個重要內容,是函數式編程的一種衍生範型。利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發的效率。
2、 Aop在Spring中的作用
提供聲明式事務;允許用戶自定義切面
-
橫切關注點:跨越應用程序多個模塊的方法或功能。即是,與我們業務邏輯無關的,但是我們需要關注的部分,就是橫切關注點。如日誌 , 安全 , 緩存 , 事務等等 …
-
切面(ASPECT):橫切關注點 被模塊化 的特殊對象。即,它是一個類。
-
通知(Advice):切面必須要完成的工作。即,它是類中的一個方法。
-
目標(Target):被通知對象。
-
代理(Proxy):向目標對象應用通知之後創建的對象。
-
切入點(PointCut):切面通知 執行的 “地點”的定義。
-
連接點(JointPoint):與切入點匹配的執行點。
在這裏插入圖片描述
SpringAOP中,通過Advice定義橫切邏輯,Spring中支持5種類型的Advice:
即 Aop 在 不改變原有代碼的情況下 , 去增加新的功能 .
3、 使用Spring實現Aop
【重點】使用AOP織入,需要導入一個依賴包!
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
方式一 : 使用Spring的API 接口 【主要SpringAPI接口實現】
- 首先寫入一個接口類
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
- 實現接口的實現類
public class userServiceImpl implements UserService {
public void add() {
System.out.println("增加");
}
public void delete() {
System.out.println("刪除");
}
public void update() {
System.out.println("修改");
}
public void select() {
System.out.println("查詢");
}
}
- aop的前置增強類
public class BeforeLog implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println(o.getClass().getName()+"的"+method.getName()+"被執行了");
}
}
- aop的後置增強類
public class afterLog implements AfterReturningAdvice {
public void afterReturning(Object o, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法,返回了"+o);
}
}
- aop的applicationContext.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.zj.service.impl.userServiceImpl"/>
<bean id="beforeLog" class="com.zj.config.BeforeLog"/>
<bean id="afterLog" class="com.zj.config.afterLog"/>
<!--方式一:配置增強類-->
<aop:config>
<aop:pointcut id="pc" expression="execution(* com.zj.service.impl.userServiceImpl.*(..))"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pc"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pc"/>
</aop:config>
- 測試類
public class MyTest {
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
- 結果
方式二 : 自定義來實現AOP 【主要是切面定義】
- 在方式一的config的包下增加DiyAOP的配置類
public class DiyPointCut {
public void before(){
System.out.println("執行方法前======");
}
public void after(){
System.out.println("執行方法後=====");
}
}
- aop的applicationContext.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.zj.service.impl.userServiceImpl"/>
<bean id="beforeLog" class="com.zj.config.BeforeLog"/>
<bean id="afterLog" class="com.zj.config.afterLog"/>
<bean id="diy" class="com.zj.config.DiyPointCut"/>
<!-- 方式二:切面增強類-->
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="pc" expression="execution(* com.zj.service.impl.userServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="pc"/>
<aop:after method="after" pointcut-ref="pc"/>
</aop:aspect>
</aop:config>
</beans>
- 測試結果爲
方式三 : 使用註解實現!
- 在方式一的config的包下增加註解的配置類
@Aspect
public class AnnotationPoinCut {
@Before("execution(* com.zj.service.impl.userServiceImpl.*(..))")
public void before(){
System.out.println("執行方法前======");
}
@After("execution(* com.zj.service.impl.userServiceImpl.*(..))")
public void after(){
System.out.println("執行方法後=====");
}
@Around("execution(* com.zj.service.impl.userServiceImpl.*(..))")
public void round(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("環繞前");
System.out.println(joinPoint.getSignature());
Object proceed = joinPoint.proceed();
System.out.println("環繞後");
System.out.println(proceed);
}
}
- aop的applicationContext.xml的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.zj.service.impl.userServiceImpl"/>
<bean id="beforeLog" class="com.zj.config.BeforeLog"/>
<bean id="afterLog" class="com.zj.config.afterLog"/>
<bean id="diy" class="com.zj.config.DiyPointCut"/>
<!--方式三:使用註解增強-->
<bean id="ann" class="com.zj.config.AnnotationPoinCut"/>
<aop:aspectj-autoproxy/>
</beans>
- 測試結果爲