詳解Java Spring AOP
前言
面向切面編程,利用 AOP 可以對業(yè)務(wù)邏輯的各個部分進行隔離,從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。
即不改變源代碼而添加新功能,可插拔的.
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一.AOP底層原理
1.AOP底層使用動態(tài)代理
有接口:jdk動態(tài)代理,即創(chuàng)建接口實現(xiàn)類代理對象
無接口:CGLIB動態(tài)代理,即創(chuàng)建子類代理對象
jdk動態(tài)代理的實現(xiàn)

創(chuàng)建接口
package com.vector.spring5;
public interface UserDao {
public int add(int a,int b);
public String update(String id);
}
接口實現(xiàn)類
接口實現(xiàn)類的方法,屬于源代碼,用aop思想增添新功能時這里不能動!
package com.vector.spring5;
public class UserDaoImpl implements UserDao{
@Override
public int add(int a, int b) {
return a+b;
}
@Override
public String update(String id) {
return id;
}
}
使用JDK動態(tài)代理對象,增添新功能
package com.vector.spring5;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
public class JDKProxy {
public static void main(String[] args) {
//創(chuàng)建接口實現(xiàn)類代理對象
Class[] interfaces = {UserDao.class};
UserDaoImpl userDao = new UserDaoImpl();
UserDao dao= (UserDao) Proxy.newProxyInstance(JDKProxy.class.getClassLoader(),interfaces,new UserDaoProxy(userDao));
int result = dao.add(1,2);
System.out.println("result: "+result);
}
}
//創(chuàng)建代理對象
class UserDaoProxy implements InvocationHandler{
//有參構(gòu)造傳遞增強對象
private Object obj;
public UserDaoProxy(){};
public UserDaoProxy(Object obj){
this.obj=obj;
}
//增強的邏輯
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//方法之前
System.out.println("方法之前執(zhí)行: "+method.getName()+":傳遞的參數(shù): "+ Arrays.toString(args));
//被增強的方法執(zhí)行
//可以根據(jù)method.getName()判斷選擇增強
Object res = method.invoke(obj,args);
//方法之后
System.out.println("方法之后執(zhí)行: "+obj);
return res;
}
}

jdk代理圖像解析


二.AOP術(shù)語
1.連接點
類里可以被增強的方法,稱為連接點.
2.切入點
類中實際被增強的方法,成為切入點.
3.通知(增強)
(1)實際被增強的方法中的邏輯部分稱為通知(增強).
(2)通知包含:前置通知,后置通知,環(huán)繞通知,異常通知,最終通知
4.切面
把增強應(yīng)用到切入點的過程稱為切面
三.AOP 操作(準備工作)
Spring 框架一般都是基于 AspectJ 實現(xiàn) AOP 操作
(1)AspectJ 不是 Spring 組成部分,獨立 AOP 框架,一般把 AspectJ 和 Spirng 框架一起使用,進行 AOP 操作
maven準備
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.8.RC1</version>
</dependency>
方式一:使用Spring的接口實現(xiàn)增添功能
實現(xiàn)組合crud和日志功能結(jié)合
applicationContext.xml
<context:component-scan base-package="com.vector"/>
<aop:config>
<!-- 切入點: expression:表達式 execution(要執(zhí)行的位置!* * * * *)-->
<aop:pointcut id="pointcut" expression="execution(* com.vector.service.UserServiceImpl.*(..))"/>
<!-- 執(zhí)行環(huán)繞增加!-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
log.java
package com.vector.log;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component("log")
public class Log implements MethodBeforeAdvice {
//method: 要執(zhí)行的目標對象的方法
//args: 參數(shù)
//target: 目標對象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被執(zhí)行了");
}
}
userService.java
package com.vector.service;
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
userServiceImpl.java
package com.vector.service;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一個用戶");
}
}
MyTest.java
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//動態(tài)代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}

方式二:自定義類
DiyPoint.java
package com.vector.diy;
import org.springframework.stereotype.Component;
@Component("diyPointCut")
public class DiyPointCut {
public void before(){
System.out.println("===方法執(zhí)行前===");
}
public void after(){
System.out.println("===方法執(zhí)行后===");
}
}
UserServiceImpl.java
package com.vector.service;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一個用戶");
}
}
applicationContext.xml
<aop:config>
<!-- 自定義切面,ref要引用的類-->
<aop:aspect ref="diyPointCut">
<!-- 切入點-->
<aop:pointcut id="pointcut" expression="execution(* com.vector.service.UserServiceImpl.*(..))"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
MyTest.java
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//動態(tài)代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}

方式三:全注解配置實現(xiàn)
UserServiceImpl.java
package com.vector.service;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService{
@Override
public void add() {
System.out.println("增加了一個用戶");
}
}
AnnotationPointCut.java
package com.vector;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
//標注這個類是一個切面
@Aspect
@Component("annotationPointCut")
//開啟aop注解驅(qū)動
@EnableAspectJAutoProxy
public class AnnotationPointCut {
@Before("execution(* com.vector.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("===方法執(zhí)行前===");
}
@After("execution(* com.vector.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("===方法執(zhí)行后===");
}
}
MyTest.java
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//動態(tài)代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}

總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
spring利用squertz實現(xiàn)定時任務(wù)
spring squertz是一個強大的定時任務(wù)處理方式。下面這篇文章主要介紹了spring利用squertz實現(xiàn)定時任務(wù)的相關(guān)資料,文中介紹的很詳細,對大家具有一定的參考借鑒價值,需要的朋友們下面來一起看看吧。2017-01-01
Spring boot攔截器實現(xiàn)IP黑名單的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring boot攔截器實現(xiàn)IP黑名單的完整步驟,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用Spring boot攔截器具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
解決Jackson反序列化map,set等復(fù)雜類型問題
這篇文章主要介紹了解決Jackson反序列化map,set等復(fù)雜類型問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringBoot解決406錯誤之返回對象缺少Getter/Setter方法引發(fā)的問題
在Spring Boot開發(fā)中,接口請求返回數(shù)據(jù)是系統(tǒng)交互的重要環(huán)節(jié),然而,開發(fā)過程中常常會遇到由于數(shù)據(jù)類型或返回格式問題導(dǎo)致的錯誤,其中最常見的就是406 Not Acceptable異常,本篇文章以實際的案例出發(fā),詳細分析在POST請求中產(chǎn)生406錯誤的原因2024-11-11
Java關(guān)鍵字final、static使用總結(jié)
final方法不能被子類的方法覆蓋,但可以被繼承。用static修飾的代碼塊表示靜態(tài)代碼塊,當(dāng)Java虛擬機(JVM)加載類時,就會執(zhí)行該代碼塊,下面通過本文給大家分享Java關(guān)鍵字final、static使用總結(jié),感興趣的朋友一起看看吧2017-07-07
使用arthas命令redefine實現(xiàn)Java熱更新(推薦)
今天分享一個非常重要的命令 redefine ,主要作用是加載外部的 .class 文件,用來替換 JVM 已經(jīng)加載的類,總結(jié)起來就是實現(xiàn)了 Java 的熱更新,感興趣的朋友跟隨小編一起看看吧2020-05-05

