Java Spring Boot 应用程序如何在内部工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44172261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:23:29  来源:igfitidea点击:

How Spring Boot Application works internally?

javaspringspring-boot

提问by Sangram Badi

I am working on Spring Boot. I have some doubt

我正在研究 Spring Boot。我有些怀疑

  1. As i know spring boot has a main() and it calls static run() which is present in SpringApplication. But i want to know what is the flow of Spring boot application ?
  2. Can we run spring boot application on server other than tomcat, if yes how ?
  3. How to add CROSS Filter in Spring boot application ? As we know in Spring MVC application we configure CROSS Filter in web.xml, but Spring boot we don't have web.xml, So how to configure this ?
  1. 据我所知,spring boot 有一个 main(),它调用 SpringApplication 中存在的静态 run()。但我想知道 Spring boot 应用程序的流程是什么?
  2. 我们可以在 tomcat 以外的服务器上运行 spring boot 应用程序吗,如果是,如何?
  3. 如何在 Spring boot 应用程序中添加 CROSS 过滤器?我们知道在Spring MVC应用程序中我们在web.xml中配置了CROSS Filter,但是Spring boot我们没有web.xml,那么如何配置呢?

采纳答案by Vijender Kumar

Following is the high-level flow of how spring boot works.

以下是 Spring Boot 工作原理的高级流程。

From the run method, the main application context is kicked off which in turn searches for the classes annotated with @Configuration, initializes all the declared beans in those configuration classes, and based upon the scope of those beans, stores those beans in JVM, specifically in a space inside JVM which is known as IOC container. After the creation of all the beans, automatically configures the dispatcher servlet and registers the default handler mappings, messageConverts, and all other basic things.

从 run 方法开始,主应用程序上下文被启动,它依次搜索用 注释的类@Configuration,初始化那些配置类中所有声明的 bean,并基于这些 bean 的范围,将这些 bean 存储在 JVM 中,特别是在JVM 内部的空间,称为 IOC 容器。创建所有 bean 后,自动配置调度程序 servlet 并注册默认处理程序映射、messageConverts 和所有其他基本内容。

Basically, spring boot supports three embedded servers:- Tomcat (default), Jetty and Undertow.

基本上,spring boot 支持三个嵌入式服务器:- Tomcat(默认)、Jetty 和 Undertow。

You can add cross filters in spring boot in one of the configuration files as

您可以在配置文件之一的 spring boot 中添加交叉过滤器

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**");
    }
}

回答by shazin

  1. As i know spring boot has a main() and it calls static run() which is present in SpringApplication. But i want to know what is the flow of Spring boot application ?
  1. 据我所知,spring boot 有一个 main(),它调用 SpringApplication 中存在的静态 run()。但我想知道 Spring boot 应用程序的流程是什么?

Spring boot works with a lot of generic AutoConfiguration, example DataSourceAutoConfigurationfor DataSourceetc. So that you don't have to do much of the configurations and focus just on business logic. Read thisfor more

Spring boot 可以使用很多通用的AutoConfiguration,例如DataSourceAutoConfigurationforDataSource等。这样你就不必做太多的配置,只关注业务逻辑。阅读本文了解更多

  1. Can we run spring boot application other than the tomcat server, if yes how ?
  1. 我们可以运行除 tomcat 服务器之外的 spring boot 应用程序,如果是,如何?

Yes, you can either start a Spring boot application as a Console application or with other web servers like Jetty. Read thisfor more

是的,您可以将 Spring boot 应用程序作为控制台应用程序启动,也可以与其他 Web 服务器(如 Jetty)一起启动。阅读本文了解更多

  1. How to add CROSS Filter in Spring boot application ? As we know in Spring MVC application we configure CROSS Filter in web.xml, but Spring boot we don't have web.xml, So how to configure this ?
  1. 如何在 Spring boot 应用程序中添加 CROSS 过滤器?我们知道在Spring MVC应用程序中我们在web.xml中配置了CROSS Filter,但是Spring boot我们没有web.xml,那么如何配置呢?

You just have to add a FilterRegistrationBeanin your class with main method or any other class with @Configurationto register a custom Filter.

你只需要FilterRegistrationBean在你的类中添加一个main 方法或任何其他类@Configuration来注册一个自定义的Filter.

    @Bean
    public FilterRegistrationBean crossFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new CrossFilter());
        registration.addUrlPatterns("/*");
        return registration;
    }