【问题标题】:spring boot DefaultPointcutAdvisor not work春季启动 DefaultPointcutAdvisor 不起作用
【发布时间】:2018-03-24 10:43:36
【问题描述】:

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

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

绒球。

<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

【问题讨论】:

  • 你为什么要以你现在的方式来做这件事?恕我直言,有更好的方法来做到这一点。

标签: java spring-boot spring-aop


【解决方案1】:

使用带有注解的spring aop风味更容易

1:您已经启用了@EnableAspectJAutoProxy

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

您可以根据您的要求@Around、@Before @After 等等 这里是doc

这里还有一些example of it in github

【讨论】:

  • Aspect 注解的方式,我知道。但我想试试 api
【解决方案2】:
AnnotationMatchingPointcut myLogAnnotationPointCut = new AnnotationMatchingPointcut(MyLog.class, true);

第一个参数应该是类注解或null 喜欢

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

【讨论】:

    猜你喜欢
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 2018-09-04
    • 2017-04-28
    • 2018-06-12
    • 1970-01-01
    • 2017-03-07
    • 2021-08-10
    相关资源
    最近更新 更多