2

I declared several @Components in src/groovy/my/package of a grails 3.1.10 plugin and added the package name to the component scan by adding the following in grails-app/init/application.groovy:

 @Override
Collection<String> packageNames() {
    super.packageNames() + ['package.from.plugin']
}

It works well as long as I am running the plugin itself. When I include my plugin by adding it in the build.gradle of my application:

compile project(':my-plugin')

and run the application, the beans declared in my-plugin do not get wired properly anymore, they cannot be found.

What is the proper way to add custom Components/Beans from Grails Plugin to Grails application component scan?

Piotr Zakrzewski
  • 3,591
  • 6
  • 26
  • 28

1 Answers1

3

Custom components should be declared in plugin declaration (pluginName.groovy inside src/base.package) inside

Closure doWithSpring() {{->
    xmlns context:"http://www.springframework.org/schema/context"


    context.'component-scan'('base-package': 'your.package') {
        context.'include-filter'(
                type:       'annotation',
                expression: Component.canonicalName)
    }
}}

this will make all Components declared with annotation in your.package available in spring context, also in the main application.

Application class inside grails-app/conf/init/application.groovy does runs only when plugin is being run standalone, outside of the main application context.

Piotr Zakrzewski
  • 3,591
  • 6
  • 26
  • 28