Will Spring allow me to build a Java application with Dependency Injection testing and a plugin architecture?

StackOverflow https://stackoverflow.com/questions/22916151

Question

If I use Spring (or another Java web application framework), do I need to build a plugin system for my application on top of that? Isn't the part of the purpose of using a framework so that programmer doesn't have to build the architecture, it's there and I merely need to hook into it (via Dependency Injection, xml configuration, or some other way)?

I asked a similar question on the Programming StackExchange, but I don't think I should have put it there. I worded my question a little different here for specificity on SO.

EDIT: I may not be wording this correctly. To me web application, for this question's context, meant the back end of the system, not the UI, Javascript, etc. Only switching out classes in the application (mock database for testing, logger, etc.).

EDIT: Thanks to Aaron for the website, but it looks like my answer is no. I don't have to build another plugin framework on top of Spring, http://deepeshdarshan.wordpress.com/2013/03/20/learn-spring-by-example-constructor-injection/.

It does seems the xml file would be unbelievable difficult to maintain, so Aaron's link looks promising.

http://deepeshdarshan.wordpress.com/2013/03/24/learn-spring-by-example-autodiscovery/

The dependencies have to be kept up with somewhere.

Était-ce utile?

La solution

Not necessarily. Spring can mimic a plugin-system if you enable auto-discovery of beans in certain packages or by creating a app-context which contains all the plugins that you want to be visible. The former is more flexible (just add JARs to the classpath to add plugins) while the latter is slightly more secure (the "plugin" system will only see beans that you are aware of).

The next step is to define a common interface, say IPlugin to find and configure each plugin.

Now, you can get a list in a plugin manager using

 @Autowired
 private List<IPlugin> plugins;

This line of code will locate all known/visible beans which implement IPlugin, collect them in a list and inject them into the field plugins.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top