我想配置我的多模塊項目使用沒有彈簧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};
}
您正在擴展'AbstractAnnotationConfigDispatcherServletInitializer',但正在努力不使用它應該使用的方式。以適當的方式使用它。此外,您嘗試的只能用於根上下文,而ContextLoaderListener加載的上下文不適用於另一個servlet加載的ocntext。由'DispatcherServlet'加載的'ApplicationContext'默認情況下是'DIspatcherServlet'的私有。 –