亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用Spring Boot AOP處理方法的入?yún)⒑头祷刂?/h1>
 更新時間:2021年08月16日 10:46:45   作者:Code0cean  
這篇文章主要介紹了使用Spring Boot AOP處理方法的入?yún)⒑头祷刂担哂泻芎玫膮⒖純r值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

前言

IOC和AOP是Spring 中最重要的兩個模塊。這里練習(xí)一下如何使用Spring Boot AOP處理方法的入?yún)⒑头祷刂怠?/p>

Spring AOP的簡單介紹:

AOP(Aspect-Oriented Programming)面向切面編程,通過預(yù)編譯方式和運(yùn)行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護(hù)的一種技術(shù)。AOP能夠?qū)⒛切┡c業(yè)務(wù)⽆關(guān),卻為業(yè)務(wù)模塊所共同調(diào)⽤的邏輯或責(zé)任(例如事務(wù)處理、⽇志管理、權(quán)限控制等)封裝起來,便于減少系統(tǒng)的重復(fù)代碼,降低模塊間的耦合度,并有利于提高系統(tǒng)的可拓展性和可維護(hù)性。

Spring AOP就是基于動態(tài)代理的,如果要代理的對象,實現(xiàn)了某個接⼝,那么Spring AOP會使⽤JDK代理,去創(chuàng)建代理對象,⽽對于沒有實現(xiàn)接⼝的對象,就⽆法使⽤ JDK代理去進(jìn)⾏代理了,這時候Spring AOP會使⽤Cglib ,這時候Spring AOP會使⽤ Cglib代理 ⽣成⼀個被代理對象的⼦類來作為代理,如下圖所示:

一篇詳細(xì)介紹AOP的文章:細(xì)說Spring——AOP詳解(AOP概覽)

1. 需求場景

前段時間實習(xí),遇到了一個需求是這樣的:項目上線前,項目經(jīng)理要求有一個用戶私密信息的字段需要在數(shù)據(jù)庫中加密存儲,從數(shù)據(jù)庫讀取出來后需要解密,正常顯示到用戶界面中。

下面的DEMO中,模擬場景項目經(jīng)理突然覺得這個用戶的身份證號是用戶隱私需要進(jìn)行加密保存,保護(hù)用戶的隱私,

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層方法做切點(diǎn),處理字段的加密解密。

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類代碼:

@Service
public class UserService {
    @Autowired
    UserDao userDao;
    public List<User> getAllUsers() {
        return userDao.getAllUsers();
    }
    public void save(User user) {
        userDao.save(user);
    }
}

Dao層UserDao接口實現(xiàn):

@Mapper
public 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 id="getAllUsers" 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
@Component
public class UserInfoHandler {
    @Pointcut("execution(* top.javahai.springbootdemo.dao.UserDao.*(..))")
    public void pointcut(){
    }
    @Around("pointcut()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        //處理方法參數(shù),如果是User就進(jìn)行加密處理
        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中定義多個切點(diǎn),另外處理的地方需要添加多個判斷。

定義多個切點(diǎn):

    @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ù)庫中

在這里插入圖片描述

查看數(shù)據(jù)庫的存儲:

在這里插入圖片描述

取出所有的用戶數(shù)據(jù):

在這里插入圖片描述

從測試結(jié)果可以看到代碼可以正確的處理方法的入?yún)⒑头祷刂怠?/p>

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論