Question

I have an Spring-powered app and want to integrate groovy. Specifically, I have one abstract java class with a set of abstract method definitions and one repository inyected with autowired.

This class must be implemented by several final groovy external classes (one for each client).

At this moment, I am calling the Groovy class in java this way:

final Class parsedClass = groovyClassLoader.parseClass(groovyFile);
final GroovyObject groovyObject = (GroovyObject) parsedClass.newInstance();
final Object response = groovyObject.invokeMethod(methodName, methodParameters);

The problem is that I need to autowired the repository variable in each Groovy external class but currently are null.

How can I notify the Groovy class to get the inyected repository variable when I create it at runtime?

Thanks!

Edit

Y have solved it using the setProperty method from groovyObjectObject this way:

groovyObject.setProperty("myRepository", myRepositoryImpl);

No correct solution

OTHER TIPS

The instance here is not created by spring, hence I don't think spring can automagically set the instance of repository in the groovyObject that you have.

However, if you can can autowire the repository into the class thats generating the groovyObject then you can manually inject the repo in the newInstance call.

parsedClass.newInstance(repository:your_autowired_repo_ref)

I have solved it using the setProperty method from groovyObjectObject this way:

groovyObject.setProperty("myRepository", myRepositoryImpl);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top