0

I am having a hard time determining whether I need to use org.springframework.boot dependencies, or use org.springframework dependencies and non-spring dependencies instead, in a non-spring boot application module such as a shared library.

At the moment I have 3 projects:

parent/
|-- client
|   |-- pom.xml
|   |-- src
|-- domain
|   |-- pom.xml
|   |-- src
|-- server
|   |-- pom.xml
|   |-- src
|-- pom.xml

My server is a Spring Boot Application that I use to serve web pages, and to use as a RESTless API (by using two different HttpSecurity configs).

My client is a Spring Boot CommandLineRunner Application that consumes the server's API service.

I have created the Domain module so that both the client and the server have access to the same versions of JPA Entities.


What I cannot figure out is when to use the org.springframework.boot dependency, and when to use the org.springframework and non-spring dependencies.

For example, in my domain module. I need some dependencies that I normally get through "org.springframework.boot::spring-boot-starter-jpa" and "org.springframework.boot::spring-boot-starter-security". But since the domain module is not a spring boot application. Should I use the non-spring-boot: "org.spring.framework.security::spring-security-core" dependency and the non-spring dependency "javax.persistence::persistence-api" instead?

What's the best practice? When to use springframework.boot dependencies and when to use springframework and non-spring dependencies? While staying as close to Spring as possible.

BARJ
  • 1,543
  • 3
  • 20
  • 28
  • The starters are nothing more then poms with a collection of jars needed to use that functionality (they don't really have anything to do with Spring Boot). They make it easier to get your dependencies. You can always use single dependencies, but you will need to find matching libraries (and versions) yourself. Using the starters you don't need to. – M. Deinum Jan 17 '17 at 06:49
  • @M.Deinum An example, with the spring-boot-starter-web, a tomcat webserver starts. I am afraid by using other starter poms from spring-boot I get similar undesired functionality and auto-configuration. – BARJ Jan 17 '17 at 06:58
  • Nothing starts If you don't use a class with a main containing `@SpringBootAPplication` or the `SpringBootServletInitializer`... So no it will not start it will only pull in dependencies. – M. Deinum Jan 17 '17 at 07:44
  • Sorry, forgot to mention that this very example of the spring-boot-starter-web was for the client module, which uses some classes from the spring web library such as the RestTemplate. It's a command line app that uses the web library but doesn't want to start the server. This can be achieved by either using spring-web, or using spring-boot-starter-web and excluding the tomcat server. But that's the point of my question. Which is the better approach considering version collision and side effects? – BARJ Jan 17 '17 at 07:46

1 Answers1

0

Use springframework.boot dependencies if you need them, don't use them if you don't need them. Simple as that. A shared library usually shouldn't need them, but it could.

Note though like others have said that your maven pom parent doesn't really tell which dependencies you actually use, so paragraph above doesn't say anything about that. Using the same parent [as with the apps that use your module] is recommended as it keeps the dependency versions in sync, be it boot or non-boot. You're saying that "with the spring-boot-starter-web, a tomcat webserver starts", but that's only true if you have an boot application class and use spring-boot plugin, which for a shared library you wouldn't have or do.

To summarize, use the same parent as apps that are meant to use your library, if you can.

eis
  • 51,991
  • 13
  • 150
  • 199
  • Sorry, the example of the spring-boot-starter-web was about my client module. The client module, which uses spring-boot-starter to run as a Spring Boot command line app. Uses various classes from the spring web library (RestTemplate is one of them). For that I used the spring-web dependency instead of the spring-boot-starter-web dependency to access RestTemplate in the client module. Otherwise it would start the tomcat webserver. I could use spring-boot-starter-web dependency and exclude the tomcat dependency. But that seems like more afford than just to use the spring-web dependency – BARJ Jan 17 '17 at 07:42
  • Can you clarify what you mean by "Use springframework.boot dependencies if you need them"? As far as I know, those Spring boot starters just add a lot of dependencies, so you could also importall of those dependencies by yourself, so in that case, you never really "need" an org.springframework.boot dependency. – g00glen00b Jan 17 '17 at 07:55
  • @g00glen00b there are spring-boot specific stuff as well, located in `org.springframework.boot` package. That's what I'm referring to. I think youre mixing Spring boot starter pom dependencies and Spring boot dependencies in general. – eis Jan 17 '17 at 08:18