简体   繁体   中英

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. 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:

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. It almost seems as if AspectJ is ignoring the @Configurable annotation when the groovy class is compiled and instantiated at runtime.

I ended up creating a base Java class, declared all my dependencies there and had the Groovy class extend that. I also had to change the @Configurable to use preConstruction = true

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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