[英]Injecting Spring bean in a Groovy class
I am trying to autowire a spring bean into a groovy class. 我正在尝试将春豆自动接线到常规类中。 The groovy class itself is not instantiated by a Spring container, so I am using the @Configurable annotation to have spring autowire the bean.
groovy类本身不是由Spring容器实例化的,所以我使用@Configurable注释使Spring自动装配bean。 When I create a the groovy class(AcceptFilter) using the new operator, I can see that Spring is properly injecting the bean however when I use the following:
当我使用new运算符创建groovy类(AcceptFilter)时,可以看到Spring正确地注入了bean,但是当我使用以下代码时:
GroovyClassLoader groovyCompiler = new GroovyClassLoader();
Class clazz = groovyCompiler.parseClass(new File("src/main/groovy/filters/pre/AcceptFilter.groovy"));
AcceptFilter filter = (AcceptFilter) clazz.newInstance();
the injected bean in the AcceptFilter class is always null. 在AcceptFilter类中注入的bean始终为null。 It almost seems as if AspectJ is ignoring the @Configurable annotation when the groovy class is compiled and instantiated at runtime.
当groovy类在运行时被编译和实例化时,AspectJ几乎似乎忽略了@Configurable注释。
I ended up creating a base Java class, declared all my dependencies there and had the Groovy class extend that. 我最终创建了一个基础Java类,在那里声明了我的所有依赖关系,并让Groovy类对此进行了扩展。 I also had to change the @Configurable to use preConstruction = true
我还必须更改@Configurable以使用preConstruction = true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.