7 AOP
module:spring-09-aop
什么是AOP
AOP(Aspect Oriented Programming)意為:面向切面編程,通過預編譯方式和運行期動態代理實作程式功能的統一維護的一種技術,AOP是OOP的延續,是軟體開發中的一個熱點,也是Spring框架中的一個重要內容,是函式式編程的一種衍生范型,利用AOP可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度降低,提高程式的可重用性,同時提高了開發的效率,

AOP在Spring中的作用
提供宣告式事務;允許用戶自定義切面
以下名詞需要了解下:
- 橫切關注點:跨越應用程式多個模塊的方法或功能,即與我們業務邏輯無關的,但是我們需要關注的部分,就是橫切關注點,如日志 , 安全 , 快取 , 事務等等 ....
- 切面(ASPECT):橫切關注點 被模塊化 的特殊物件,即,它是一個類,
- 通知(Advice):切面必須要完成的作業,即,它是類中的一個方法,
- 目標(Target):被通知物件,
- 代理(Proxy):向目標物件應用通知之后創建的物件,
- 切入點(PointCut):切面通知 執行的 “地點”的定義,
- 連接點(JointPoint):與切入點匹配的執行點,

SpringAOP中,通過Advice定義橫切邏輯,Spring中支持5種型別的Advice:

即AOP在不改變原有代碼的情況下,去增加新的功能!
使用Spring實作AOP
【重點】使用AOP織入,需要匯入相應的jar包
<!--使用aop開發-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
第一種方式:
通過 Spring API 實作
1、撰寫業務類的介面和實作類:
package com.zzb.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
package com.zzb.service;
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("查詢一個用戶");
}
}
2、撰寫環繞增強類,一個前置增強,一個后置增強:
package com.zzb.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("執行了" + target.getClass().getName() + "的" + method.getName() + "方法");
}
}
package com.zzb.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("執行了" + target.getClass().getName() + "的" + method.getName() + "方法,回傳值為" + returnValue);
}
}
3、配置Spring的組態檔,并且實作AOP的切入,注意約束匯入,
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" />
<bean id="log" />
<bean id="afterLog" />
<!--方法一: 使用原生Spring API 介面-->
<!--配置AOP,需要匯入AOP的約束-->
<aop:config>
<!--expression(回傳值 包名 類名 方法名 引數型別)-->
<aop:pointcut id="pointcut" expression="execution(* com.zzb.service.UserServiceImpl.*(..))"/>
<!--執行環繞增強-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
4、測驗
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
System.out.println("==============================================================");
userService.delete();
}
測驗結果:
執行了com.zzb.service.UserServiceImpl的add方法
增加一個用戶
執行了com.zzb.service.UserServiceImpl的add方法,回傳值為null
============================
執行了com.zzb.service.UserServiceImpl的delete方法
洗掉一個用戶
執行了com.zzb.service.UserServiceImpl的delete方法,回傳值為null
Spring的AOP就是將公共的業務 (日志 , 安全等) 和領域業務結合起來 , 當執行領域業務時 , 將會把公共業務加進來 . 實作公共業務的重復利用 . 領域業務更純粹 , 程式員專注領域業務 , 其本質還是動態代理 ,
第二種方式:
自定義類來實作AOP,目標業務依然是userServiceImpl
1、撰寫一個切入類
package com.zzb.diy;
public class DiyPointCut {
public void before(){
System.out.println("===============使用方法前===================");
}
public void after(){
System.out.println("===============使用方法后===================");
}
}
2、配置Spring的組態檔
<!--方法二: 使用切面-->
<bean id="diy" />
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="pointcut" expression="execution(* com.zzb.service.UserServiceImpl2.*(..))"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
3、測驗
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
System.out.println("==============================================================");
userService.delete();
}
測驗結果:
===============使用方法前===================
增加一個用戶
===============使用方法后===================
==============================================================
===============使用方法前===================
洗掉一個用戶
===============使用方法后===================
第三種方式:
使用注解實作
1、撰寫一個注解實作的增強類
package com.zzb.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect // 標注這個類是一個切面
public class AnnotationPointCut {
@Before("execution(* com.zzb.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("-------------程式運行前----------------");
}
@After("execution(* com.zzb.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("-------------程式運行后----------------");
}
@Around("execution(* com.zzb.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("前");
// 執行方法,jp相當于切入點
Object o = jp.proceed();
// 獲得方法簽名
System.out.println(jp.getSignature());
System.out.println("后");
System.out.println(o);
}
}
2、配置Spring的組態檔
<aop:aspectj-autoproxy/>
<bean id="annotationPointCut" />
3、測驗
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
System.out.println("==============================================================");
userService.delete();
}
測驗結果:
前
-------------程式運行前----------------
增加一個用戶
void com.zzb.service.UserService.add()
后
null
-------------程式運行后----------------
==============================================================
前
-------------程式運行前----------------
洗掉一個用戶
void com.zzb.service.UserService.delete()
后
null
-------------程式運行后----------------
關于 aop:aspectj-autoproxy/
通過aop命名空間的<aop:aspectj-autoproxy />宣告自動為spring容器中那些配置@aspectJ切面的bean創建代理,織入切面,當然,spring 在內部依舊采用AnnotationAwareAspectJAutoProxyCreator進行自動代理的創建作業,但具體實作的細節已經被<aop:aspectj-autoproxy />隱藏起來了
<aop:aspectj-autoproxy />有一個proxy-target-class屬性,默認為false,表示使用jdk動態代理織入增強,當配為<aop:aspectj-autoproxy poxy-target-/>時,表示使用CGLib動態代理技術織入增強,不過即使proxy-target-class設定為false,如果目標類沒有宣告介面,則spring將自動使用CGLib動態代理,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/232853.html
標籤:Java
上一篇:6 代理模式
下一篇:8 整合MyBatis
