Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dantonyuk/2740d6934c0bfb6553267139eade97da to your computer and use it in GitHub Desktop.
Save dantonyuk/2740d6934c0bfb6553267139eade97da to your computer and use it in GitHub Desktop.

Injecting Multiple Beans

Suppose you are implementing a pluggable application. By "pluggable" it is meant that when the application is running, there may be jar-files in the classpath that provide some additional functionality and came from 3rd party sides. It means you do not control them. You do not know how many of them there, you do not know the exact implementations in them.

To provide an entrypoint the application has Plugin interface:

public interface Plugin {

  void init(AppContext appContext);

  // ...
}

So any plugin implemented by anyone can get the access to the application and do something with it. It does not really matter what they can and what they can't. What matters is that any plugin should be a bean in the application Spring container.

To manage the plugins you have PluginManager defined in the application as follows:

@Component
public class PluginManager {
  // ...
}

Since PluginManager should work with all the plugins that are already in the Spring container, we should somehow let it know about them. How can we do that? Note that we know neither how many of them are in the container nor what are the implementations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment