目前我正在使用 AspectJ Load Time Weaving 来拦截基本实体的构造函数以进行审计。然而,当运行应用程序时,我得到的结果非常不一致,这些结果围绕aspectJ 编织到LTW 类中的aspectOf() 方法。
在某些情况下,应用程序运行,编织正确完成,并且代码按预期运行。其他时候我遇到的人:
java.lang.NoSuchMethodException: ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect.aspectOf()
目前我正在使用 https://github.com/subes/invesdwin-instrument 将检测代理动态附加到 JVM 中,因此我们的部署人员不需要进行任何额外的配置。
我的 Spring 应用程序主:
@SpringBootApplication
@EntityScan(basePackages = {"ca.gc.cfp.model"})
public class CfpWsApplication {
public static void main(final String[] args) {
DynamicInstrumentationLoader.waitForInitialized();
DynamicInstrumentationLoader.initLoadTimeWeavingContext();
if (!InstrumentationLoadTimeWeaver.isInstrumentationAvailable()) {
throw new IllegalStateException(
"Instrumentation is not available AspectJ weaver will not function.");
}
SpringApplication.run(CfpWsApplication.class, args);
}
LTW方面:
@Aspect
public class BaseEntityAspect {
Logger logger = LoggerFactory.getLogger(BaseEntityAspect.class);
/** Application context property needed to fetch the AuditDate bean */
@Autowired private ApplicationContext context;
@AfterReturning(
"onBaseEntityCreated() && !within(ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect)")
public void injectAuditTimeStamp(JoinPoint joinPoint) {
try {
AuditDate auditDate = context.getBean(AuditDate.class);
Object entityTarget = joinPoint.getTarget();
// Inject the auditing date for this Entity instance
if (entityTarget instanceof BaseEntity) {
BaseEntity baseEnt = (BaseEntity) entityTarget;
baseEnt.setAuditDate(auditDate.getAuditingTimeStamp());
}
} catch (NullPointerException e) {
logger.error(
e.getMessage()
+ " Not yet in the conext of an httpRequest, the AuditDate bean has not yet been instantiated.");
}
}
@Pointcut(
"execution(ca.gc.cfp.model.entity.BaseEntity.new(..)) && !within(ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect)")
public void onBaseEntityCreated() {}
}
带有临时工厂方法的 Aspect 配置类,使用 Aspects utils 让 spring 知道它应该向spectJ询问编织的 Aspect:
@Configuration
@EnableAspectJAutoProxy
public class AspectConfig {
/**
* Static factory for access to the load time woven aspect. This allows the aspect to be injected
* with the application context and beans from the spring IoC
*/
@Bean
public BaseEntityAspect getBaseEntityAspect() {
return Aspects.aspectOf(BaseEntityAspect.class);
}
}
aop.xml:
<aspectj>
<weaver options="-verbose -showWeaveInfo -Xreweavable -debug">
<include within="ca.gc.cfp.model" />
<include within="ca.gc.cfp.model..*" />
<include within="ca.gc.cfp.core.cfpws.repository.aspect..*"/>
</weaver>
<aspects>
<aspect name="ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect" />
</aspects>
</aspectj>
在很多情况下,当我使用此配置运行时,我会得到以下信息:
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect]: Factory method 'getBaseEntityAspect' threw exception; nested exception is org.aspectj.lang.NoAspectBoundException: Exception while initializing ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect: java.lang.NoSuchMethodException: ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect.aspectOf()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:622) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
... 19 common frames omitted
Caused by: org.aspectj.lang.NoAspectBoundException: Exception while initializing ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect: java.lang.NoSuchMethodException: ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect.aspectOf()
at org.aspectj.lang.Aspects.aspectOf(Aspects.java:50) ~[aspectjrt-1.9.4.jar:1.9.4]
at ca.gc.cfp.core.cfpws.configuration.AspectConfig.getBaseEntityAspect(AspectConfig.java:22) ~[classes/:na]
at ca.gc.cfp.core.cfpws.configuration.AspectConfig$$EnhancerBySpringCGLIB$$1cae4c58.CGLIB$getBaseEntityAspect$0(<generated>) ~[classes/:na]
at ca.gc.cfp.core.cfpws.configuration.AspectConfig$$EnhancerBySpringCGLIB$$1cae4c58$$FastClassBySpringCGLIB$$84edb9e.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244) ~[spring-core-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363) ~[spring-context-5.1.4.RELEASE.jar:5.1.4.RELEASE]
at ca.gc.cfp.core.cfpws.configuration.AspectConfig$$EnhancerBySpringCGLIB$$1cae4c58.getBaseEntityAspect(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_211]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_211]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_211]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_211]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.1.4.RELEASE.jar:5.1.4.RELEASE]
... 20 common frames omitted
Caused by: java.lang.NoSuchMethodException: ca.gc.cfp.core.cfpws.repository.aspect.BaseEntityAspect.aspectOf()
at java.lang.Class.getDeclaredMethod(Class.java:2130) ~[na:1.8.0_211]
at org.aspectj.lang.Aspects.getSingletonOrThreadAspectOf(Aspects.java:134) ~[aspectjrt-1.9.4.jar:1.9.4]
at org.aspectj.lang.Aspects.aspectOf(Aspects.java:45) ~[aspectjrt-1.9.4.jar:1.9.4]
... 31 common frames omitted
但是情况并非总是如此,有时应用程序运行得很好,代码按预期执行并且没有任何问题。这个问题已经困扰我几周了,我似乎无法弄清楚是什么导致这段代码偶尔工作,有时不工作。难道是 CGLIB 代理向编译器隐藏了aspectOf()方法吗??
编辑/更新:我能够删除使用上述第三方依赖项将 java 代理动态加载到 Spring 应用程序上下文中。相反,我使用了 javaagent 参数,并且它在我的 IDE 中运行良好。然而,在终端中通过 Maven 构建和运行仍然会导致问题。我已经通过环境变量指定了 javaagent 参数:MAVEN_OPTS。这样做之后,行家似乎正在捡起它,但我的课程仍然没有被编织。
关于您自己的答案:AspectJ Maven 插件可以帮助您进行编译时编织(CTW),而不是加载时编织(LTW)。对于 LTW,您需要确保编织代理在加载任何目标类之前处于活动状态,因为 LTW 在类加载器级别工作。仅当您在本机语法(不是基于注释)中使用方面或想要使用 CTW 时,才需要 AspectJ Maven。 我不知道这个
invesdwin-instrument 工具,但 AspectJ 编织器提供了自己在运行时附加它的功能。第三方工具应该不是必需的。不管怎样,我确实建议修改 Java 命令行,以确保在容器中加载其他任何内容之前编织代理已就位。您的部署人员应该帮助您确保 -javaagent:...
参数存在。这是他们的工作!解决这个问题是为了让他们的生活更轻松,但应用程序的行为可能不可预测,这不会提高其稳定性。
如果您在 JRE 9+ 上以 可执行 JAR 的形式运行应用程序(Spring Boot 或其他类型),可能 Agent Embedder Maven Plugin 就是您想要的。 它使您能够在可执行 JAR 中嵌入 Java 代理,并使用
Launcher-Agent-Class
JVM 机制
自动启动它。此插件添加的独特功能,并且通过原始 JVM 机制不可用:您可以
嵌入并运行
P.S.:对于 AspectJ 编织器,由于 JVM 很早就启动代理,编织将处于活动状态,无需额外的 Spring 配置,并且它应该适用于所有类加载器 - 不再出现热附加编织器时出现的
ClassLoader [...] does NOT provide an 'addTransformer(ClassFileTransformer)' method
错误通过
spring-instrument.jar
。