2014-04-13 44 views
5

在我的春天應用程序,我有春天環境中的以下配置類:在Spring應用程序使用多個調度servlet的工作

WebAppInitializer.java

@Order(value=1) 
public class WebAppInitializer implements WebApplicationInitializer { 

    @SuppressWarnings("resource") 
    @Override 
    public void onStartup(ServletContext container) { 
     // Create the 'root' Spring application context 
     AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
     rootContext.register(WebAppConfig.class); 

     // Manage the lifecycle of the root application context 
     //container.addListener(new ContextLoaderListener(rootContext)); 

     // Create the dispatcher servlet's Spring application context 
     AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); 
     dispatcherContext.register(DispatcherConfig.class); 

     // Register and map the dispatcher servlet 
     ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("/"); 
    } 

} 

WebAppConfig.java

@EnableWebMvc 
@EnableTransactionManagement(mode=AdviceMode.PROXY, proxyTargetClass=true) 
@ComponentScan(value="spring.webapp.lojavirtual") 
@Configuration 
public class WebAppConfig extends WebMvcConfigurerAdapter { 

    @Override 
    public void addResourceHandlers(ResourceHandlerRegistry registry) { 
     registry.addResourceHandler("/bootstrap/**").addResourceLocations("/bootstrap/").setCachePeriod(31556926); 
     registry.addResourceHandler("/extras/**").addResourceLocations("/extras/").setCachePeriod(31556926); 
     registry.addResourceHandler("/jquery/**").addResourceLocations("/jquery/").setCachePeriod(31556926); 
    } 

    @Override 
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 
     configurer.enable(); 
    } 

} 

DispatcherConfig.java

@Controller 
@Import(WebAppConfig.class) 
public class DispatcherConfig { 

    @Bean 
    public ViewResolver jspResolver() { 
     InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
     viewResolver.setPrefix("/WEB-INF/jsp/"); 
     viewResolver.setSuffix(".jsp"); 
     return viewResolver; 
    } 

} 

我想添加其他調度的servlet我的應用程序。我的第一個想法是屁股下面的代碼上面的類:

在WebAppInitializer.java

一個新的塊這樣的,更改其名稱,在適當的地方:

// Create the dispatcher servlet's Spring application context 
     AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); 
     dispatcherContext.register(DispatcherConfig.class); 

     // Register and map the dispatcher servlet 
     ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); 
     dispatcher.setLoadOnStartup(1); 
     dispatcher.addMapping("/"); 

並添加一個新的類如DispatcherConfig.java,在上面的代碼中選擇了該名稱。

我的問題是:

1)首先,我的做法是添加一個新的分發程序Servlet的正確方法?

2)其次,如果問題1的答案是「是」,這名字,我應該在WebAppInitializer改變?

3)在我的控制器(S),我該怎麼sinalize爲其調度的servlet我徵用應該去?我的控制器使用類似下面的呼叫視圖方法:

@RequestMapping(value="view_mapping") 
public method() { 
    ModelAndView mav = new ModelAndView() 
    mav.setViewName("view_name"); 
    return mav; 
} 
+0

你幾乎總是不應該有多個servlet,而應該把所有你需要的映射添加到單個servlet中。你爲什麼認爲你需要更多? – chrylis

回答

9

你可以有許多DispatcherServlets你想要的。基本上你需要做的是複製配置並給servlet一個不同的名字(否則它會覆蓋前一個),並且有一些單獨的配置類(或者xml文件)。

你的控制器不應該關心他們運行的DispatcherServlet如果你包含代碼來檢測(如果你添加另一個,而另一個你需要不斷修改你的控制器來解決這個問題)。

然而,雖然通常可以有多個servlet,但對多個servlet沒有多少需求,您可以使用DispatcherServlet的單個實例處理它。

+0

拜託,你能告訴我使用多個dispatcherServlets有什麼好處嗎? –

+0

不在評論中...提出這個問題...... –

+0

好的。其實,人們在這裏爲這類問題而低估。我仍然會嘗試 –

1

如果你使用spring 3.2或更高版本,你可以使用下面的代碼。

使用重寫getServletName()方法爲所有dispacher servlet創建不同的類,以避免同名衝突。

public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

     @Override 
     protected Class<?>[] getRootConfigClasses() { 
      // TODO Auto-generated method stub 
      return new Class<?>[] { RootConfig.class }; 
     } 

     @Override 
     protected Class<?>[] getServletConfigClasses() { 
      // TODO Auto-generated method stub 
      return new Class<?>[] { WebConfig.class }; 
     } 

     @Override 
     protected String[] getServletMappings() { 
      // TODO Auto-generated method stub 
      return new String[] { "/config1/*" }; 
     } 
    } 




public class WebAppInitializer2 extends AbstractAnnotationConfigDispatcherServletInitializer { 


     @Override 
     protected Class<?>[] getRootConfigClasses() { 
      // TODO Auto-generated method stub 
      return new Class<?>[] { RootConfig.class }; 
     } 

     @Override 
     protected Class<?>[] getServletConfigClasses() { 
      // TODO Auto-generated method stub 
      return new Class<?>[] { WebConfig2.class }; 
     } 

     @Override 
     protected String[] getServletMappings() { 
      // TODO Auto-generated method stub 
      return new String[] { "/config2/*" }; 
     } 

      @Override 
      protected String getServletName() { 
       // TODO Auto-generated method stub 
      return "config2"; 
     } 
    } 
+3

所有調度程序servlet都不是通用的根配置類?這個事實在你的例子中意味着什麼? – croraf

+0

用於創建多個Web應用程序初始化程序,每個Web應用程序初始化程序類應該實現'WebApplicationInitializer'接口,而不是擴展'AbstractAnnotationConfigDispatcherServletInitializer'類 – jcflorezr

相關問題