代码之家  ›  专栏  ›  技术社区  ›  Tonney Bing

spring boot DefaultPointcutAdvisor不工作

  •  -1
  • Tonney Bing  · 技术社区  · 8 年前

    当我使用advisor api实现这样的aop日志时,但它不起作用。 这是advisor配置类。

    @Configuration
    public class AspectAutoConfig {
    
        @Bean
        public DefaultPointcutAdvisor myLogAnnotation() {
            DefaultPointcutAdvisor myLogAdvisor = new DefaultPointcutAdvisor();
            myLogAdvisor.setOrder(Ordered.HIGHEST_PRECEDENCE + 500);
            AnnotationMatchingPointcut myLogAnnotationPointCut =
                    new AnnotationMatchingPointcut(MyLog.class, true);
            MethodLogInterceptor logInterceptor = new MethodLogInterceptor();
            myLogAdvisor.setPointcut(myLogAnnotationPointCut);
            myLogAdvisor.setAdvice(logInterceptor);
            return myLogAdvisor;
        }
    }
    

    这是拦截器类。

    public class MethodLogInterceptor implements MethodInterceptor {
    
        @Override
        public Object invoke(MethodInvocation invocation) throws Throwable {
            System.out.println("methodName = " + invocation.getMethod().getName()
                    + " , arg[] = " + Arrays.toString(invocation.getArguments()));
            try {
                return invocation.proceed();
            } catch (Exception e) {
                throw e;
            } finally {
                System.out.println("finish invoke " + invocation.getMethod().getName());
            }
        }
    }
    

    这是应用程序类。

    @EnableAspectJAutoProxy(proxyTargetClass = true)
    @SpringBootApplication
    public class AspectApiApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(AspectApiApplication.class, args);
        }
    }
    

    pom。

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
        </dependencies>
    

    但它不起作用,有人能帮我吗。谢谢 github示例是 https://github.com/luolibing/coding-life/tree/master/spring-boot-sample/spring-boot-aspect-api

    2 回复  |  直到 8 年前
        1
  •  0
  •   Bhushan Uniyal    8 年前

    轻松使用spring aop flavor和注释

    1: 您已经启用了@EnableSpectJautoProxy

     @SpringBootApplication
        @EnableAspectJAutoProxy
        class Application {
        //main class
        }
    

    您只需创建自定义方面逻辑

    @Aspect
    @Component
    public class CustomAspect  {
    
        @Pointcut("@annotation(cn.tim.aspect.api.annotation.MyLog)")
        public void target() {};
    
    @Before("target()")
    public void before() {
    //do what you want
    }
    }
    

    您可以根据您的要求在周围、@之前、@之后以及更多 这里是 doc

    还有一些 example of it in github

        2
  •  0
  •   pampus    5 年前
    AnnotationMatchingPointcut myLogAnnotationPointCut = new AnnotationMatchingPointcut(MyLog.class, true);
    

    第一个参数应为class annotation或null 喜欢

    new AnnotationMatchingPointcut(Service.class,MyLog.class, true);