英文:
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("...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
...
}
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
专注分享java语言的经验与见解,让所有开发者获益!
评论