我有一个弹簧书应用程序,效果很好。如果我的应用程序抛出异常,我想向我发送电子邮件通知。我认为aspectj很合适。
首先,我尝试使用 Spring AOP 运行时编织,它适用于公共方法。但是,我也想收到有关私有方法的通知,并且我已经安排了任务。根据 Spring AOP 文档,运行时编织不适用于计划任务和私有方法。因此,我决定在我的引导应用程序中使用 aspectj 编译时间编织。
我在这里找到了官方的 aspectj gradle 插件:
https://plugins.gradle.org/plugin/at.jku.isse.gradient-gradle
我的启动应用程序有这个依赖:
compile group: 'org.springframework.boot', name: 'spring-boot-configuration-processor', version:'1.5.14.RELEASE'
如果我使用默认编译器,我的项目编译得很好。但是如果我使用 AspectJ 编译器,它总是会抱怨:
[ant:iajc] warning Field value processing of @ConfigurationProperty meta-data is not supported
[ant:iajc] warning Hibernate JPA 2 Static-Metamodel Generator 4.3.11.Final
[ant:iajc] error at (no source information available)
D:\work\proj\build\classes\java\main\com\abc\dao\entity\Channel_.java:0::0 Internal compiler error: java.lang.Exception: java.lang.NoClassDefFoundError: org/springframework/boot/configurationprocessor/metadata/JsonMarshaller at org.aspectj.org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:169)
如果我删除该依赖项,aspectj 编译正常,我会得到 jar 文件。但是当我运行我的代码时,我得到了这个:
Caused by: org.springframework.aop.framework.AopConfigException: Advice must be declared inside an aspect type: Offending method 'public void com.proj.aop.AspectConfiguration.afterThrowing(org.aspectj.lang.JoinPoint,java.lang.Throwable)' in class [com.proj.aop.AspectConfiguration]
这是我的建议课:
package com.proj.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;
@Aspect
@Configuration
public class AspectConfiguration {
@AfterThrowing (pointcut="execution(* org.hibernate..*.*(..)) || execution(* com.proj..*.*(..))", throwing="excep")
public void afterThrowing(JoinPoint joinPoint, Throwable excep){
System.out.println("inafterthrowing");
}
}
我的 appconfig@EnableAspectJAutoProxy
添加了注释。
如果我在我的 Advice 类中替换@Configuration
为@Component
,我可以运行我的应用程序,但不会调用 afterthrowing 方法。如果我删除@Configuration
.
所以我的问题是:
- 为什么我在由 AspectJ 编译时得到
java.lang.NoClassDefFoundError: org/springframework/boot/configurationprocessor/metadata/JsonMarshaller
if 我作为依赖项?spring-boot-configuration-processor
- 在 Spring Boot 应用程序中使用 AspectJ 并使用 gradle 构建的正确方法是什么?(使用
@Configuration
or@Component
?为什么如果我使用@Component
or 不会调用我的后续处理,为什么如果我使用会出现异常@Configuration
?)
谢谢