Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Spring Boot Application works internally? [closed]

I am working on Spring Boot. I have some doubt

  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 ?
like image 764
Sangram Badi Avatar asked May 25 '17 04:05

Sangram Badi


People also ask

How does spring boot application works internally?

Spring Boot automatically configures your application based on the dependencies you have added to the project by using @EnableAutoConfiguration annotation. For example, if MySQL database is on your classpath, but you have not configured any database connection, then Spring Boot auto-configures an in-memory database.

How do spring boot annotations work internally?

Spring Boot Annotations is a form of metadata that provides data about a program that is not a part of the program itself. They do not have any direct effect on the operation of the code they annotate. Spring Boot Annotations do not use XML and instead use the convention over configuration principle.

Can we run spring boot application without @SpringBootApplication?

Uses. It's not mandatory to put @SpringBootApplication to create a Spring Boot application, you can still use @Configuration and @EnableAutoConfiguration individually as shown in the example given in the next point.


2 Answers

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

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.

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

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

@Configuration @EnableWebMvc public class WebConfig extends WebMvcConfigurerAdapter {      @Override     public void addCorsMappings(CorsRegistry registry) {         registry.addMapping("/api/**");     } } 
like image 187
Vijender Kumar Avatar answered Sep 17 '22 06:09

Vijender Kumar


  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 ?

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

  1. Can we run spring boot application other than the tomcat server, if yes how ?

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

  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 ?

You just have to add a FilterRegistrationBean in your class with main method or any other class with @Configuration to register a custom Filter.

    @Bean     public FilterRegistrationBean crossFilter() {         FilterRegistrationBean registration = new FilterRegistrationBean();         registration.setFilter(new CrossFilter());         registration.addUrlPatterns("/*");         return registration;     } 
like image 43
shazin Avatar answered Sep 17 '22 06:09

shazin