栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Web.xml中注册Spring @Configuration带注释的类而不是applicationContext.xml文件?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Web.xml中注册Spring @Configuration带注释的类而不是applicationContext.xml文件?

web.xml
你需要引导上下文
AnnotationConfigWebApplicationContext

<servlet>    <servlet-name>appServlet</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> org.package.YouConfigurationAnnotatedClass        </param-value>    </init-param></servlet>

并且不要忘了使用

@EnableWebMvcMVC
注释。

EDIT as a “comments follow up” => to be Turing Complete:

是的,你当然需要听众。尽管以上内容完全回答了“ 如何在Web.xml中注册Spring

@Configuration
带注释的类而不是applicationContext.xml文件 ”的问题,但这是Spring官方文档中的一个示例,其布局完整
web.xml

<web-app>  <!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext       instead of the default XmlWebApplicationContext -->  <context-param>      <param-name>contextClass</param-name>      <param-value>          org.springframework.web.context.support.AnnotationConfigWebApplicationContext      </param-value>  </context-param>  <!-- Configuration locations must consist of one or more comma- or space-delimited       fully-qualified @Configuration classes. Fully-qualified packages may also be       specified for component-scanning -->  <context-param>      <param-name>contextConfigLocation</param-name>      <param-value>com.acme.AppConfig</param-value>  </context-param>  <!-- Bootstrap the root application context as usual using ContextLoaderListener -->  <listener>      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <!-- Declare a Spring MVC DispatcherServlet as usual -->  <servlet>      <servlet-name>dispatcher</servlet-name>      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>      <!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContextinstead of the default XmlWebApplicationContext -->      <init-param>          <param-name>contextClass</param-name>          <param-value>   org.springframework.web.context.support.AnnotationConfigWebApplicationContext          </param-value>      </init-param>      <!-- Again, config locations must consist of one or more comma- or space-delimitedand fully-qualified @Configuration classes -->      <init-param>          <param-name>contextConfigLocation</param-name>          <param-value>com.acme.web.MvcConfig</param-value>      </init-param>  </servlet>  <!-- map all requests for /app/* to the dispatcher servlet -->  <servlet-mapping>      <servlet-name>dispatcher</servlet-name>      <url-pattern>/app/*</url-pattern>  </servlet-mapping></web-app>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/404918.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号