migrate configuration from dispatcher servlet xml

2019-08-10 17:30发布

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();
    }
}

1条回答
小情绪 Triste *
2楼-- · 2019-08-10 18:14

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 have WebConfig just like your MvcConfig class, like this:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.web")
public class WebConfig 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();
    }
}

Also, you could have a RepositoryConfig that contains your data access related configurations, like this:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackges = "com.example.repository")
public class RepositoryConfig() {
    // put your datasource, entity manager, jdbc templates, tx managers here
}

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:

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { RepositoryConfig.class, SecurityConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

}

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 of WebApplicationInitializer named AbstractDispatcherServletInitializer makes it even easier to register the DispatcherServlet by simply overriding methods to specify the servlet mapping and the location of the DispatcherServlet configuration. For more details please consult the spring documentation.

查看更多
登录 后发表回答