What fundamental difference is there in initializing the Spring configuration classes in getRootConfigClasses and getServletConfigClasses in the WebInitializer class ?
I have tried changing the initialization of my configuration classes in both methods without different results, the theory says that they are different contexts but both ways work the same.
I would like to know what the correct way is or if it represents any impact on my project to have it in one way or another.
@Configuration
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{SecurityConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class, SwaggerConfig.class};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] {new DelegatingFilterProxy("springSecurityFilterChain")};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
It works the same way, for example:
@Configuration
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{WebConfig.class, SecurityConfig.class,SwaggerConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[] {new DelegatingFilterProxy("springSecurityFilterChain")};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
The method
getServletConfigClasses()
returns the class used to configure the web layer, for example: Controllers, Views, etc., while itgetRootConfigClasses()
returns the class used to configure the other layers of the application, for example: Services, Data Access, etc.It is possible to place all the configuration in the class returned by
getRootConfigClasses()
.