当前位置: 首页 > 面试题库 >

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

韩经武
2023-03-14
问题内容

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

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 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提供注释映射。我已经完成了web.XML.我想尝试使用注释映射,如下所示:

  • 几乎所有的事情都很顺利,我可以打开和,我来到controller.java。但是当我打开时,我得到一个404错误。 我将Eclipse与一个“动态Web项目”一起使用,controller.java文件位于/src(默认包)下,Web.xml文件位于/webcontent/web-inf下。 我希望你能给我点提示。

  • 我正在使用注释处理器来处理方法参数的注释。 用于参数的注释类型有一个注释@参数 现在,当注释处理器运行时,我想检查参数注释()是否有参数注释。我通过执行以下代码来实现这一点。 由于某种原因,arg始终为空。是否有注释未返回的原因?

  • 我有一个Spring bean,在Spring Bean中我有一个对其他bean列表的依赖项。我的问题是:如何注入一个通用的bean列表作为该bean的依赖项? 例如,一些代码: 我的豆子: 问题是:;如何在Painter中获得颜色列表?另外,还有一点:我应该让@Configuration返回接口类型还是类? 谢谢你的帮助!

  • 我正在学习Spring Core认证,我对这个基于学习材料的问题的答案有一些疑问。 为什么不允许使用@Configuration注释最终类 为了证实这一论断,我的理由如下: 考虑下面的配置类: 乍一看,这种情况可能看起来很奇怪,因为第一个方法(帐户存储库())将JdbcAcCountRepository对象实例化为一个bean,该对象具有id=AcCountRepository,遵循Spring默

  • 我想使用Mongock迁移工具来初始化存储在数据库中的应用程序配置。我遇到的问题是我的一个配置在使用@Configuration注释的类中使用。由于Mongock更改集在@Configuration之后执行,它无法从数据库中检索尚未存在的值,这会导致应用程序崩溃。有没有办法推迟创建@Configuration类?或者我应该在不使用mongock的情况下初始化这个配置?