Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why choose the embedded container for the Spring Boot project

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail why you chose the embedded container of the Spring Boot project. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Spring Boot is a new framework provided by the Pivotal team, which is designed to simplify the initial building and development process of new Spring applications. The framework is configured in a specific way so that developers no longer need to define a templated configuration. In this way, Spring Boot is committed to becoming a leader in the booming field of rapid application development (rapid application development).

Spring Boot inherits the advantages of Spring and adds some new features and features:

(1) SpringBoot was born with Spring4.0. Once it was launched, it caused a huge reversal.

(2) literally, Boot means boot, so SpringBoot helps developers build Spring framework quickly.

(3) SpringBoot helps developers start a Web container quickly.

(4) SpringBoot inherits the excellent genes of the original Spring framework.

(5) SpringBoot simplifies the process of using Spring

(6) Spring Boot brings us the efficiency of scripting language development, but Spring Boot does not surprise us with new technologies, which are common to Java EE developers.

The default web container for the Spring Boot project is Tomcat, but developers can modify it as needed, such as using Jetty or Undertow,Spring Boot to provide the corresponding starters.

How Do

Exclude tomcat's starter from the pom file

Org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat

Increase Jetty dependency

Org.springframework.boot spring-boot-starter-jetty

By starting with the mvn spring-boot:run command, you can see that Jetty has been started.

Jetty container startup

PS: if you use gradle, you can refer to the official documentation-Use Jetty instead of Tomcat

Analysis.

The reason for supporting the above switching is the automatic configuration of Spring Boot. I first exclude spring-boot-starter-web dependencies from Tomcat dependencies so that it doesn't conflict with Jetty dependencies. Spring Boot decides which web container to use based on the type of container class scanned under classpath.

Looking at the internal structure of the EmbeddedServletContainerAutoConfiguration class in IDEA, you can see conditional matching annotations such as @ ConditionalOnClass ({Servlet.class, Server.class, Loader.class, WebAppContext.class}). If you can find instances of the above three classes in the Jar package of Jetty, you decide to use the jetty container.

@ Configuration@ConditionalOnClass ({Servlet.class, Server.class, Loader.class, WebAppContext.class}) @ ConditionalOnMissingBean (value = {EmbeddedServletContainerFactory.class}, search = SearchStrategy.CURRENT) public static class EmbeddedJetty {public EmbeddedJetty () {} @ Bean public JettyEmbeddedServletContainerFactory jettyEmbeddedServletContainerFactory () {/ / returns the container factory instance to construct the container instance return new JettyEmbeddedServletContainerFactory ();}}

Similarly, you can see that there is a similar judgment and usage code for Tomcat:

@ Configuration@ConditionalOnClass ({Servlet.class, Tomcat.class}) @ ConditionalOnMissingBean (value = {EmbeddedServletContainerFactory.class}, search = SearchStrategy.CURRENT) public static class EmbeddedTomcat {public EmbeddedTomcat () {} @ Bean public TomcatEmbeddedServletContainerFactory tomcatEmbeddedServletContainerFactory () {return new TomcatEmbeddedServletContainerFactory () }} this is the end of the article on "Why choose the embedded container of the Spring Boot project". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report