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

普里特曼尼:

我在Web应用程序中一起使用jsf和spring。我已经在一个配置类中配置了数据源和会话工厂,该配置类使用了诸如此类的注释@Configuration, @ComponentScan我在我的项目中没有任何applicationContext.xml文件,因为我正在处理Configuration类中上下文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,的确是,如果我没有明确给出contextConfigLocationparam web.xml,它将搜索名为applicationContext.xml的默认spring上下文文件web.xml现在,如果我不想使用spring上下文文件并且使用注释进行所有配置,该怎么办?我应该如何注册侦听器类,ContextLoaderListener以便在不使用xml文件且仅使用批注的情况下,能够使用spring和jsf运行Web应用程序?

托利蒂乌斯:

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注释。

进一步阅读:

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

是的,您当然需要听众。尽管以上内容完全回答了“ 如何在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 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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android Studio中注释我的XML文件

如何在一个Spring应用程序中的web.xml中注册多个servlet

如何编写Spring ApplicationContext.xml文件?

如何验证spring applicationContext.xml文件

Spring无法从ApplicationContext xml文件加载类

如何将applicationContext.xml转换为spring @Configuration类?

如何在XML中注释一行?

如何在spring xml配置中注入环境变量?

Maven:如何在web.xml文件中填充变量

如何在pom.xml文件中设置主类

如何在Spring Test中注册ApplicationEnvironmentPreparedEvent

如何在Spring Boot中注册ServletContextListener

如何在春季将xml bean配置文件导入到@configuration类?

如何通过XML RPC在Odoo 11中注册付款到发票

使用Spring Batch和Spring Boot 2.2.5时,无法从xml配置中注册Custom LineMapper

如何将XML字符串发布到Web服务而不是xml文件

如何指定将在Spring中注册bean的默认配置文件?

如何在.sbt文件中注释行

如何在.plist文件中注释行?

如何在文件中注释行

如何在.xml文件而不是.java文件中设置自定义字体?

如何在不是有效xml文件的文件中生成关闭节点?

从Web检索XML文件

如何在AutoFac中注册配置文件AutoMapper?

如何在Unity中注册AutoMapper配置文件

如何在VB6中注册.ocx文件?

C#如何在数据集WriteXml()函数生成xml文件时使用XDocument类更新xml文件

使用JPA的Spring安全性。如何配置applicationContext-security.XML文件?(使用DaoAuthenticationProvider)

如何在python中注册从抽象类继承的类