Spring is a comprehensive programming and configuration model or framework for building robust Java applications. It provides a comprehensive programming and configuration model that encompasses almost every aspect of an enterprise application. Spring simplifies Java development by providing a modular and layered architecture, enabling developers to build scalable, maintainable, and robust applications.
On the other hand, Spring Boot is an extension of the Spring framework, designed to simplify the initial setup and development of new Spring applications. It is an opinionated approach that bundles commonly used third-party libraries and handles most of the boilerplate configurations automatically. Spring Boot follows the "opinionated defaults configuration" approach, which means it preconfigures many of the common settings based on conventional wisdom and best practices.
Key Differences between Spring and Spring Boot
Purpose: Spring is a comprehensive framework for building Java applications, while Spring Boot is a tool that simplifies the initial setup and development of Spring-based applications.
Configuration: In Spring, you need to configure various components manually, either through XML or Java-based configuration. Spring Boot, on the other hand, comes with sensible defaults and auto-configuration for most common use cases, reducing the need for manual configuration.
Dependency Management: Spring requires you to specify and manage dependencies manually. Spring Boot comes with an embedded dependency management system, making it easier to include and manage external libraries.
Embedded Servers: Spring Boot includes embedded servers like Tomcat, Jetty, or Undertow, allowing you to run your application as a standalone application without the need for a separate web server.
Production-Ready Features: Spring Boot comes with production-ready features out of the-box, such as metrics, health checks, and externalized configuration, making it easier to run and monitor your application in a production environment.
Development Tooling: Spring Boot provides additional development tools and plugins for popular IDEs like IntelliJ IDEA and Eclipse, simplifying the development process.
When to Use Spring Boot
Spring Boot is an excellent choice when you are starting a new Spring-based project, especially for smaller applications or microservices. It simplifies the initial setup, reduces boilerplate code, and provides a streamlined development experience with its opinionated defaults and embedded servers.
When to Use Spring Framework
If you are working on an existing Spring application or have specific requirements that cannot be met by Spring Boot's opinionated defaults, you should consider using the core Spring Framework. Spring provides more flexibility and control over the configuration and allows you to customize every aspect of your application.
Illustrating the Difference with Code Examples
Let me illustrate the difference between the Spring Framework and Spring Boot with code examples.
Using the Spring Framework
Assuming you're using Maven for configuration, here's what you have to do to create a Spring Web application running in the Jetty embedded web server with MySQL. You have to specify everything you need. You have to figure out how to deploy this (a bunch of JARs or an uber-JAR?). You're on your own for configuration. But! You'll build probably the tiniest Spring program possible, and start-up will be lightning-fast! The Spring Framework has few opinions about how you should use it. On the downside, this means you have to do more configuration. On the other hand, the Spring Framework will pretty much let you do whatever you want, including very stupid things!
This is how I've used the Spring Framework for most of my career.
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.14.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>10.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
And you'll have to set up Jetty yourself:
public class Application {
public static void main(String[] args) throws Exception {
Server server = new Server();
ServerConnector connector = new ServerConnector(server);
connector.setPort(8080);
server.addConnector(connector);
ServletContextHandler contextHandler = new ServletContextHandler();
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx);
ServletHolder servletHolder = new ServletHolder("mvc-dispatcher", dispatcherServlet);
contextHandler.addServlet(servletHolder, "/*");
server.setHandler(contextHandler);
server.start();
}
}
Using Spring Boot
With Spring Boot, Spring has everything figured out. You don't need to manually list everything you need. All you have to do is add the "starters" for the things you want, and Spring will figure out the rest. They've figured out deployment for you (uber-JAR). They've got your app config ready to go (application.yml). But there's a price. Spring Boot is a lot heavier than the Spring Framework by itself. You're probably pulling in more JARs than you actually need. Spring Boot has strong opinions. Don't argue with Spring Boot, or you will have a bad time.
I mostly go with Spring Boot these days. It's a lot faster to get a project up and running. Even though the result is more "bloated" than putting it all together by hand.
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.4</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
And super easy startup:
@SpringBootApplication
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Conclusion
In summary, Spring is the comprehensive framework for building Java applications, while Spring Boot is a tool that simplifies the initial setup and development of Spring-based applications by providing opinionated defaults and auto-configuration. Spring Boot is built on top of the Spring Framework and is designed to work seamlessly with it.
For new projects, especially smaller applications or microservices, Spring Boot can be an excellent choice as it reduces boilerplate code and provides a streamlined development experience. For existing applications or projects with specific requirements that cannot be met by Spring Boot's opinionated defaults, the core Spring Framework may be a better choice, offering more flexibility and customization options.
Top comments (2)
Is the booting, compiling speed, or jar sizes too much of a difference? Will it kill my github minutes?
Also, is it worth creating a project using spring just to understand and learn about what happens behind the scenes?
Spring Boot has reasonable booting, compiling speeds, and JAR sizes, especially for smaller projects. Using Spring Boot itself won't significantly impact your GitHub minutes, which are mainly consumed by your GitHub Actions workflows.
Creating a Spring project is an excellent way to understand the underlying concepts and principles of the framework, such as dependency injection, aspect-oriented programming, and auto-configuration. Learning Spring can be valuable for building enterprise-level applications, despite some initial overhead.