2016-09-18 231 views
0

我想配置我的多模塊項目使用沒有彈簧MVC的Spring。沒有彈簧的web應用程序的彈簧配置MVC

這裏是項目的層次結構:

brewspberry-RPM-父

---- brewspberry-API(包含web服務)

---- brewspberry芯(含有服務和DAO )

---- brewspberry-web應用(含網頁頁面,servlet,...)

brewspberry核心是Web應用程序的Maven的依賴。

我試圖做的是能夠在webapp中自動裝入核心bean。我使用基於Java的配置。

這裏是我的春天web應用程序初始化:

public class SpringWebappInitializer extends 
    AbstractAnnotationConfigDispatcherServletInitializer implements 
    WebApplicationInitializer { 

public void onStartup(ServletContext servletContext) 
     throws ServletException { 

    AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 

    rootContext.setServletContext(servletContext); 

    // rootContext.setConfigLocation("net.brewspberry.util"); 

    rootContext.register(SpringCoreConfiguration.class); 

    //servletContext.addListener(new ContextLoaderListener(rootContext)); 

    getWebAppContext(servletContext); 

} 




private void getWebAppContext(ServletContext servletContext) { 
    // now the config for the Dispatcher servlet 
    AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext(); 
    // mvcContext.setConfigLocation("net.brewspberry.util.config"); 
     mvcContext.register(SpringWebappConfiguration.class); 


    ServletRegistration.Dynamic dispatcher = servletContext.addServlet(
      "DispatcherServlet", new DispatcherServlet(mvcContext)); 

    dispatcher.setLoadOnStartup(1); 
    dispatcher.addMapping("*.do"); 
} 




@Override 
protected Filter[] getServletFilters() { 
    return null; // new Filter[] { new AuthentificationFilter() }; 

} 

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

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

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

的配置類是:

@Configuration 
@EnableWebMvc 
@ComponentScan({ "net.brewspberry" }) 
public class SpringWebappConfiguration extends WebMvcConfigurerAdapter { 

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

@Override 
public void addResourceHandlers(ResourceHandlerRegistry registry) { 
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); 
} 

@Override 
public void addViewControllers(ViewControllerRegistry registry) { 
    registry.addViewController("/").setViewName("forward:/login.jsp"); 
} 

@Bean(name = "viewResolver") 
public InternalResourceViewResolver getViewResolver() { 
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 
    viewResolver.setPrefix("/"); 
    viewResolver.setSuffix(".jsp"); 
    return viewResolver; 
} 
} 

我想是的servlet可以從Brewspberry核心模塊注入服務。

我試圖從以前的帖子中SO的解決方案,在創建包含這個抽象的Servlet包括:

@Override 
public void init(ServletConfig arg0) throws ServletException { 

    // Autowire beans in webapp 

    final AutowireCapableBeanFactory autowireCapableBeanFactory = WebApplicationContextUtils 
      .getWebApplicationContext(servletContext) 
      .getAutowireCapableBeanFactory(); 
    autowireCapableBeanFactory.autowireBean(this); 
} 

我試過幾件事情,但得到參數servletContext當我仍然得到一個NullPointerException異常:

  • 從arg0.getServletContext()

  • 通過自動裝配它

我確定核心配置在測試中起作用。我的問題是與Web應用程序到核心配置

更新

通過刪除重寫onStartup方法和添加這兩種配置類getRootConfigClasses(),創建參數servletContext:

@Override 
protected Class<?>[] getRootConfigClasses() { 

    return new Class<?>[]{SpringCoreConfiguration.class, SpringWebappConfiguration.class}; 
} 
+0

您正在擴展'AbstractAnnotationConfigDispatcherServletInitializer',但正在努力不使用它應該使用的方式。以適當的方式使用它。此外,您嘗試的只能用於根上下文,而ContextLoaderListener加載的上下文不適用於另一個servlet加載的ocntext。由'DispatcherServlet'加載的'ApplicationContext'默認情況下是'DIspatcherServlet'的私有。 –

回答

0

你是在擴展AbstractAnnotationConfigDispatcherServletInitializer,但正在努力不使用它應該使用的方式。

具有以下

public class SpringWebappInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

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

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

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

更換你的類,將註冊所有需要(包括適當的servlet映射),並會爲你已經有工作的servlet代碼。

主要問題是,您已經重寫了onStartup方法,基本上破壞了AbstractAnnotationConfigDispatcherServletInitializer的所有功能。這已經爲您創建了ContextLoaderListenerDispatcherServlet

+0

太棒了!通過在getRootConfigClasses()中聲明webapp和core config,現在servlet上下文不再爲空,我可以將核心服務注入控制器。非常感謝 ! – Biologeek

+0

您的網絡應用程序不應該是根配置的一部分!這應該是servlet配置的一部分。我真的希望你沒有做到這一點,因爲這意味着你將你的應用程序的一部分加載兩次! –