Following is the code for my dispatcher-servlet.xml and configuration class :
Where do i put my db configuration and entity definitions?
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
super.addViewControllers(registry);
// Default view
registry.addViewController("/").setViewName("home");
registry.addViewController("/login").setViewName("login");
registry.addViewController("/home").setViewName("home");
}
@Bean
public Filter basicMetricFilter() {
return new PieRequestMetricsFilter();
}
}
You can put those configurations in
MvcConfig
but it's NOT a good idea. A better approach is to define one config for each architectural aspect of your application and then put related configs only in each one. Suppose you have a web application with traditional layered architecture. In this example you would haveWebConfig
just like yourMvcConfig
class, like this:Also, you could have a
RepositoryConfig
that contains your data access related configurations, like this:For wiring these configurations together, there is no need for any xml file such as dispatcher servlet or web.xml. You can define a
WepApplicationInitializer
that defines parent and child configs in this scenario, like this:WebApplicationInitializer
is an interface provided by Spring MVC that ensures your implementation is detected and automatically used to initialize any Servlet 3 container. An abstract base class implementation ofWebApplicationInitializer
namedAbstractDispatcherServletInitializer
makes it even easier to register theDispatcherServlet
by simply overriding methods to specify the servlet mapping and the location of theDispatcherServlet
configuration. For more details please consult the spring documentation.