java - 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)
我在调试模式下进入WebMvcConfigurationSuport
,servletContext
属性为空
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>
解决方案
推荐阅读
- typescript - 如何在 Visual Studio Code 中加载 TypeScript 错误日志文件
- html - 输入 1 个字符后输入框失去焦点
- php - netbeans 设置版本 php 7.2.12 i
- r - 在R中查找一列的最大值和最小值以及其他两列的差异
- javascript - 计算地点之间的行驶距离
- haskell - 我可以在 Haskell 中定义参数不相等的参数数据类型吗?
- java - Timertask 类是否合适?
- java - 如何使用 CriteriaQuery 查询 OneToOne 实体的特定值
- javascript - 悬停时每个字母的橡皮筋
- powershell - 如何删除文件夹并将其内容移动到 Powershell 中的父级?