Search code examples
springgrailsgroovycomponent-scan

Grails: How to specify Spring component scan exclusions using the bean build DSL


I have some Spring components declared using annotations and I need to use them inside a Grails application.

What I need to do is a component scan from a base package, while excluding some of the components inside the package, using BeanBuilder DSL in resources.groovy. This is how I would achieve this using XML configuration:

<context:component-scan base-package="my.base.package" >
     <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

In the docs (http://grails.org/doc/latest/guide/spring.html#theBeanBuilderDSLExplained, section Using Spring Namespaces) are some hints on how to use the Spring namespaces inside BeanBuilder. This, for example, effectively imports all components inside my.base.package:

xmlns context:"http://www.springframework.org/schema/context"
context.'component-scan'('base-package': "my.base.package")

But I don't know how to specify the exclusion filter through the nested context:exclude-element using the BeanBuilder DSL syntax. I tried the following, to no avail:

xmlns context:"http://www.springframework.org/schema/context"
context.'component-scan'('base-package': "webcat.purchaseorder") {
    context.exclude-filter(type:"annotation", expression:"org.springframework.stereotype.Controller") 
}

Can anybody point me in the right direction? I also tried putting the xml inside a file and then imported it via importBeans, which did work, but I'd really like to use the DSL syntax directly.


Solution

  • try

    context."exclude-filter"(type:"annotation", expression:"org.springframework.stereotype.Controller")