Yosua Lijanto Binar
Yosua Lijanto Binar

Reputation: 670

Where is SpringBootServletInitializer's DispatcherServlet?

How SpringBootServletInitializer determines RootConfig.class, WebConfig.class, and maps DispatcherSevlet?

Upvotes: 1

Views: 857

Answers (1)

Evgeni Dimitrov
Evgeni Dimitrov

Reputation: 22506

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

return application.sources(Application.class); - loads the Application.class. That's your main configuration, where you can declare @Beans. You can add more @Configuration classes by putting them in the same folder, for example, and they will be "component-scanned".

If you declare a @Configuration class that extends WebMvcConfigurerAdapter, you have an access to the web configuration like resource handlers, argument resolvers, etc.

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**")
                .addResourceLocations("/public-resources/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS).cachePublic());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver>   argumentResolvers) {
         argumentResolvers.add(new FooBarHandlerMethodArgumentResolver());
    }
}

By default the dispatcher servlet is configured to the root path "/" If you need more details, see the auto configuration.

Upvotes: 3

Related Questions