Springboot中Aspect实现切面(以后台请求为例)

1.添加maven依赖

        <!--开启注解-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

2.定义注解

package com.hz.demo.common.customerAnnotation;

/**
 * @author hz
 * @create 2019- 03- 12 14:03
 */
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface ManageIntecpet {
   /**
    * 请求方法名称
    * @return
    */
   String name();

   /**
    * 是否对该方法(业务)校验
    * @return
    */
   boolean check() default false;
}

 

3.定义切面类

package com.hz.demo.aspect;

import com.hz.demo.common.customerAnnotation.ManageIntecpet;
import com.hz.demo.common.util.LogOut;
import com.hz.demo.persistence.DTO.PublicReturn;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * <p>切面类</p>
 * @author hz
 * @Description: <p>对com.hz.demo.controller.management包 所有类下的映射方法进行拦截</p>
 * <p>环绕通知:执行目标方法前,进行注解所设值校验,执行目标方法后,对返回数据进行修改</p>
 * @create 2019-09-02
 */
@Aspect
@Component
public class ManageAspect {
    @Autowired
    LogOut logOut;
    private final String POINT_CUT = "execution(public * com.hz.demo.controller.management.*.*(..))";

    @Pointcut(POINT_CUT)
    public void pointcut() {

    }

    @Around(value = POINT_CUT)
    public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Exception {
        logOut.info("@Around环绕通知开始进入");
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = signature.getMethod();
        if (method.isAnnotationPresent(ManageIntecpet.class)) {
            ManageIntecpet annotation = method.getAnnotation(ManageIntecpet.class);
            logOut.info("获取注解名称[name:{}]", annotation.name());
            if (annotation.check()) {
                logOut.info("开始执行check()中业务逻辑");
                logOut.info("执行check()中业务逻辑结束");
            }
            PublicReturn obj = null;
            try {
                obj = (PublicReturn) proceedingJoinPoint.proceed(); //可以加参数
                Map object = (Map) obj.getObject();
                object.put("test","@aspect 修改后数据");
                logOut.info("修改后json[{}]", obj);
            } catch (Throwable throwable) {
                throwable.printStackTrace();
                return PublicReturn.builder().code(500).msg("系统异常").build();
            }
            logOut.info("@Around环绕通知执行结束");
            return obj;
        }
        return PublicReturn.builder().code(400).msg("该方法上未设置@ManageIntecpet").build();
    }
}

 

4.测试代码

package com.hz.demo.controller.management;

import com.hz.demo.common.customerAnnotation.ManageIntecpet;
import com.hz.demo.common.util.LogOut;
import com.hz.demo.persistence.DTO.PublicReturn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/manage")
public class TestController {
    @Autowired
    LogOut logOut;

    /**
     * 测试注解
     * @return
     */
    @RequestMapping("/testAspect")
    @ManageIntecpet(name="测试切面注解",check = true)
    public PublicReturn testAspect() {
        Map<String,String> map = new HashMap();
        map.put("test", "普通数据");
        logOut.info(map.toString());
        return PublicReturn.builder().code(200).msg("获取成功").object(map).build();
    }
}

 

5.返回结果

如果方法上没有加 @ManageIntecpet

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值