如何在 web.xml 中注册 Spring @Configuration 注释类而不是 applicationContext.xml 文件? [英] How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml?

查看:27
本文介绍了如何在 web.xml 中注册 Spring @Configuration 注释类而不是 applicationContext.xml 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using jsf and spring together in web application. I have configured datasource and session factory in one configuration class which uses annotations like @Configuration, @ComponentScan etc. I don't have any applicationContext.xml file in my project as I am handling every entry of context xml in Configuration class. The test case works successfully but when I deploy my web application, it gives me error

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

java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

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

Now if I give listener class in web.xml,

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

它给了我错误,

/WEB-INF/applicationContext.xml 未找到

/WEB-INF/applicationContext.xml not found

根据ContextLoaderListener的文档,如果我在web.xml中不明确给出contextConfigLocation参数,它确实会在 web.xml 中搜索名为 applicationContext.xml 的默认 spring 上下文文件.现在,如果我不想使用spring上下文文件并使用注释进行所有配置,我该怎么办?我应该如何注册侦听器类 ContextLoaderListener 以便在不使用 xml 文件和仅使用注释的情况下,我能够使用 spring 和 jsf 运行我的 Web 应用程序?

As per the document of ContextLoaderListener, it's true that if I don't give contextConfigLocation param in web.xml explicitly, it will search for the default spring context file named applicationContext.xml in web.xml. Now, what should I do if I don't want to use spring context file and do all the configuration with annotations? How should I register listener class ContextLoaderListener so that without use of xml file and using annotations only, I be able to run my web application with spring and jsf?

推荐答案

web.xml 中,您需要使用 AnnotationConfigWebApplicationContext 引导上下文:

In web.xml you need to bootstrap the context with 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 注释启动.

And don't forget to use @EnableWebMvc for your MVC annotations to kick in.

进一步阅读:

是的,你当然需要一个听众.虽然上面完全回答了如何在 web.xml 中注册 Spring @Configuration 注解类而不是 applicationContext.xml 文件"的问题,但这里是一个 示例来自布局完整 web.xml 的 Spring 官方文档:

Yes of course you need a listener. Although the above completely answers the question "How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml", here is an example from Spring official documentation that layouts the full 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>

这篇关于如何在 web.xml 中注册 Spring @Configuration 注释类而不是 applicationContext.xml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆