如何使两个AbstractAnnotationConfigDispatcherServletInitializer共享一个根上下文?

huangapple 未分类评论43阅读模式
英文:

How i can make two AbstractAnnotationConfigDispatcherServletInitializer's with one root context?

问题

@Override
public void onStartup(ServletContext servletContext) throws ServletException {

    // 根上下文
    AnnotationConfigWebApplicationContext rootContext =
            new AnnotationConfigWebApplicationContext();
    rootContext.register(RootConfig.class); // 根上下文的配置类
    rootContext.scan("...service", "...dao"); // 仅扫描某些包
    servletContext.addListener(new ContextLoaderListener(rootContext));

    // 分发器servlet 1
    AnnotationConfigWebApplicationContext webContext1 = 
            new AnnotationConfigWebApplicationContext();
    webContext1.setParent(rootContext);
    webContext1.register(WebConfig1.class); // servlet 1 的配置类
    webContext1.scan("...web1");            // 扫描其他一些包
    ServletRegistration.Dynamic dispatcher1 =
    servletContext.addServlet("dispatcher1", new DispatcherServlet(webContext1));
    dispatcher1.setLoadOnStartup(1);
    dispatcher1.addMapping("/subcontext1");

    // 分发器servlet 2
    ...
}
@Override
protected Class<?>[] getRootConfigClasses() {
    return new Class[]{RootConfig.class}; // 根上下文的配置类
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[]{WebConfig1.class}; // servlet 1 的配置类
}

@Override
protected String[] getServletMappings() {
    return new String[]{"/subcontext1"}; // 映射到的URL路径
}
英文:

I know that i can write to two dispatchers servlets with one root context in this way:

@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(&quot;...service&quot;, &quot;...dao&quot;); // 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(&quot;...web1&quot;);            // scan some other packages
ServletRegistration.Dynamic dispatcher1 =
servletContext.addServlet(&quot;dispatcher1&quot;, new DispatcherServlet(webContext1));
dispatcher1.setLoadOnStartup(1);
dispatcher1.addMapping(&quot;/subcontext1&quot;);

// dispatcher servlet 2
...

}

But how i can do this with AbstractAnnotationConfigDispatcherServletInitializer ? If it is impossible - why we can two methods "getRootConfigClasses" and "getServletConfigClasses" ?

答案1

得分: 0

我认为我已经找到了解决方案 - 如果我们使用两个类与AbstractAnnotationConfigDispatcherServletInitializer - 这意味着如果我们在其中一个类中的getRootConfigClasses方法中定义了根类,而另一个类将返回一个空数组"{}"作为类 - 第一个根类将成为两个DispatcherServlet的根类。

英文:

I think i had found decision - if we use two clases with AbstractAnnotationConfigDispatcherServletInitializer - it is meanining if we define in one of them Root classes in method getRootConfigClasses and another will return empty array "{}" of clases - first root classes will be root classes for both DispatcherServlet's

huangapple
  • 本文由 发表于 2020年4月5日 08:13:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/61036471.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定