具有多个调度程序的Spring Java配置

编程入门 行业动态 更新时间:2024-10-27 14:33:16
本文介绍了具有多个调度程序的Spring Java配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我现在有一些体验Spring,并且还使用了一些纯Java配置web应用程序。但是,这些通常基于一个安静的简单设置:

I've some experience Spring now and also have some pure java config web-apps in use. However, these are usually based on a quiet simple setup:

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

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

For my current project I need to have separate dispatcher contexts with different configuration. That's not a problem with the XML based configuration as we have a dedicated ContextLoaderListener that's independent from Dispatcher Configuration. But with java config I'm not sure if what I'm doing is fine so far ;)

这是一个常见的DispatcherConfig:

Here's a common 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"; } }

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

As said, I need a second (third, ...) dispatcher with another mapping (and view resolvers). So, I copied the config and added for both getServletName() (otherwise both will be named as 'dispatcher' which will cause errors). The second config was looking like that:

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"; } }

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

When I use it like this, starting application results in a problem with 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) ...

所以我从其中一个 AbstractAnnotationConfigDispatcherServletInitializer 中删除了第二个 MyAppConfig.class ,它运行正常。但是,这感觉不是正确的方法;)

So I removed the second MyAppConfig.class return from one of the AbstractAnnotationConfigDispatcherServletInitializer and it works fine. However, that doesn't feel to be the right way ;)

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

For my understanding: should all DispatcherConfig be handled within one AbstractAnnotationConfigDispatcherServletInitializer or should I separate them as I did? I tried to configure them in one class but then my config was totally mixed (so I believe that's not the desired way).

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

How do you implement such a case? Is it possible to set the ContextLoaderListener in java config outside of the AbstractAnnotationConfigDispatcherServletInitializer? Or should I create a DefaultServlet which has only the root config? What about implementing the base interface of that configuration WebApplicationInitializer?

推荐答案

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

Mahesh C. showed the right path, but his implementation is too limited. He is right on one point : you cannot use directly AbstractAnnotationConfigDispatcherServletInitializer for multiple dispatcher servlet. But the implementation should :

  • 创建根应用程序上下文
  • 给它一个初始配置并说它应扫描的包
  • 为它添加一个ContextListener到servlet上下文
  • 然后为每个调度程序servlet
    • 创建子应用程序上下文
    • 为其提供相同的初始配置和要扫描的包
    • 使用上下文创建DispatcherServlet
    • 将其添加到servlet上下文
    • create a root application context
    • gives it an initial configuration and say what packages it should scan
    • add a ContextListener for it to the servlet context
    • then for each dispatcher servlet
      • create a child application context
      • gives it the same an initial configuration and packages to scan
      • create a DispatcherServlet using the context
      • add it to the servlet context

      这是一个更完整的实现:

      Here is a more complete implementation :

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

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

      That way, you have full control on which beans will end in which context, exactly as you would have with XML configuration.

更多推荐

具有多个调度程序的Spring Java配置

本文发布于:2023-11-25 08:04:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   程序   Java   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!