Sagar
Sagar

Reputation: 5596

Spring boot starters creation options

I see Spring boot project has organized starters in a certain way, i.e. starters modules only have pom dependencies in them and autoconfiguration is a separate project where all pom dependencies of all starters have been added as optional dependencies.

It's a different approach from some other big projects like Apache camel. Their starters have pom dependencies as well as autoconfiguration code as well. Also, these starters have dependencies on other internal starters.

My question is - when we should choose one approach over the other and what is the best way to create spring boot starters? What are the downsides/advantages of one over the other? Also, why did the spring boot folks choose to organize starters in the way they did as having pom dependencies and autoconfiguration together seems to be an intuitive way to go?

Upvotes: 2

Views: 147

Answers (1)

Stephane Nicoll
Stephane Nicoll

Reputation: 33151

That's a very good question and there are different facets to consider.

Let's start with Spring Boot. We didn't want starters to be mandatory in any way. If you want to craft your own starter for a "web application" or if you want to list the dependencies yourself ("webmvc" + your favourite container), so be it. That's the main reason why the code is not in the starter, but not only. Another reason is that the auto-configuration code is available and can adapt to libraries you add. If that was split you'd need a combination of adding the library + the "right" auto-configuration module. It is very easy to forget the latter.

Now for a third-party. I can't speak for Camel but separating the auto-configuration code from the starter boils down to whether the code you auto-configure can adapt to optional dependencies. If your use case is about auto-configuring library "acme" and it doesn't have any optional feature, then it would be more natural to just have one module and be done with it. If you start putting optional dependencies in your starter, you may want to reconsider.

If "acme" has several flavours, options or optional features, then it would be nicer to separate the auto-configuration. This lets other craft their own starter with the auto-configuration module and their options. On your side, you can craft "one starter" that provides the options you want to promote. A bit like what we do with spring-boot-starter-web that brings Tomcat as the default container.

Upvotes: 3

Related Questions