久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

使用Spring Boot AOP處理方法的入?yún)⒑头祷刂?/h1>
瀏覽:62日期: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 中最重要的兩個模塊。這里練習(xí)一下如何使用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)⽤的邏輯或責(zé)任(例如事務(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. 需求場景

前段時間實習(xí),遇到了一個需求是這樣的:項目上線前,項目經(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)文章:

主站蜘蛛池模板: 思南县| 西丰县| 安宁市| 巢湖市| 长春市| 乳源| 疏勒县| 固阳县| 白河县| 株洲县| 南平市| 寿阳县| 九龙城区| 临武县| 樟树市| 西城区| 石城县| 延安市| 镶黄旗| 利川市| 延长县| 台北市| 七台河市| 垣曲县| 丰台区| 紫云| 海城市| 博兴县| 郎溪县| 阜南县| 灵石县| 苗栗县| 贞丰县| 桐梓县| 平邑县| 承德县| 南丹县| 永安市| 汉阴县| 嵊州市| 西安市|