栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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 {


    
    @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;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830796.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号