首页 > 解决方案 > org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.web.servlet.HandlerMapping]:没有设置 ServletContext

问题描述

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

SpringConfiguration

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

@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

    }

SpringWebConfiguration 是 spring-mvc 的类:

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

HttpServerRunner,我试图初始化 SpringConfiguration 上下文的非弹簧类:

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();
}

因此,在 init 方法中,我需要SpringConfiguration加载上下文以获取 bean。

我收到的错误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)

我在调试模式下进入WebMvcConfigurationSuportservletContext属性为空

web.xml

<servlet>
            <servlet-name>springMvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextClass</param-name>
                <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
            </init-param>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>com.netoptics.server.SpringWebConfiguration</param-value>
            </init-param>
        </servlet>


    <context-param>
        <param-name>contextClass</param-name>
        <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
    </context-param>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>com.netoptics.server.SpringConfiguration</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

<servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>/v2/*</url-pattern>
    </servlet-mapping>

标签: javaspringspring-mvcservletsjetty

解决方案


推荐阅读