Vaadin18升级22使用web.xml与原生Spring启动报错如何解决?
配置调整步骤
1. 修改web.xml配置
首先删除已废弃的disable.automatic.servlet.registration上下文参数,替换为Vaadin 22+支持的新版配置,完整调整后的web.xml核心配置如下:
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="false"> <!-- metadata-complete="false"必须保留,确保容器加载Vaadin的初始化器 --> <context-param> <!-- 新版禁用自动Servlet注册的参数名 --> <param-name>vaadin.disable-automatic-servlet-registration</param-name> <param-value>true</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>AppServlet</servlet-name> <servlet-class>org.example.AppServlet</servlet-class> <!-- 新增异步支持配置,Vaadin 22+要求Servlet开启异步支持 --> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>AppServlet</servlet-name> <url-pattern>/app</url-pattern> <url-pattern>/app/*</url-pattern> </servlet-mapping> </web-app>
2. 优化自定义Servlet实现
你当前的AppServlet只是代理SpringServlet,可以直接继承SpringServlet减少冗余代码,调整后代码如下:
/** * Servlet to wire Vaadin into web.xml. */ public class AppServlet extends SpringServlet { public AppServlet() { // 第二个参数false代表关闭自动路由扫描,和你原有逻辑保持一致 super(false); } @Override protected WebApplicationContext getWebApplicationContext() { // 从Servlet上下文获取Spring原生上下文 return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); } }
如果不想修改原有Servlet的继承结构,也可以保留原有代理逻辑,只需确保自定义Servlet开启异步支持即可。
3. 补充Spring上下文配置
在你的Spring配置类上添加@EnableVaadin注解,指定你的Vaadin UI、视图类所在的包路径,示例如下:
@Configuration @EnableVaadin("org.example") // 替换为你项目中Vaadin相关代码所在的根包 @ComponentScan(basePackages = "org.example") public class SpringConfig { // 你的其他Spring配置 }
这个注解会自动注册Vaadin运行需要的核心Bean,缺失会导致Lookup初始化不完整。
内容的提问来源于stack exchange,提问作者tanderson
相关产品推荐
相关产品推荐

