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 {


    /**
     * 异常的切面
     *
     * @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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值