Spring vs Spring Boot: Core Differences, Configuration Tips, and Deployment Guide
This article compares Spring and Spring Boot, explains their core concepts, walks through MVC, template engine, and security configurations, shows Maven and Gradle dependency setups, and details how to package and deploy applications with code examples.
Overview
The article explains the differences between Spring and SpringBoot, helping developers who are new to SpringBoot understand how it extends Spring, reduces XML configuration, and simplifies building, testing, and deploying Java web applications.
What is Spring
Spring is a comprehensive Java framework that provides core infrastructure support such as dependency injection and ready‑to‑use modules like SpringJDBC, SpringMVC, SpringSecurity, SpringAOP, SpringORM, and SpringTest. These modules reduce boiler‑plate code, for example by using JdbcTemplate to simplify database operations.
What is Spring Boot
SpringBoot is an extension of the Spring framework that eliminates the need for extensive XML configuration. It creates standalone applications, embeds servlet containers (Tomcat, Jetty, Undertow) without requiring a WAR file, offers starter dependencies to simplify build configuration, performs auto‑configuration, provides production‑ready metrics, and avoids any XML configuration requirements.
Key Features of Spring Boot
Create standalone Spring applications.
Embed Tomcat, Jetty, or Undertow containers (no WAR deployment needed).
Provide starters that simplify Maven/Gradle dependencies.
Auto‑configure as much as possible.
Expose production metrics and health checks.
No XML configuration is required.
Maven Dependencies
<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>SpringBoot requires only a single starter dependency to run a web application:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.6.RELEASE</version>
</dependency>MVC Configuration
In Spring you must define a Servlet and mapping, typically via web.xml or an Initializer class. Example of a programmatic initializer:
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("/");
}
}SpringBoot simplifies this: adding @EnableWebMvc and a configuration class with a view resolver is enough.
@Configuration
@EnableWebMvc
public class ClientWebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver bean = new InternalResourceViewResolver();
bean.setViewClass(JstlView.class);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
}Template Engine Configuration
Configuring Thymeleaf in Spring:
@Configuration
@EnableWebMvc
public class MvcWebConfig implements WebMvcConfigurer {
@Autowired
private ApplicationContext applicationContext;
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".html");
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
engine.setEnableSpringELCompiler(true);
return engine;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
}In SpringBoot the same can be achieved by adding spring-boot-starter-thymeleaf and placing templates under src/main/resources/templates.
Spring Security Configuration
A simple in‑memory authentication using HTTP Basic:
@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();
}
}SpringBoot uses the same configuration; adding spring-boot-starter-security pulls in all required dependencies automatically.
Application Bootstrap
SpringBoot’s entry point is a class annotated with @SpringBootApplication:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}When packaged as a WAR for external servlet containers, extend 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());
}
}Packaging and Deployment
Both Spring and SpringBoot can be built with Maven or Gradle. SpringBoot’s Maven plugin can produce executable JARs or WARs, allowing java -jar execution or deployment to external containers. Advantages of SpringBoot over classic Spring include embedded container support, simplified command‑line execution, optional dependency exclusion to avoid JAR conflicts, flexible configuration profiles, and random port generation for integration testing.
Conclusion
In short, SpringBoot is essentially an extension of Spring that streamlines development, testing, and deployment by providing auto‑configuration, starter dependencies, and embedded containers, making Java web application creation faster and more convenient.
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.
Java Backend Technology
Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!
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.
