当我使用顾问程序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);
}
}
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
答案 0 :(得分:0)
使用带注释的spring aop flavor轻松实现
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
答案 1 :(得分:0)
AnnotationMatchingPointcut myLogAnnotationPointCut = new AnnotationMatchingPointcut(MyLog.class, true);
第一个参数应该是类注解或者为空 喜欢
new AnnotationMatchingPointcut(Service.class,MyLog.class, true);