Spring Boot中的DispatcherServlet和web.xml
我目前正在尝试将我的项目从Java EE转移到Spring Boot项目。 但是,我对调度程序servlet和web.xml的部分感到困惑和困惑,似乎不再有项目读取web.xml了。 当前项目在tomcat 7上运行。
在我的web.xml
文件中,我有很多servlet
, servlet-mapping
, filter
和filter mapping
,我真的不明白如何在调度程序中进行映射。
我在下面附上了我的web.xml
样本,版本是2.5。
displayName description ... Some Filter Name Some Filter Class Some Param Name Some Value Some Filter Name Some url-pattern Some Param Name Some Param Value Some Servlet Name Some Servlet Class Some Servlet Name Some Url Pattern
QNS:
- 我应该将我的
web.xml
所有内容转换为依赖于spring调度程序,如果是,我该如何实现呢? - 是远离
web.xml
的方式去春季启动项目吗?
谁能请我一起指导我? 谢谢!!
- 是的,spring boot不再在xml配置上进行中继,它会自动配置与调度程序servlet的等效项。 您可以按照以下链接查看如何注册filter: 如何在Spring Boot中添加filter类?
- 如果使用maven而不是gradle,则spring启动项目中唯一的XML应该是
pom.xml
。 使用spring boot的方法是将所有xml配置,web.xml等移动到spring boot的自动配置+ java配置。
当您在Java配置中执行所有操作并遵循其主体时,Spring启动非常有效。 根据我的经验,当你开始合并XML配置和遗留弹簧时,它开始打破自动配置过程,并且更好地尝试尽可能地遵守新的Spring引导最佳实践。
-
您可以保留
web.xml
,但需要添加org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener 在
web.xml
。 并且,需要pom.xml
依赖项。 -
所有监听器类,filter都在Java类中转换。 这个类是
@Configuration.
-
如果您有一个可以移动到配置类的截取器。