将配置从调度程序servlet xml迁移到java类。我是否需要(java类和xml)? [英] migrate configuration from dispatcher servlet xml to java class. Do i need both(java class and xml)?

查看:130
本文介绍了将配置从调度程序servlet xml迁移到java类。我是否需要(java类和xml)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的dispatcher-servlet.xml和配置类的代码:



我在哪里放置我的数据库配置和实体定义?

  @Configuration 
public class MvcConfig extends WebMvcConfigurerAdapter {
$ b $ @Override
public void addViewControllers(ViewControllerRegistry registry ){
super.addViewControllers(registry);
//默认视图
registry.addViewController(/)。setViewName(home);
registry.addViewController(/ login)。setViewName(login);
registry.addViewController(/ home)。setViewName(home);


@Bean
public Filter BasicMetricFilter(){
return new PieRequestMetricsFilter();



$ div $解析方案

可以将这些配置放在 MvcConfig 中,但这不是个好主意。更好的方法是为应用程序的每个架构方面定义一个配置文件,然后仅在每个配置文件中放置相关的配置文件。假设您有一个具有传统分层体系结构的Web应用程序。在这个例子中,您可以像 MvcConfig 类一样使用 WebConfig ,如下所示:

  @Configuration 
@EnableWebMvc
@ComponentScan(basePackages =com.example.web)
公共类WebConfig扩展WebMvcConfigurerAdapter {

@Override
public void addViewControllers(ViewControllerRegistry registry){
super.addViewControllers(registry);
//默认视图
registry.addViewController(/)。setViewName(home);
registry.addViewController(/ login)。setViewName(login);
registry.addViewController(/ home)。setViewName(home);


@Bean
public Filter BasicMetricFilter(){
return new PieRequestMetricsFilter();


$ / code $ / pre

另外,你可以有一个 RepositoryConfig ,其中包含您的数据访问相关配置,如下所示:

  @Configuration 
@EnableTransactionManagement
@EnableJpaRepositories(basePackges =com.example.repository)
public class RepositoryConfig(){
//将您的数据源,实体管理器,jdbc模板,tx管理器放在这里



$ b $ p
$ b

为了将这些配置连接在一起,不需要任何xml文件,如调度程序servlet或web.xml。您可以定义一个 WepApplicationInitializer 来定义这种情况下的父配置和子配置,如下所示:

  public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
$ b @Override
protected Class<?> [] getRootConfigClasses(){
return new Class [] {RepositoryConfig.class ,SecurityConfig.class};

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

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


$ b code
$ b

WebApplicationInitializer 是Spring MVC提供的一个接口,它确保您的实现被检测到并自动用于初始化任何Servlet 3容器。一个名为 AbstractDispatcherServletInitializer 的抽象基类实现 WebApplicationInitializer 使得更容易注册 DispatcherServlet 通过重写方法来指定servlet映射和 DispatcherServlet 配置的位置。
欲了解更多详情,请查阅spring文档。


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 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.

这篇关于将配置从调度程序servlet xml迁移到java类。我是否需要(java类和xml)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆