Spring vs Spring Boot: Core Differences, Configuration, and Deployment Guide
This article compares Spring and Spring Boot, explaining their fundamental concepts, Maven dependencies, MVC and security configurations, template engine setup, startup mechanisms, and packaging options, while providing practical code examples to help Java developers choose and migrate between the two frameworks.
Overview
Spring is a comprehensive Java framework that provides infrastructure support such as dependency injection and a collection of modules (SpringJDBC, SpringMVC, SpringSecurity, etc.), while Spring Boot is an extension of Spring that eliminates XML configuration and streamlines development through auto‑configuration and starter dependencies.
What is Spring
Spring offers a full‑stack solution for Java applications, including modules like SpringJDBC, SpringMVC, SpringSecurity, and others, which reduce boilerplate code and accelerate development. For example, JDBCTemplate simplifies database operations to a few lines.
What is Spring Boot
Spring Boot builds on Spring by removing the need for extensive XML configuration, providing embedded servlet containers (Tomcat, Jetty, Undertow), starter dependencies, automatic configuration, production‑ready metrics, and eliminating XML files.
Configuration Analysis
Maven Dependencies
Typical Spring web dependencies require multiple entries:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.0.RELEASE</version>
</dependency>Spring Boot reduces this to a single starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>Testing libraries are also consolidated into spring-boot-starter-test.
MVC Configuration
In classic Spring, a WebApplicationInitializer or web.xml must define the DispatcherServlet and related listeners:
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.pingfangushi");
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}Spring Boot requires only the @EnableWebMvc annotation on a configuration class and a simple view resolver bean.
Template Engine (Thymeleaf)
Spring needs explicit thymeleaf-spring5 dependencies and a configuration class that defines a SpringResourceTemplateResolver, SpringTemplateEngine, and registers a ThymeleafViewResolver. Spring Boot activates Thymeleaf with the single spring-boot-starter-thymeleaf starter.
Spring Security Configuration
Traditional Spring security setup involves adding spring-security-web and spring-security-config modules, then extending WebSecurityConfigurerAdapter:
@Configuration
@EnableWebSecurity
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password(passwordEncoder().encode("password"))
.authorities("ROLE_ADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}Spring Boot simplifies this to adding spring-boot-starter-security, which pulls in all required dependencies automatically.
Application Startup
Spring Boot’s entry point is a class annotated with @SpringBootApplication containing a main method that calls SpringApplication.run. It uses an embedded servlet container and automatically scans components in the same package and sub‑packages.
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}For traditional Spring, the container reads web.xml (or a ServletContainerInitializer) to create a DispatcherServlet and load the application context.
Packaging and Deployment
Both frameworks support Maven and Gradle. Spring Boot’s Maven plugin can produce executable jar or war files that run with java -jar or be deployed to an external servlet container by extending SpringBootServletInitializer:
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
servletContext.addListener(new HttpSessionEventPublisher());
}
}Spring Boot’s advantages over classic Spring include embedded container support, easy standalone execution, optional dependency exclusion for external containers, flexible configuration profiles, and random port allocation for integration tests.
Conclusion
In short, Spring Boot is an opinionated extension of Spring that simplifies development, testing, and deployment by providing auto‑configuration, starter dependencies, and embedded servers, while retaining full compatibility with the underlying Spring ecosystem.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
