SpringBoot中Inteceptor 之 MethodInterceptor

时间:2025-04-05 07:27:06

MethodInterceptor是AOP项目中的拦截器,它拦截的目标是方法,即使不是controller中的方法

自定义方法拦截器

package ;

@FunctionalInterface
public interface MethodInterceptor extends Interceptor {
    Object invoke(MethodInvocation var1) throws Throwable;
}

自定义拦截器,实现MethodInterceptor接口的invoke()方法

package ;

import ;
import ;

public class MyMethodInterceptor implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        ("MyMethodInterceptor  "+().getName());
        return ();
    }
}

案例一

使用aspectj execution表达定义切点;使用 AspectJExpressionPointcut定义切点并注册

package ;

import ;
import ;
import ;
import ;
import ;

@Configuration
public class InterceptorConfig {
    public static final String traceExecution = "execution(* ..*.*(..))";

    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor(){
        MyMethodInterceptor methodInterceptor = new MyMethodInterceptor();
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        (traceExecution);

        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        (pointcut);
        (methodInterceptor);
        return advisor;
    }
}

案例二

与案例一的主要区别是用JdkRegexpMethodPointcut来构造切点

package ;

import ;
import ;
import ;
import ;
import ;
import ;

@Configuration
public class InterceptorConfig {
    public static final String traceExecution = "execution(* ..*.*(..))";

    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor() {
        MyMethodInterceptor methodInterceptor = new MyMethodInterceptor();
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        //case 1
        // AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        // (traceExecution);

        //case 2
        JdkRegexpMethodPointcut pointcut2 = new JdkRegexpMethodPointcut();
        (".*");

        (pointcut2);
        (methodInterceptor);
        return advisor;
    }
}

案例三

自定义注解 AspectJExpressionPointcut 

package ;

import .*;

@Target()
@Retention()
@Documented
public @interface InterceptorAnnotation {
}
package ;

import ;
import ;
import ;
import ;
import ;
import ;

@Configuration
public class InterceptorConfig {
    public static final String traceAnnotationExecution = "@annotation()";
    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor() {
        MyMethodInterceptor methodInterceptor = new MyMethodInterceptor();
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        //case 3
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        (traceAnnotationExecution);

        (pointcut);
        (methodInterceptor);
        return advisor;
    }
}

案例四

自定义注解 AnnotationMatchingPointcut

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;

@Configuration
public class InterceptorConfig {

    @Bean
    public DefaultPointcutAdvisor defaultPointcutAdvisor() {
        MyMethodInterceptor methodInterceptor = new MyMethodInterceptor();
        DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
        //case 4
        AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(null, );

        (pointcut);
        (methodInterceptor);
        return advisor;
    }
}

测试类

package ;

import ;

@Service
public class UserService {

    public void saveUser(String name, String sex) {
        ("name = " + name + ", sex = " + sex+" id =");
        sayHello(name);
    }

    public void sayHello(String name){
        ("Hello name = " + name+" Welcome in Spring Interceptor world");
    }
}

请求调用测试 

package ;

import ;
import ;
import ;
import ;
import ;

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value="/" ,method = )
    public void saveUser(String name,String sex){
       (name,sex);

    }
}

启动调用测试

package ;

import ;
import ;
import ;
import ;
import ;

@SpringBootApplication
public class Application {
    @Autowired
    private ApplicationContext applicationContext;
    public static void main(String[] args) {
        ApplicationContext applicationContext =  (, args);

        UserService userService=(UserService) ("userService");
        ("wagnchao","nan");
        ("wangchaochao");
    }

}

 

总结

四种构造Pointcut切点方式

AspectJExpressionPointcut   JdkRegexpMethodPointcut  AnnotationMatchingPointcut