38

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 ?
Mehravish Temkar
  • 4,275
  • 3
  • 25
  • 44
Sangram Badi
  • 4,054
  • 9
  • 45
  • 78

2 Answers2

50

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/**");
    }
}
Vijender Kumar
  • 1,285
  • 2
  • 17
  • 26
  • Good explanation of spring boot flow. Thanks If I want to run spring boot application using jboss server, then how to configure this? – Sangram Badi May 25 '17 at 05:42
  • You can configure the jboss server like any other server i'e configure it from the server view in your ide, the only thing here is that you can not configure jboss as an Embedded server with spring boot. – Vijender Kumar May 25 '17 at 05:44
  • for run spring boot application in jboss, i need deploy the war file in jboss server, right ? – Sangram Badi May 25 '17 at 06:21
  • 1
    Right. You can either deploy it manually or you can write a task in your build script which will automatically copy the war to your deployment directory. There are several ways, depends upon your need. – Vijender Kumar May 25 '17 at 06:25
  • Any reference? I need it for my thesis – watchme Mar 18 '18 at 12:02
  • @VijenderKumar So by default, tomcat will act as both application and web server? – Sindhu Arju Jun 02 '20 at 14:52
6
  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;
    }
shazin
  • 21,379
  • 3
  • 54
  • 71
  • I want run spring boot application using jboss server, so how can I configure this? – Sangram Badi May 25 '17 at 05:39
  • 1
    You can package the Spring Boot application as a .war file and deploy in a JBOSS server. As long as JBOSS Server has Servlet Specification 3 it will work. – shazin May 25 '17 at 06:20