Spring Boot使用方法切面某个有注解的方法

Spring Boot使用方法切面某个有注解的方法,第1张

package org.example.annotation;

import java.lang.annotation.*;

@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DemoAnnotation {

    String value() default "test";

}
package org.example.config;

import lombok.extern.slf4j.Slf4j;
import org.example.interceptor.DemoInterceptor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(name = "aop.exception.pointcut", matchIfMissing = true)
@Slf4j
public class AopConfig {


    /**
     * 异常的切面
     *
     * @return
     */
    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor2() {
        // 声明切点
        //JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        //pointcut.setPatterns("com.example.*");
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("@annotation(org.example.annotation.DemoAnnotation)"); // 拦截org.example.service包和子包下带一个参数的任何方法

        // 声明增强
        DemoInterceptor interceptor = new DemoInterceptor();
        // 配置切面
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        advisor.setPointcut(pointcut);
        advisor.setAdvice(interceptor);
        return advisor;
    }
}
package org.example.service.impl;

import org.example.annotation.DemoAnnotation;
import org.example.entity.User;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    @DemoAnnotation("insert")
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public User findById(Long id) {
        return userMapper.selectById(id);
    }
}
package org.example.interceptor;
import cn.hutool.json.JSONUtil;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.example.annotation.DemoAnnotation;

import java.lang.reflect.Method;

public class DemoInterceptor implements MethodInterceptor {

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        System.out.println("--------------------------------------------------");
        // 参数
        Object[] object = methodInvocation.getArguments();
        for (Object obj : object) {
            System.out.println(JSONUtil.toJsonStr(obj));
        }

        // 对象
        Object obj = methodInvocation.getThis();
        System.out.println(obj.getClass().getName());

        // 方法
        Method method = methodInvocation.getMethod();
        System.out.println(method.getName());


        // 继续执行,obj是方法执行结果,有可能是void方法,此时obj == null
        obj = methodInvocation.proceed();
        if (obj != null){
            System.out.println(obj.getClass().getName());
        }

        // 获取注解
        DemoAnnotation d = method.getAnnotation(DemoAnnotation.class);
        if (d == null) {
            System.out.println("当前类没有DemoAnnotation注解");
        } else {
            System.out.println(d.value());
        }

        System.out.println("--------------------------------------------------");
        return obj;
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/723376.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存