首页 文章

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

提问于
浏览
66

我在web应用程序中一起使用jsf和spring . 我已经在一个配置类中配置了数据源和会话工厂,该配置类使用 @Configuration, @ComponentScanI don't have any applicationContext.xml file in my project 之类的注释,因为我正在处理Configuration类中上下文xml的每个条目 . 测试用例成功运行但是当我部署我的Web应用程序时,它给了我错误

java.lang.IllegalStateException:找不到WebApplicationContext:没有注册ContextLoaderListener?

现在如果我在web.xml中给出listener类,

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

它给了我错误,

找不到/WEB-INF/applicationContext.xml

根据 ContextLoaderListener 的文档,'s true that if I don'在 web.xml 中明确地给出 contextConfigLocation param,它将在 web.xml 中搜索名为 applicationContext.xml 的默认 spring 上下文文件 . 现在,如果我不想使用spring上下文文件并使用注释进行所有配置,我该怎么办?我应该如何注册监听器类 ContextLoaderListener ,以便不使用xml文件和仅使用注释,我能够使用spring和jsf运行我的Web应用程序?

2 回答

  • 11

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

    进一步阅读:

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

    是的,你当然需要一个听众 . 虽然上面完全回答了“如何在web.xml中注册Spring @Configuration注释类而不是applicationContext.xml文件”的问题,但这里是一个来自Spring官方文档的example,它布局了完整的 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>
    
  • 129

    在这里提出一个老问题,但是最近的Spring版本(v3.0)现在你可以完全摆脱web.xml,只要你在支持Servlet 3.0的web容器上部署你的应用程序 .

    可以实现Spring的WebApplicationInitializer接口来执行与web.xml中相同的配置 . 在Servlet 3.0容器上运行的Spring 3.0应用程序将自动检测此实现类 .

    如果设置相当简单,您可以使用Spring提供的另一个类,如下所示 . 这里所做的就是设置@Configuration类并列出servlet映射 . 保持设置非常简单 .

    public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer{
    
        @Override
        protected Class<?>[] getRootConfigClasses() {
            return null;
        }
    
        @Override
        protected Class<?>[] getServletConfigClasses() {
    
            return new Class[] {AppConfig.class};
        }
    
        @Override
        protected String[] getServletMappings() {
            return new String[] {
                     "*.html"
                    ,"*.json"
                    ,"*.do"};
        }
    }
    

相关问题