Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Resolve Spring dependency in Groovy script

Jacopo Freddi August 3, 2018

In my JIRA Server plugin I developed a service that takes a IssueEvent and automates status transitions. I want to be able to call this service in selected projects of my JIRA instance, and the solution we came up with was with a Groovy script: we should be able to load our service in the script and call its methods specifying that the script should only load in certain projects.

We wrote our whole project with dependency injection provided by Spring (@Components and @Autowires) and our service is resolved the same way: the method is provided by an interface, the implementation is annotated with @Component and in the constructor both JIRA API and internal dependencies are @Autowired.

If we implemented the service as a Listener, things would go smoothly, but within the Groovy script we're unable to instantiate the service. We tried:

If loaded with ComponentAccessor OSGi resolver, the service class is resolved but the object is not. Otherwise, all attempts to just instantiate an object without explicitly calling the constructor (or using an empty constructor) will fail because the inner service field is not instantiated.

 

What am I doing wrong? There must be something obvious I'm missing, but I can't find it online.

1 answer

1 accepted

1 vote
Answer accepted
Jacopo Freddi August 6, 2018

The obvious thing I was missing was to annotate my service with the @ExportAsService annotation. This allowed my service to be resolved with the @PluginModule annotation within the groovy script and all ran smoothly:

 

implementation class

@Component
@ExportAsService
public class IssueStatusUpdateServiceImpl implements IssueStatusUpdateService { ... }

groovy script

import com.onresolve.scriptrunner.runner.customisers.WithPlugin
import com.onresolve.scriptrunner.runner.customisers.PluginModule
import my.service.IssueStatusUpdateService

@WithPlugin("my-plugin")

@PluginModule
IssueStatusUpdateService issueStatusUpdateService

issueStatusUpdateService.transitionOnIssueEvent(event)

pom.xml

<plugin>
<configuration>
<instructions>
<Export-Package>
my-service.IssueStatusUpdateService
</Export-Package>
</instructions>
</configuration>
</plugin>

Suggest an answer

Log in or Sign up to answer