具有多个调度程序的Spring Java Config

del*_*ter 9 java spring spring-mvc spring-java-config

我现在有一些体验Spring,并且还有一些纯Java配置web-apps正在使用中.但是,这些通常基于一个安静的简单设置:

  • 服务/存储库的应用程序配置
  • 一个调度程序(和一些控制器)的调度程序配置
  • (可选)弹簧安全性以确保访问

对于我当前的项目,我需要具有不同配置的单独调度程序上下文.这不是基于XML的配置的问题,因为我们有一个独立于Dispatcher Configuration的专用ContextLoaderListener.但是使用java配置我不确定到目前为止我做的是否正常;)

这是一个常见的DispatcherConfig:

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

  @Override
  protected String getServletName() {
    return "myservlet";
  }
}
Run Code Online (Sandbox Code Playgroud)

如上所述,我需要第二个(第三个,......)调度程序与另一个映射(和视图解析器).因此,我复制了配置并添加了两个getServletName()(否则两者都将被命名为'dispatcher',这将导致错误).第二个配置看起来像这样:

public class AnotherWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

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

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

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

  @Override
  protected String getServletName() {
    return "anotherservlet";
  }
}
Run Code Online (Sandbox Code Playgroud)

当我像这样使用它时,启动应用程序会导致ContextLoaderListener出现问题:

java.lang.IllegalStateException: Cannot initialize context because there is already a root application context present - check whether you have multiple ContextLoader* definitions in your web.xml!
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:277)
...
Run Code Online (Sandbox Code Playgroud)

所以我从AbstractAnnotationConfigDispatcherServletInitializer中删除了第二个MyAppConfig.class返回 ,它工作正常.然而,这感觉不是正确的方式;)

根据我的理解:是否应该在一个AbstractAnnotationConfigDispatcherServletInitializer中处理所有DispatcherConfig,还是应该像我一样将它们分开?我尝试在一个类中配置它们,但后来我的配置完全混合(所以我认为这不是理想的方式).

你是如何实施这种情况的?是否可以在AbstractAnnotationConfigDispatcherServletInitializer之外的java配置中设置ContextLoaderListener?或者我应该创建一个只有root配置的DefaultServlet?如何实现该配置WebApplicationInitializer的基本接口?

Ser*_*sta 13

Mahesh C.展示了正确的道路,但他的实施太有限了.他是对的:你不能直接AbstractAnnotationConfigDispatcherServletInitializer用于多个调度程序servlet.但实施应该:

  • 创建根应用程序上下文
  • 给它一个初始配置,并说出它应扫描的包
  • 为它添加一个ContextListener到servlet上下文
  • 然后为每个调度程序servlet
    • 创建子应用程序上下文
    • 给它提供相同的初始配置和扫描包
    • 使用上下文创建DispatcherServlet
    • 将它添加到servlet上下文

这是一个更完整的实现:

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // root context
    AnnotationConfigWebApplicationContext rootContext =
            new AnnotationConfigWebApplicationContext();
    rootContext.register(RootConfig.class); // configuration class for root context
    rootContext.scan("...service", "...dao"); // scan only some packages
    servletContext.addListener(new ContextLoaderListener(rootContext));

    // dispatcher servlet 1
    AnnotationConfigWebApplicationContext webContext1 = 
            new AnnotationConfigWebApplicationContext();
    webContext1.setParent(rootContext);
    webContext1.register(WebConfig1.class); // configuration class for servlet 1
    webContext1.scan("...web1");            // scan some other packages
    ServletRegistration.Dynamic dispatcher1 =
    servletContext.addServlet("dispatcher1", new DispatcherServlet(webContext1));
    dispatcher1.setLoadOnStartup(1);
    dispatcher1.addMapping("/subcontext1");

    // dispatcher servlet 2
    ...
}
Run Code Online (Sandbox Code Playgroud)

这样,您可以完全控制哪些bean将在哪个上下文中结束,就像使用XML配置一样.


小智 7

如果您使用通用WebApplicationInitializer接口而不是使用spring提供的抽象实现 - AbstractAnnotationConfigDispatcherServletInitializer,我认为您可以解决这个问题.

这样,您可以创建两个单独的初始化程序,因此您将在startUp()方法上获得不同的ServletContext,并为每个初始化程序注册不同的AppConfig和dispatcher servlet.

其中一个实现类可能如下所示:

public class FirstAppInitializer implements WebApplicationInitializer {

    public void onStartup(ServletContext container) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(AppConfig.class);
        ctx.setServletContext(container);

        ServletRegistration.Dynamic servlet = container.addServlet(
                "dispatcher", new DispatcherServlet(ctx));

        servlet.setLoadOnStartup(1);
        servlet.addMapping("/control");

    }

}
Run Code Online (Sandbox Code Playgroud)