简体   繁体   English

Spring AOP 与 GraalVM 原生镜像

[英]Spring AOP with GraalVM Native Image

Is there any way to use aspects in Spring Boot GraalVM native-image?有没有办法在 Spring Boot GraalVM native-image 中使用方面? I need it for logging purpose.我需要它来记录日志。 I got following error on image run:图像运行时出现以下错误:

Caused by: org.aspectj.weaver.BCException: AspectJ internal error

        at org.aspectj.weaver.reflect.ReflectionWorld.makeAnnotationFinderIfAny(ReflectionWorld.java:132) ~[na:na]
        at org.aspectj.weaver.reflect.ReflectionWorld.<init>(ReflectionWorld.java:97) ~[na:na]
        at org.aspectj.weaver.reflect.ReflectionWorld.getReflectionWorldFor(ReflectionWorld.java:51) ~[na:na]
        at org.aspectj.weaver.tools.PointcutParser.setClassLoader(PointcutParser.java:222) ~[na:na]
        at org.aspectj.weaver.tools.PointcutParser.<init>(PointcutParser.java:208) ~[na:na]
        at org.aspectj.weaver.tools.PointcutParser.getPointcutParserSupportingSpecifiedPrimitivesAndUsingSpecifiedClassLoaderForResolution(PointcutParser.java:170) ~[na:na]
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.initializePointcutParser(AspectJExpressionPointcut.java:242) ~[na:na]
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.buildPointcutExpression(AspectJExpressionPointcut.java:221) ~[na:na]
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.obtainPointcutExpression(AspectJExpressionPointcut.java:198) ~[na:na]
        at org.springframework.aop.aspectj.AspectJExpressionPointcut.getClassFilter(AspectJExpressionPointcut.java:177) ~[na:na]
        at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:226) ~[na:na]
        at org.springframework.aop.support.AopUtils.canApply(AopUtils.java:289) ~[na:na]
        at org.springframework.aop.support.AopUtils.findAdvisorsThatCanApply(AopUtils.java:321) ~[na:na]
        at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findAdvisorsThatCanApply(AbstractAdvisorAutoProxyCreator.java:128) ~[com.fon.footballfantasy.FootballFantasyApplication:5.3.1]
        at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findEligibleAdvisors(AbstractAdvisorAutoProxyCreator.java:97) ~[com.fon.footballfantasy.FootballFantasyApplication:5.3.1]
        at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.getAdvicesAndAdvisorsForBean(AbstractAdvisorAutoProxyCreator.java:78) ~[com.fon.footballfantasy.FootballFantasyApplication:5.3.1]
        at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:337) ~[com.fon.footballfantasy.FootballFantasyApplication:5.3.1]
        at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:289) ~[com.fon.footballfantasy.FootballFantasyApplication:5.3.1]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:444) ~[na:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[na:na]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:609) ~[na:na]
        ... 37 common frames omitted

I suppose that problem is Spring AOP runtime weaving, but how to solve it?我想这个问题是 Spring AOP 运行时编织,但是如何解决呢?

EDIT:编辑:
Thank you for answers.谢谢你的回答。 Sorry for not providing additional info earlier.很抱歉没有提前提供更多信息。
Sample project: https://github.com/programer20/graalvm-demo示例项目: https://github.com/programer20/graalvm-demo
I was creating native image by following official documentation getting started steps https://repo.spring.io/milestone/org/springframework/experimental/spring-graalvm-native-docs/0.8.3/spring-graalvm-native-docs-0.8.3.zip./reference/index.html#_getting_started我正在按照官方文档入门步骤https://repo.spring.io/milestone/org/springframework/experimental/spring-graalvm-native-docs/0.8.3/spring-graalvm-native-docs-创建本机图像0.8.3.zip./reference/index.html#_getting_started
I tried with both 0.8.3 and 0.8.5 versions.我尝试了 0.8.3 和 0.8.5 版本。

I think you are right about the problem.我认为你对这个问题是正确的。 If you were doing build time weaving, it would be totally fine as the modified byte code will be fed into GraalVM native-image for analysis and inclusion in the image.如果您在构建时编织,那将完全没问题,因为修改后的字节码将被输入 GraalVM native-image以进行分析并包含在图像中。 If doing loadtime weaving I believe it can work but haven't confirmed recently if you use loadtime weaving at the point the native-image is being built (via setting the java options to include the aspectjweaver agent), the classes will be woven as they are loaded and the woven form will be included in the image.如果进行加载时间编织,我相信它可以工作,但最近尚未确认是否在构建本机映像时使用加载时间编织(通过设置 java 选项以包含 aspectjweaver 代理),这些类将被编织,因为它们已加载,编织形式将包含在图像中。 It can never really work at image runtime because there is no notion of classes any more, and classes cannot be dynamically defined.它永远不会在图像运行时真正起作用,因为不再有类的概念,并且类不能动态定义。

So yes, since Spring AOP can be done quite late on, as configuration is resolved, there may be problems.所以是的,因为 Spring AOP 可以在很晚的时候完成,随着配置的解决,可能会有问题。 Take a look at the spring native project for the very latest support building your Spring projects into native-images, but we have no samples there for Spring AOP right now as I recall.查看spring 本机项目,以获得将 Spring 项目构建为本机映像的最新支持,但我们现在没有 Spring AOP 的示例。 I'd encourage you to raise issues against that project, including a sample project that show your specific problem can be invaluable.我鼓励您针对该项目提出问题,包括显示您的特定问题的示例项目可能非常宝贵。 You haven't mentioned how you are creating the native-image right now which may influence my recommendations.您还没有提到您现在如何创建本机图像,这可能会影响我的建议。 I think pushing some analysis/weaving a bit earlier in the process could make it work but haven't been into that space yet.我认为在这个过程中更早地推动一些分析/编织可以使它起作用,但还没有进入那个领域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM