Question

I have a spring config where I define hundreds of actions which extend MyAction. I have a pool where an execution service can look up actions. I can't use the appContext directly because each action has one or more "keys" which the execution service will use and pool cuts that dependency.

So the pool must be able to collect all beans of type MyAction (or rather beans that extend MyAction).

The method ApplicationContext.getBeansOfType() seems to do what I need but when can I safely call it?

It would be great if I could call it in a @PostConstruct method but is it guaranteed that the bean factory has seen added each and every bean from the config at that time?

Note: Almost all of my beans are @Lazy

Was it helpful?

Solution

You could use injection by constructor and passing a Collection of yours MyAction

Something like

 @Component
 public class Foo {
     private final Set<MyAction> myActions;

     @Inject
     public Foo(Set<MyAction> myActions) { this.myActions = myActions; }
 }

or

 public class Foo {
     private Set<MyAction> myActions;

     @Inject
     public void setMyActions(Set<MyAction> myActions) { this.myActions = myActions; }
 }

Spring will take care of creating the set with all beans that extends MyAction.

In the first case, they are injected by constructor, you can safely use them in any method.

In the second case, Spring will eventually call the setter. You can either do any post processing in the setter or add a @PostConstruct method that works on myActions.

OTHER TIPS

Try to use ListableBeanFactory like this. Also here is API documentation. Method getBeansOfType has parameter allowEagerInit which forces eager init of lazy object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top