没有spring MVC的webapps的Spring配置

时间:2016-09-18 17:58:39

标签: spring spring-mvc servlets configuration

我正在尝试配置我的多模块项目,以便在没有Spring MVC的情况下使用Spring。

这是项目层次结构:

brewspberry-RPM-父

---- brewspberry-api(包含webservices)

---- brewspberry-core(包含服务和DAO)

---- brewspberry-webapp(包含网页,servlet,......)

brewspberry-core是webapp的maven依赖。

我尝试做的是能够在webapp中自动装配核心bean。我使用基于Java的配置。

这是我的Spring webapp初始化程序:

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的前一篇文章中尝试了一个解决方案,该解决方案包括创建一个包含以下内容的Abstract 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()

  • 通过自动装配

我确切地说核心配置在测试中有效。我得到的问题是webapp到核心配置

更新

通过删除overriden onStartup方法并将两个配置类添加到getRootConfigClasses(),创建了servletContext:

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

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

1 个答案:

答案 0 :(得分:0)

您正在扩展 mv /Users/becky/composer.phar /usr/local/bin/composer.phar ,但我们正在努力不使用应该使用的方式。

使用以下

替换您的课程
AbstractAnnotationConfigDispatcherServletInitializer

这将注册所需的所有内容(包括正确的servlet映射),并将为您已经工作的servlet创建代码。

主要问题是您已覆盖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"}; } } 方法,基本上会破坏onStartup的所有功能。这已经为您创建了AbstractAnnotationConfigDispatcherServletInitializerContextLoaderListener