【发布时间】: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