使用Spring Boot AOP處理方法的入?yún)⒑头祷刂?/h1>
瀏覽:30日期:2023-12-03 14:32:58
目錄前言Spring AOP的簡單介紹:1. 需求場景User類定義如下:2. 解決方案3. 代碼實現(xiàn)Controller層UserController類的代碼:Service層UserService類代碼:Dao層UserDao接口實現(xiàn):UserMapper.xml文件實現(xiàn):使用環(huán)繞通知@Around注解實現(xiàn)定義多個切點:4. 測試查看數(shù)據(jù)庫的存儲:取出所有的用戶數(shù)據(jù):前言IOC和AOP是Spring 中最重要的兩個模塊。這里練習一下如何使用Spring Boot AOP處理方法的入?yún)⒑头祷刂怠?/p>Spring AOP的簡單介紹:
AOP(Aspect-Oriented Programming)面向切面編程,通過預(yù)編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術(shù)。AOP能夠?qū)⒛切┡c業(yè)務(wù)⽆關(guān),卻為業(yè)務(wù)模塊所共同調(diào)⽤的邏輯或責任(例如事務(wù)處理、⽇志管理、權(quán)限控制等)封裝起來,便于減少系統(tǒng)的重復(fù)代碼,降低模塊間的耦合度,并有利于提高系統(tǒng)的可拓展性和可維護性。
Spring AOP就是基于動態(tài)代理的,如果要代理的對象,實現(xiàn)了某個接⼝,那么Spring AOP會使⽤JDK代理,去創(chuàng)建代理對象,⽽對于沒有實現(xiàn)接⼝的對象,就⽆法使⽤ JDK代理去進⾏代理了,這時候Spring AOP會使⽤Cglib ,這時候Spring AOP會使⽤ Cglib代理 ⽣成⼀個被代理對象的⼦類來作為代理,如下圖所示:
![使用Spring Boot AOP處理方法的入?yún)⒑头祷刂? src=]()
一篇詳細介紹AOP的文章:細說Spring——AOP詳解(AOP概覽)
1. 需求場景前段時間實習,遇到了一個需求是這樣的:項目上線前,項目經(jīng)理要求有一個用戶私密信息的字段需要在數(shù)據(jù)庫中加密存儲,從數(shù)據(jù)庫讀取出來后需要解密,正常顯示到用戶界面中。
下面的DEMO中,模擬場景項目經(jīng)理突然覺得這個用戶的身份證號是用戶隱私需要進行加密保存,保護用戶的隱私,
User類定義如下:public class User { private Integer id; private String username; private String password; private String identityNum; //省略getter、setter、toString方法}2. 解決方案
因為是臨時加的需求,考慮到多個實體類中都會有identityNum屬性,為了不侵入原本的業(yè)務(wù)代碼和數(shù)據(jù)處理代碼和業(yè)務(wù)代碼的解耦,一個比較好的方案是使用Spring AOP處理,以DAO層方法做切點,處理字段的加密解密。
3. 代碼實現(xiàn)下面使用Spring Boot+MyBatis實現(xiàn)DEMO,模擬上述場景和解決方案實現(xiàn)。
Controller層UserController類的代碼:@RestController@RequestMapping('/users')public class UserController { @Autowired UserService userService; @GetMapping public List<User> getAllUsers(){return userService.getAllUsers(); } @PostMapping public void save(@RequestBody User user){userService.save(user); }}Service層UserService類代碼:
@Servicepublic class UserService { @Autowired UserDao userDao; public List<User> getAllUsers() {return userDao.getAllUsers(); } public void save(User user) {userDao.save(user); }}Dao層UserDao接口實現(xiàn):
@Mapperpublic interface UserDao { List<User> getAllUsers(); void save(@Param('user') User user);}UserMapper.xml文件實現(xiàn):
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-mapper.dtd'><mapper namespace='top.javahai.springbootdemo.dao.UserDao'> <insert id='save'>insert into user values (#{user.id},#{user.username},#{user.password},#{user.identityNum}) </insert> <select resultType='top.javahai.springbootdemo.entity.User'>select id,username,password,identity_num as identityNum from user </select></mapper>
切面類UserInfoHandler實現(xiàn)如下,這里只是使用字符串截取的方法模擬加密代碼
使用環(huán)繞通知@Around注解實現(xiàn)@Aspect@Componentpublic class UserInfoHandler { @Pointcut('execution(* top.javahai.springbootdemo.dao.UserDao.*(..))') public void pointcut(){ } @Around('pointcut()') public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {//處理方法參數(shù),如果是User就進行加密處理Object[] args = joinPoint.getArgs();for (Object arg : args) { if (arg instanceof List){if (((List) arg).get(0) instanceof User){ ((List<User>) arg).forEach(user->{user.setIdentityNum('encode'+user.getIdentityNum()); });} } if (arg instanceof User){String identityNum = ((User) arg).getIdentityNum();((User) arg).setIdentityNum('encode'+identityNum); }}//執(zhí)行方法,獲取返回值Object obj = joinPoint.proceed();//處理方法返回值if (obj instanceof List){ if (!((List) obj).isEmpty()){if (((List) obj).get(0) instanceof User){ ((List<User>) obj).forEach(data->{data.setIdentityNum(data.getIdentityNum().substring(6)); });} }}return obj; }}
如果是在其他實體類中也存在identityNum身份證字段,則需要在@PointCut中定義多個切點,另外處理的地方需要添加多個判斷。
定義多個切點: @Pointcut('execution(* top.javahai.springbootdemo.dao.UserDao.*(..)) ||' + 'execution(* top.javahai.springbootdemo.dao.ResumeDao.*(..))') public void pointcut(){}4. 測試
通過http://localhost:8080/users接口,將保存一個新的用戶數(shù)據(jù)到數(shù)據(jù)庫中
![使用Spring Boot AOP處理方法的入?yún)⒑头祷刂? src=]()
查看數(shù)據(jù)庫的存儲:![使用Spring Boot AOP處理方法的入?yún)⒑头祷刂? src=]()
取出所有的用戶數(shù)據(jù):![使用Spring Boot AOP處理方法的入?yún)⒑头祷刂? src=]()
從測試結(jié)果可以看到代碼可以正確的處理方法的入?yún)⒑头祷刂怠?/p>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
標簽:
Spring
相關(guān)文章:
1. 解決Python 進程池Pool中一些坑2. 三個不常見的 HTML5 實用新特性簡介3. Python獲取抖音關(guān)注列表封號賬號的實現(xiàn)代碼4. Python使用jupyter notebook查看ipynb文件過程解析5. ajax請求添加自定義header參數(shù)代碼6. python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法7. Python如何讀寫CSV文件8. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究9. php測試程序運行速度和頁面執(zhí)行速度的代碼10. 無線標記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁
IOC和AOP是Spring 中最重要的兩個模塊。這里練習一下如何使用Spring Boot AOP處理方法的入?yún)⒑头祷刂怠?/p>Spring AOP的簡單介紹:
AOP(Aspect-Oriented Programming)面向切面編程,通過預(yù)編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術(shù)。AOP能夠?qū)⒛切┡c業(yè)務(wù)⽆關(guān),卻為業(yè)務(wù)模塊所共同調(diào)⽤的邏輯或責任(例如事務(wù)處理、⽇志管理、權(quán)限控制等)封裝起來,便于減少系統(tǒng)的重復(fù)代碼,降低模塊間的耦合度,并有利于提高系統(tǒng)的可拓展性和可維護性。
Spring AOP就是基于動態(tài)代理的,如果要代理的對象,實現(xiàn)了某個接⼝,那么Spring AOP會使⽤JDK代理,去創(chuàng)建代理對象,⽽對于沒有實現(xiàn)接⼝的對象,就⽆法使⽤ JDK代理去進⾏代理了,這時候Spring AOP會使⽤Cglib ,這時候Spring AOP會使⽤ Cglib代理 ⽣成⼀個被代理對象的⼦類來作為代理,如下圖所示:
一篇詳細介紹AOP的文章:細說Spring——AOP詳解(AOP概覽)
1. 需求場景前段時間實習,遇到了一個需求是這樣的:項目上線前,項目經(jīng)理要求有一個用戶私密信息的字段需要在數(shù)據(jù)庫中加密存儲,從數(shù)據(jù)庫讀取出來后需要解密,正常顯示到用戶界面中。
下面的DEMO中,模擬場景項目經(jīng)理突然覺得這個用戶的身份證號是用戶隱私需要進行加密保存,保護用戶的隱私,
User類定義如下:public class User { private Integer id; private String username; private String password; private String identityNum; //省略getter、setter、toString方法}2. 解決方案
因為是臨時加的需求,考慮到多個實體類中都會有identityNum屬性,為了不侵入原本的業(yè)務(wù)代碼和數(shù)據(jù)處理代碼和業(yè)務(wù)代碼的解耦,一個比較好的方案是使用Spring AOP處理,以DAO層方法做切點,處理字段的加密解密。
3. 代碼實現(xiàn)下面使用Spring Boot+MyBatis實現(xiàn)DEMO,模擬上述場景和解決方案實現(xiàn)。
Controller層UserController類的代碼:@RestController@RequestMapping('/users')public class UserController { @Autowired UserService userService; @GetMapping public List<User> getAllUsers(){return userService.getAllUsers(); } @PostMapping public void save(@RequestBody User user){userService.save(user); }}Service層UserService類代碼:
@Servicepublic class UserService { @Autowired UserDao userDao; public List<User> getAllUsers() {return userDao.getAllUsers(); } public void save(User user) {userDao.save(user); }}Dao層UserDao接口實現(xiàn):
@Mapperpublic interface UserDao { List<User> getAllUsers(); void save(@Param('user') User user);}UserMapper.xml文件實現(xiàn):
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-mapper.dtd'><mapper namespace='top.javahai.springbootdemo.dao.UserDao'> <insert id='save'>insert into user values (#{user.id},#{user.username},#{user.password},#{user.identityNum}) </insert> <select resultType='top.javahai.springbootdemo.entity.User'>select id,username,password,identity_num as identityNum from user </select></mapper>
切面類UserInfoHandler實現(xiàn)如下,這里只是使用字符串截取的方法模擬加密代碼
使用環(huán)繞通知@Around注解實現(xiàn)@Aspect@Componentpublic class UserInfoHandler { @Pointcut('execution(* top.javahai.springbootdemo.dao.UserDao.*(..))') public void pointcut(){ } @Around('pointcut()') public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {//處理方法參數(shù),如果是User就進行加密處理Object[] args = joinPoint.getArgs();for (Object arg : args) { if (arg instanceof List){if (((List) arg).get(0) instanceof User){ ((List<User>) arg).forEach(user->{user.setIdentityNum('encode'+user.getIdentityNum()); });} } if (arg instanceof User){String identityNum = ((User) arg).getIdentityNum();((User) arg).setIdentityNum('encode'+identityNum); }}//執(zhí)行方法,獲取返回值Object obj = joinPoint.proceed();//處理方法返回值if (obj instanceof List){ if (!((List) obj).isEmpty()){if (((List) obj).get(0) instanceof User){ ((List<User>) obj).forEach(data->{data.setIdentityNum(data.getIdentityNum().substring(6)); });} }}return obj; }}
如果是在其他實體類中也存在identityNum身份證字段,則需要在@PointCut中定義多個切點,另外處理的地方需要添加多個判斷。
定義多個切點:@Pointcut('execution(* top.javahai.springbootdemo.dao.UserDao.*(..)) ||' + 'execution(* top.javahai.springbootdemo.dao.ResumeDao.*(..))') public void pointcut(){}4. 測試
通過http://localhost:8080/users接口,將保存一個新的用戶數(shù)據(jù)到數(shù)據(jù)庫中
從測試結(jié)果可以看到代碼可以正確的處理方法的入?yún)⒑头祷刂怠?/p>
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
