org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.web.servlet.HandlerMapping]:未设置ServletContext

时间:2019-05-13 09:44:11

标签: java spring spring-mvc servlets jetty

我正在尝试在应用程序中启用SpringMVC。该应用程序具有旧代码,并且Spring的集成非常困难。我有一个名为HttpServerRunner的主班。在此类的init方法中,我正在调用initSpringContext,这给了我一个新的root spring上下文实例。 现在,如果我要添加@EnableWebMvc配置,则旧方法initSpringContex会抛出No ServletContext setservletContext的{​​{1}}属性似乎为空。我提到WebMvcConfigurationSupport这个主要课程不是春季课程。我认为我打电话给HttpServerRunner太早了,或者类似这样。如果我删除new ApplicationContex配置,则可以使用RestControllers,但不能使用Jackson来返回对象。

@EnableWebMvc

是包含我的应用程序中所有bean的类:

SpringConfiguration

SpringWebConfiguration是spring-mvc的类:

@Configuration
@EnableScheduling
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.netoptics", "com.whitelist.manager", "com.websilicon", "com.wsnms.server", "com.anue", "com.wsnms"})
public class SpringConfiguration {

    @Bean
    ......

    @Bean
    .....

        @Bean
        ......

        @Bean
        .....

        @Bean

    }

HttpServerRunner,我尝试在其中初始化SpringConfiguration上下文的非spring类:

@Configuration
@EnableWebMvc
@ComponentScan({"com"})
public class SpringWebConfiguration {
}

因此,在init方法中,我需要加载public class HttpServerRunner implements WsHttpServerUpdateable { private ApplicationContext springAppContext; private void initSpringAppContext() { this.springAppContext = new AnnotationConfigApplicationContext(SpringConfiguration.class); } } private void init(String[] args) throws Throwable { WsObjectRegistry.replaceInstance(RedundancyListener.class, DatabaseReplication.getInstance()); initSpringAppContext(); initHttpServer(); } 上下文以获取bean。

我在SpringConfiguration中收到错误:

initSpringAppContext

我以Caused By: BeanCreationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) 进入调试模式,WebMvcConfigurationSuport属性为空

web.xml

servletContext

0 个答案:

没有答案