Reputation: 1420
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);
Upvotes: 0
Views: 655
Reputation: 1420
I have solved it using the setProperty method from groovyObjectObject this way:
groovyObject.setProperty("myRepository", myRepositoryImpl);
Upvotes: 0
Reputation: 9868
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)
Upvotes: 1