How Spring Boot Application works internally?
- 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
- 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
- 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;
}
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/**");
}
}