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

2023-12-31

我在 Web 应用程序中一起使用 jsf 和 spring。我在一个配置类中配置了数据源和会话工厂,该类使用注释,例如@Configuration, @ComponentScan etc. 我的项目中没有任何 applicationContext.xml 文件因为我正在处理配置类中上下文 xml 的每个条目。测试用例成功运行,但是当我部署 Web 应用程序时,出现错误

java.lang.IllegalStateException:未找到 WebApplicationContext:否 ContextLoaderListener 已注册?

现在,如果我在 web.xml 中提供侦听器类,

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

它给了我错误,

找不到/WEB-INF/applicationContext.xml

根据文件ContextLoaderListener,确实,如果我不给contextConfigLocation参数输入web.xml明确地,它将搜索名为的默认 spring 上下文文件applicationContext.xml in web.xml。现在,如果我不想使用 spring 上下文文件并使用注释进行所有配置,我该怎么办?我应该如何注册监听类ContextLoaderListener这样,在不使用 xml 文件并仅使用注释的情况下,我就能够使用 spring 和 jsf 运行我的 Web 应用程序?


In 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>

并且不要忘记使用@EnableWebMvc让您的 MVC 注释生效。

进一步阅读:

  • Spring 3.1 MVC 增强 http://blog.springsource.com/2011/06/13/spring-3-1-m2-spring-mvc-enhancements-2/
  • Spring 3.1 MVC命名空间增强和配置 http://blog.springsource.com/2011/02/21/spring-3-1-m1-mvc-namespace-enhancements-and-configuration/

编辑为“评论跟进” => 成为图灵完备:

是的,你当然需要一个倾听者。虽然上面完全回答了问题“如何在web.xml中注册Spring @Configuration注解的类而不是applicationContext.xml文件”,这里有一个example http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/htmlsingle/spring-framework-reference.html#beans-java-instantiating-container-web来自 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 AnnotationConfigWebApplicationContext
           instead 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-delimited
           and 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在web.xml中注册Spring @Configuration注解的类而不是applicationContext.xml文件? 的相关文章

随机推荐

  • Python 3:如何让“else”语句仅在“if”语句都不为真时才适用?

    抱歉 这里是初学者 尝试使该程序扫描某个字母组合 如果没有找到任何字母组合 则返回 else 语句 但是 我不知道如何仅当所有 if 语句返回 False 时才应用 else 语句 这是我的代码 class color BOLD 033 1
  • 正确访问存在重复索引值的切片

    我有一个带有索引的数据框 有时包含具有相同索引值的行 现在我想对该数据帧进行切片并根据行索引设置值 考虑以下示例 import pandas as pd df pd DataFrame index 1 2 2 3 values 10 20
  • 在 Selenium python 中通过 xpath 模式查找元素

    我正在使用 selenium python 和 lettuce 来测试 django 应用程序 以下模式中有许多具有 xpath 的元素 我不知道文档中存在多少个这些元素 id accordion note 1 id accordion n
  • “查找最近位置”如何运作?

    如今 大多数餐馆和其他企业都拥有 查找地点 http www fedex com Dropoff start 他们网站上的功能列出了给定地址 邮政编码的最近位置 这是如何实现的 将邮政编码与数据库进行匹配是一种简单的 无需思考的方法 但可能
  • C++17 中有 typeid 的反函数吗?

    C 17 是否提供了一种从 typeid 获取类型的方法 或者工厂模式仍然是唯一的方法 type info是运行时值 其确切内容只能通过运行时执行来确定 C 是一种静态类型语言 在编译时 类型一切必须被知道 像这样 type info基于具
  • 通过 spring-data 迭代 MongoDB 中的大型集合

    Friends 我通过 spring data 在 java 项目中使用 MongoDB 我使用存储库接口来访问集合中的数据 对于某些处理 我需要迭代集合的所有元素 我可以使用存储库的 fetchAll 方法 但它总是返回 ArrayLis
  • Mule OAuth2 使用客户端凭证作为 grant_type

    我的要求是使用OAuth2的client credentials grant type来获取Mule中的访问令牌 我想实现一个支持 OAuth 的自定义连接器 我无法使用以下配置来实现它
  • 电脑锁定时如何发送电子邮件?

    我想使用 Excel VBA 发送 Outlook 电子邮件 代码Sendupdate手动运行时有效 我的第二个宏StartTimer旨在当我不在办公桌前时在设定时间执行上述操作 当计算机被锁定时 电子邮件不会发送 当我回到办公桌前时 电子
  • Itext7 HTML2PDF - PDF 文件中的图片未旋转

    我有一个 HTML 文件 其中包含一些旋转的图片 当我将 HTML 文件转换为 PDF 文件时 图片不会旋转 看来 EXIF 0112 没有正确应用 iText Html2Pdf HtmlConverter ConvertToPdf HTM
  • JQuery 字符串包含检查[重复]

    这个问题在这里已经有答案了 我需要检查一个字符串是否包含另一个字符串 var str1 ABCDEFGHIJKLMNOP var str2 DEFG 我应该使用哪个函数来确定 str1 是否包含 str2 你可以使用javascriptin
  • 我可以向另一个域发出 XMLHttpRequest 吗?

    有没有办法将 XMLHttpRequest 与其他域结合使用 我想解析来自 Google 的一些 xml 而无需使用服务器 因此运行起来非常简单 var req getXmlHttpRequestObject req open GET ht
  • Python截断长字符串

    如何在 Python 中将字符串截断为 75 个字符 JavaScript 中是这样完成的 var data saddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
  • Windows 上的 npm 安装失败并出现错误签名错误

    我正在使用 nvm 来管理我的节点版本 我有一个使用节点 6 10 2 的项目 像这样简单的事情npm install g gulp我收到以下错误 write EPROTO 101057795 error 1408D07B SSL rout
  • 如何使用c#打开excel文档

    我用过创建新的电子表格并保存到用户文件夹 我希望保存后能够自动打开它 该文件为 xlsx 格式 I tried SpreadsheetDocument Open fileName true 这根本不起作用 我想要代码在用户拥有的任何版本的
  • Chrome 88 时区符号不正确

    Starting from about 20 01 2021 after the Chrome update to version 88 0 the time zone incorrectly processed in applicatio
  • 根据类型返回一个值

    考虑下面的例子 template
  • Apache Zeppelin 问题 - Python 错误

    我对 Apache Zeppelin 有疑问 当我运行一个新的 python 笔记本并尝试执行类似的命令时import dask它会导致以下错误 Traceback most recent call last File tmp 159931
  • 从 UIImage 获取二进制数据

    我有一个要求 我必须将图像从 ios 设备发送到另一个设备 corebluetooth 其他设备是 BLE 设备 基本需求是 1 图像尺寸应为128X160 2 8 位 即每种颜色 8 位 3 3个通道 4 每像素 24 位 5 图像应有
  • SAML2.0认证后调用Graph api。如何获取所需的身份验证令牌

    我们的 Web aap 通过 SAML2 0 与 Azure AD 进行身份验证 类似于this https learn microsoft com en us azure active directory develop active d
  • 如何在web.xml中注册Spring @Configuration注解的类而不是applicationContext.xml文件?

    我在 Web 应用程序中一起使用 jsf 和 spring 我在一个配置类中配置了数据源和会话工厂 该类使用注释 例如 Configuration ComponentScan etc 我的项目中没有任何 applicationContext