Get services extended from a certain interface

27 Views Asked by At

Is possible to get all services extended from a certain interface in grails

So I can collect them within one service

1

There are 1 best solutions below

0
Puneet Behl On BEST ANSWER

Yes, I think you can do this by autowiring to List type. Let's say you have three implementations of an interface BaseService in grails-app/services as:

grails-app/services/exampleapp/BaseService.groovy

package exampleapp

interface BaseService {
}

grails-app/services/exampleapp/OneImplService.groovy

package exampleapp

class OneImplService implements BaseService {
}

grails-app/services/exampleapp/TwoImplService.groovy

package exampleapp

class TwoImplService implements BaseService {
}

Now, you can inject all implementations of BaseSerive as follows:

grails-app/services/exampleapp/TestService.groovy

package exampleapp

import org.springframework.beans.factory.annotation.Autowired

class TestService {

    @Autowired
    List<BaseService> baseServiceList

    void doSomething() {
        assert baseServiceList.size() == 2
    }
}

Please note that the above example is tested with Grails 5 web application. But, I would be surprised if this does not work in the previous versions of Grails.