What exactly is the difference between Spring and Spring Boot? Before we get into the details, it is worth noting that Spring has a huge ecosystem of its own. It comprises of Spring framework at its core and many other projects like Spring Boot built on top of the core framework.
In this article, we will do a quick Spring vs Spring Boot comparison where Spring is the core java framework, and “Spring Boot” is an extension project built using the Spring core. Apart from the inherited modules, you can also mix third-party libraries and frameworks in your spring project.
Towards the end of the article, we will also talk briefly about a few other projects in the Spring ecosystem, like Spring Cloud, Spring Data, Spring Integration, and Spring Batch.
What is the Spring Core?
The spring core framework is designed to provide all-around infrastructural backing for developing Java Enterprise applications. Dependency injection (DI) or inversion of control (AOC) is one of the most powerful features of Spring, along with a robust configurational model it provides for building Java EE apps.
Spring also comes packed with many other core modules like Spring JDBC, Spring ORM, Spring MVC Test. The purpose of adding ready to use modules in the spring framework is to simplify development and save time.
All of this allows you to make loosely coupled scalable applications that can be easily unit tested and scaled as the feature requirements grow.
Look at the below image, features of the spring framework are organized in 20 modules. You can use one or more of these modules to build any kind of scalable application in Java.
While spring has added many features over the years, starting a new project with spring framework requires a lot of manual configuration that takes time. For example, you would need to setup Entity Manager, Transaction management, web server, logger, hibernate data source and session factory etc.
To cut down the configuration efforts and to speed up development, the Pivotal team created Spring Boot which can be used to create stand-alone applications and microservices with a very minimal configuration. It also brings in many ready to use starter packs to give your application a head start.
When I say standalone application, it simply means that the generated application includes everything you need including the servlet container like Tomcat, you can just run your application with almost everything auto-configured in the generated app.
It is worth noting that, use of starter packs and auto-configuration makes it much more opinionated and takes away the flexibility that you get in the core Spring framework. But that is by design and favorable in this case!
You might also want to check out an in-depth analysis of the best java frameworks done by Cleopatra.
Let us look at few more details at granular level –
Dependency Management: Spring vs Spring Boot
We will take couple of examples to understand this –
In a spring core app, you will add at the minimum below modules for any web project.
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>tomcat-embed-core</artifactId> <version>1.4.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-bean</artifactId> <version>2.5.9.RELEASE</version> </dependency> </dependencies>
On the other hand, in a Spring Boot application, you would just add the spring boot starter packs fit for your application. In this case, we are talking about a web application, so include the spring-boot-starter-web pack.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.3.4.RELEASE</version> </dependency> </dependencies>
As your project requirements grow, you will need to keep on adding such dependencies in the spring framework, whereas, in spring boot, you would get one or the other starter pack to do the job. Look at another example below –
You can add thyme leaf dependency using the starter pack in spring boot, as below –
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
It doesn’t just end here, there is an online tool, spring initializer – https://start.spring.io/. It is a graphical user interface that can be used to generate the basic construct of your project.
Look at the image below, you can select the options on the interface and your project configuration will be generated for you. On the other hand, all of this needs to be done manually in a typical spring core application.
While we looked at auto dependency management in Spring Boot, auto-configuration is another great time saver. Some of the configurations that are auto handled include the likes of below-
- Dispatcher Servlet configuration
- Data Source configuration
- Error Page configuration
- Jars for static dependencies
- Tomcat as default embedded servlet
If you are not using Spring Boot, but plain Spring, you would be struggling to do configuration like below, manually –
<web-app...> <!-------- DispatcherServlet configuration here-----> .... <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/myWeb-servlet.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> </web-app>
Another example below, where you would end up doing a lot of manual configuration in plain spring –
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="persistenceUnitName" value="hsql_pu" / <property name="dataSource" ref="dataSource" /> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>/>
On the other hand, Spring Boot manages the internal dependencies and auto-configures most of the above. If the classpath includes spring jar, you will get dispatcher servlet auto-configured. If you include Hibernate in the classpath, you will get the data source auto-configured.
Spring Vs Spring Boot: Quick Comparison
|Spring Core||Spring Boot|
|Open source, free to use the framework. Makes the core of the spring ecosystem||Extension project built on top of spring core, the one on the left.|
|Provides a flexible configurable model for JEE applications with dependency injection at its best||Brings opinionated starter packs for speed development of stand-alone java apps|
|Requires manual dependency management and significant configuration in large projects.||Dependency management is auto handled in spring boot. Visual initializer further speeds up everything.|
|Requires manual configuration for various modules like Hibernate, Entity manager, Dispatcher servlet, and everything else||Provides ready to use starter packs that are pre-configured for the most part and needs minimal manual configuration|
|One would typically use spring core to build libraries, frameworks, and platforms that can be utilized for building Java EE applications by the end customer||One would typically use spring boot to build applications to be used directly by the end customer.|
|Spring ecosystem includes many other projects like Spring cloud, Spring Batch, Spring Integration.||One would typically combine Spring Boot with other projects like Spring cloud to deliver enterprise-grade production-ready applications.|
Spring Boot: Starter Packs (Few Examples)
While there is a long list of spring boot starter packs, let us look at a few of them –
|spring-boot-starter-thymeleaf||To include thymeleaf template engine in your spring boot application|
|spring-boot-starter-artemis||For Apache Artemis JMS messaging|
|spring-boot-starter-web-services||Starter pack to build web services with spring boot.|
|spring-boot-starter-mail||Brings java mail and email sending functionality to your JEE app|
|spring-boot-starter-web||To get a complete working web app configured with every dependency. Also, includes Tomcat by default.|
|spring-boot-starter-test||To use JUnit, Hamcrest, and Mockito along with other testing libraries to test your spring based java application|
|spring-boot-starter-aop||Include AOP – To do aspect-oriented programming|
|spring-boot-starter-data-cassandra||To work with spring Cassandra databases functionality|
|spring-boot-starter-data-jpa||To include spring data JPA with hibernate in your app|
|spring-boot-starter-groovy-templates||Bring the power of Groovy templates to your MVC app|
Apart from offering features like IOC, Spring is an imperative framework that serves as a foundation for the Spring Boot framework.
Spring Boot clearly brings in developer happiness and for sure has taken the spring ecosystem to the heights it currently enjoys.
From a job openings perspective as well, you would find spring boot as one of the key requirements these days, wherever you see java-based microservices or web applications related projects.
Do let us know your views around this spring vs spring boot comparison via comments. Also, would be great if you can share your experience working with these frameworks, with our readers!