Spring: spring-data-mongodb or spring-boot-starter-data-mongodb - spring

Which's the difference between
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
and,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
I'm developing an spring boot service.

spring-boot-starter-data-mongodb contains configuration classes for Spring Boot. It also includes the spring-data-mongodb library so you would only need to include the start in your boot app:
https://search.maven.org/artifact/org.springframework.boot/spring-boot-starter-data-mongodb/2.0.5.RELEASE/jar

spring-boot-starter-data-mongodb is a spring boot starter pom. For more information on starters:
spring-boot-starters
Dependency management is a critical aspects of any complex project. And doing this manually is less than ideal; the more time you spent on it the less time you have on the other important aspects of the project.
Spring Boot starters were built to address exactly this problem. Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors.

Related

Using spring-boot-starter dependency on a non boot project, Is a good practice?

lets say that I need to develop a not boot app to develop an application that uses redis cache to store and retrive data.
I can use the spring-boot-starter dependency instead adding multiple dependencies, and it is working fine.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
Now my question..
Is it a good practice to add spring-boot-starter dependency on a non boot project?
Sure you can, just keep in mind that it will bring some transitive dependencies into your project, like org.springframework.spring-{context,beans,core,tx,...}.

What are the consequences of not using Spring Boot as the parent pom?

I have a multi module Maven project that share a common parent pom. Some of these modules will use Spring Boot but some will not. I don't want to use Spring Boot as the parent of MY parent pom and have unnecessary dependencies in modules that don't use them.
What are the consequences of not using the Spring Boot parent pom? Will everything still work with all the auto configured goodness? I am considering the following solution of putting this in my parent pom as an alternative
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
But I am pretty new to all this Maven stuff and Spring Boot so I don't want any unintended consequences or to declaw the capabilities of Spring Boot in any way. Can someone explain what (if anything) I will lose out on if I do this? Thanks!
The Reference Guide explains how to use Spring Boot without the spring-boot-starter-parent and what will happen. This answer also explains the difference between spring-boot-starter-parent and spring-boot-dependencies.
Summary: With both ways you get the powerful dependency management of all Spring Boot components and third party libraries. The spring-boot-starter-parent also configures some Maven plugins like spring-boot-maven-plugin for running mvn spring-boot:run.

Which one should I use among spring boot, spring BOM and spring IO?

I tried to read documents regarding Spring BOM, Spring Boot and Spring IO. But there is no clarification on, how we should use them together or not?
In my project, we already have our own Parent POM, So I can’t use as parent them but all they have alternative way to use, Like below by defining dependency management
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>${org.springframework-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Spring BOM, Spring Boot and Spring IO resolve version required for you
So what is exactly difference between them? Which one should I prefer? And in Which condition?
The Spring Framework bom provides dependency management for all of Spring Framework's modules. The Spring Boot bom is a superset of this. In addition to providing dependency management for Spring Framework, it also provides dependency management for Spring Boot's modules, a number of other Spring projects, and various third-party dependencies which Spring Boot supports. The Spring IO Platform bom is, in turn, a superset of the Spring Boot bom. The main change is that it adds dependency management for all of the Spring projects' dependencies.
If you're not using Spring Boot, or if you want to use your own dependency management, then you may want to use the Spring Framework bom . If you're using Spring Boot, or you want some help with dependency management, then you should choose between using Spring Boot's bom or the Spring IO Platform bom. The main choice here is how close to the leading edge you want to be. If you favour being up-to-date then use the Spring Boot bom. If your project is more conservative and backwards compatibility is of paramount importance, then you should consider using the Spring IO platform bom.

Spring framework compatibility between various projects

I am learning Spring framework and while trying "various" sub-projects within this, I got this doubt.
Spring framework has "core spring" at the heart of it. Now, as the project grows, e.g. trying other features like: spring-mvc, spring-web flow , spring security etc. Are all those sub-projects part of same release. For example, if I look for spring 4.0.2 release, would all these sub-projects be included in this? (hence release for various sub-project with same number: 4.0.2).
If this is not correct, then how do we ensure to chose the compatible sub-projects?
Thanks
spring-mvc is part of the spring framework, the others are separate projects following their own versioning. In general there is a minimum version for the projects and most work fine with newer versions.
If you want to be sure use the Spring IO Platform to manage your dependencies.
In your pom add
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>1.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
Then you can simply add the dependencies (without version) to your dependencies section
<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
</dependencies>
For a list of managed dependencies (and version) check Appendix A of the reference guide.
Spring framework has "core spring" at the heart of it. Now, as the
project grows, e.g. trying other features like: spring-mvc, spring-web
flow , spring security etc. Are all those sub-projects part of same
release
spring-mvc and spring-web are both individual artifacts that you'll find within a single Spring release. They are versioned together, and you should always use the same version for all of them in any given project.
spring-security, however, is a completely different beast. It sits on top of Spring, but it's versioned completely separately. You need to make sure that the version of Spring Security you use is combined with a compatible version of Spring.

spring-boot-starter versus spring-boot-starter-xxx

I noticed that the Spring Boot Sample Data Redis declares the following dependencies:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
</dependency>
See here for full POM: https://github.com/spring-projects/spring-boot/blob/v1.0.0.RC4/spring-boot-samples/spring-boot-sample-data-redis/pom.xml
I see no mention of the <artifactId>spring-boot-starter-redis</artifactId>
My question is: when do I use spring-boot-starter versus spring-boot-starter-xxx where xxx is the name of the project (here Redis)?
The answer to the specific question: spring-boot-starter is a baseline for the others, and for standalone (non-web) apps that don't use any other Spring components - it has basic support for Spring, Logging, and Testing, but nothing else (no webapp features, no database etc.). Since all the other starters depend on it, once you use another one you can remove the vanilla starter. EDIT: see here https://github.com/spring-projects/spring-boot/commit/77fd127e09963a844f8fb4e574e1f0d9d3424d4e.
Up to you on the redis starter, but I would use the starter if it exists, since it will typically cut down on the number of dependencies you need to declare. The redis one actually doesn't add a lot of value (hence it didn't exist until recently), but it probably ought to be used in the sample.

Resources