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

  • Home
  • SEARCH
  • 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 6322107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:16:10+00:00 2026-05-24T16:16:10+00:00

I have a system which is using Spring for dependency injection. I use annotation-based

  • 0

I have a system which is using Spring for dependency injection. I use annotation-based autowiring. The beans are discovered by component scanning, i.e. my context XML contains this:

<context:component-scan base-package="org.example"/>

I have created a noddy example below to illustrate my problem.

There is a Zoo which is a container for Animal objects. The developer of Zoo does not know which Animal objects will be contained whilst he is developing Zoo; the set of concrete Animal objects instantiated by Spring is known at compile-time, but there are various build profiles resulting in various sets of Animals, and the code for Zoo must not change under these circumstances.

The purpose of Zoo is to allow other parts of the system (illustrated here as ZooPatron) to access the set of Animal objects at runtime, without needing to depend explicitly on certain Animals.

Actually, the concrete Animal classes will all be contributed by various Maven artifacts. I want to be able to assemble a distribution of my project by simply depending on the various artifacts containing these concrete Animals, and have everything autowire correctly at compile-time.

I have attempted to solve this problem (unsuccessfully) by having the individual Animals depend upon the Zoo, in order that they can call a registration method on the Zoo during @PostConstruct. This avoids the Zoo depending explicitly on an explicit list of Animals.

The problem with this approach is that the customers of Zoo wish to interact with it only when all the Animals have registered. There is a finite set of Animals which is known at compile-time, and the registration all happens during the Spring wiring phase of my lifecycle, so a subscription model should be unneccesary (i.e. I don’t wish to add Animals to the Zoo at runtime).

Unfortunately, all the customers of Zoo simply depend upon Zoo. This is exactly the same relationship which the Animals have with Zoo. Therefore, the @PostConstruct methods of the Animals and ZooPatron are called in an arbitrary sequence. This is illustrated with the example code below – at the time @PostConstruct is invoked on ZooPatron, no Animals have registered, it is some milliseconds later when they all register.

So there are two types of dependency here, which I am struggling to express in Spring. The customers of Zoo only want to use it once all the Animals are in it. (perhaps “Ark” would have been a better example…)

My question is basically: what is the best way to solve this problem?

@Component
public class Zoo {

    private Set<Animal> animals = new HashSet<Animal>();

    public void register(Animal animal) {
        animals.add(animal);
    }

    public Collection<Animal> getAnimals() {
        return animals;
    }

}

public abstract class Animal {

    @Autowired
    private Zoo zoo;

    @SuppressWarnings("unused")
    @PostConstruct
    private void init() {
        zoo.register(this);
    }

    @Component
    public static class Giraffe extends Animal {
    }

    @Component
    public static class Monkey extends Animal {
    }

    @Component
    public static class Lion extends Animal {
    }

    @Component
    public static class Tiger extends Animal {
    }

}

public class ZooPatron {

    public ZooPatron(Zoo zoo) {
        System.out.println("There are " + zoo.getAnimals().size()
                             + " different animals.");
    }

}

@Component
public class Test {

    @Autowired
    private Zoo zoo;

    @SuppressWarnings("unused")
    @PostConstruct
    private void init() {
        new Thread(new Runnable() {
            private static final int ITERATIONS = 10;
            private static final int DELAY = 5;
            @Override
            public void run() {
                for (int i = 0; i<ITERATIONS; i++) {
                    new ZooPatron(zoo);
                    try {
                        Thread.sleep(DELAY);
                    } catch (InterruptedException e) {
                        // nop
                    }
                }
            }
        }).start();     
    }

}

public class Main {

    public static void main(String... args) {
        new ClassPathXmlApplicationContext("/context.xml");
    }

}

Output:

There are 0 different animals.
There are 3 different animals.
There are 4 different animals.
There are 4 different animals.
... etc

Explanation of accepted solution

Basically the answer is: no, you cannot guarantee the order of @PostConstruct calls without either going “outside” Spring or modifying its behaviour.

The real problem here was not that I wanted to sequence the @PostConstruct invocations, that was merely a symptom of the dependencies being expressed incorrectly.

If the consumers of Zoo depend upon him, and Zoo in turn depends upon Animals, everything works correctly. My mistake was that I didn’t want Zoo to depend upon an explicit list of Animal subclasses, and therefore introduced this registration method. As pointed out in the answers, mixing a self-registration mechanism with dependency injection will never work without unnecessary complexity.

The answer is to declare that Zoo is dependent upon a collection of Animals, then allow Spring to populate the collection through auto-wiring.

Thus, there is no hard list of collection members, they are discovered by Spring, but the dependencies are correctly expressed and therefore the @PostConstruct methods happen in the sequence I want.

Thanks for the excellent answers.

  • 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-24T16:16:11+00:00Added an answer on May 24, 2026 at 4:16 pm

    You might instead have the Set of Animals @Injected into the Zoo.

    @Component
    public class Zoo {
    
        @Inject
        private Set<Animal> animals = new HashSet<Animal>();
    
        // ...
    }
    

    Then Zoo’s @PostConstruct should only be called once all the Animals are injected. The only gotcha is that there must be at least one Animal in the system, but it doesn’t sound like that should be an issue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a system which is writing files to a folder using FTP. I
I have started using Linq to SQL in a (bit DDD like) system which
I have a J2EE-based system which is running currently on Apache Tomcat. We are
I have a simple (mass)-spring system wih two points which are connected with a
I have a system which sits on a web server and generates files on
I have a web system which has a classical parent-children menu saved in a
I have a system in which different server processes are handling requests passed as
I have a current system which is build as a Windows Application, and does
I have a logger system which basically is a fancy way of writing my
We are an organisation who have purchased a system which is used by doctors

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.