Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7605343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:08:42+00:00 2026-05-31T00:08:42+00:00

I came across a situation that I had in another project that I’m not

  • 0

I came across a situation that I had in another project that I’m not exactly sure the best way to do within Grails. To set it up, this is what I’m doing in a plain Spring project.

I have two classes that inherit from the same interface:

public interface BaseInterface {
    void doSomething();
}

public class Impl1 implements BaseInterface {
    public void doSomething(){
        System.out.println("doing impl 1");
    }
}

public class Impl2 implements BaseInterface {
    public void doSomething(){
        System.out.println("doing impl 2");
    }
}

So far pretty standard, I have N beans that I want to call sequentially to do work. (The example is obviously trivial). Within another Java class I can then do some magic to get all the beans injected(autowired) as an array.

@Autowired(required=false)
    private BaseInterface[] theWorkers;

This will give me an array of worker beans as long as I have added them to the bean container in the configuration.

Now I’m trying to do the same thing in Grails. The same formula doesn’t work. Putting the @Autowired portion in a service, and creating Impl1 and Impl2 within resources.groovy does not seem to do the job. So I’m wondering what the best solution is:

1) I’m missing something simple that will make this work very easily.

2) Do something similar to what’s suggested by duffymo here. I’d create a named bean in resources.groovy that used a custom factory. That factory would emit a class that would contain all the classes implementing a certain interface. I’d use something similar to the suggestion to pull the services/classes matching the criteria then have that service allow someone to iterate over it’s subclasses to do work.

3) Create a named bean for each of the Impl# classes within resources.groovy and then just use their distinct names and inject all of them into the classes individually. This option would not really scale or give much dynamism, but would work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:08:43+00:00Added an answer on May 31, 2026 at 12:08 am

    If you get access to the Spring application context you can call getBeansOfType which returns all known beans that implement a specified interface or extend a specified base class. So I’d register each bean in resources.groovy but also a manager class that gets a reference to the application context and finds the interface implementations for you. You said you want to call them sequentially, so you should implement the Ordered interface too.

    Here’s the manager class (put it in src/groovy/ in the correct folder for the package and rename it to whatever you want):

    package com.foo
    
    import org.springframework.beans.factory.InitializingBean
    import org.springframework.context.ApplicationContext
    import org.springframework.context.ApplicationContextAware
    
    class BaseInterfaceManager implements ApplicationContextAware, InitializingBean {
    
       ApplicationContext applicationContext
    
       List<BaseInterface> orderedImpls
    
       void afterPropertiesSet() {
          orderedImpls = applicationContext.getBeansOfType(BaseInterface).values().sort { it.order }
       }
    }
    

    Then change the beans so they implement Ordered:

    import org.springframework.core.Ordered;
    
    public class Impl1 implements BaseInterface, Ordered {
       public void doSomething(){
          System.out.println("doing impl 1");
       }
    
       public int getOrder() {
          return 42;
       }
    }
    

    and

    import org.springframework.core.Ordered;
    
    public class Impl2 implements BaseInterface, Ordered {
       public void doSomething(){
          System.out.println("doing impl 2");
       }
    
       public int getOrder() {
          return 666;
       }
    }
    

    Register all three in resources.groovy (use whatever bean names you want):

    beans = {
    
       impl1(Impl1)
       impl2(Impl2)
    
       baseInterfaceManager(BaseInterfaceManager)
    }
    

    And then you can add a dependency injection in a service or controller or whatever for the baseInterfaceManager bean and use it to loop through the implementation classes in order:

    class FooService {
    
       def baseInterfaceManager
    
       void someMethod() {
          for (impl in baseInterfaceManager.orderedImpls) {
             impl.doSomething()
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently came across a situation where I had to use XPath as part
Recently I came across a situation where, while using a double-byte language, I had
I came across a situation where I have a pretty big file that I
In answering this question , I came across a situation that I don't understand.
I came across a situation where i had to count the number of occurences
I came across this situation while migrating our DB from Foxpro to SQL. Below
I came across with these situation while building a class diagram for a system,
I am working on a website and I came across an interesting situation. In
I came across with a curious situation when using jamod to write to modbus.
I recently came across the situation where I wanted a set of classes to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.