Rectangle 27 0

java How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml?


<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>
<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>
@EnableWebMvc
AnnotationConfigWebApplicationContext
web.xml

Hi, I am not using Spring MVC here, only JSF and Spring 3.1 RC1. Though I wrote this servlet in my web.xml but still got same error "no ContextLoaderListener registered". I then did entry for listener class of spring and then got other error of applicationContext.xml not found. I hope I am making my point clear.

I never received you edit request, but I updated my answer with an example of a full web.xml form the Spring official documentation.

In my web application config i had to have <load-on-startup>1</load-on-startup> in dispatcher servlet. Without this dispatcher have not be loaded

Note
Rectangle 27 0

java How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml?


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"};
    }
}

Bumping up an old question here, but with recent versions of Spring (v3.0+) now you can get rid of web.xml altogether, provided you are deploying your app on a web container that supports Servlet 3.0+.

If you do try to get rid of web.xml be sure you're using a container that supports Servlet 3.0 like Tomcat 7.0.57. P.S. Steve, nice work with the leading commas!

One can implement Spring's WebApplicationInitializer interface to do the same configurations that one would do in web.xml. This implementation class will be automatically detected by Spring 3.0+ app running on Servlet 3.0+ containers. If the set up is rather simple, you could instead use another class provided by Spring as shown below. All one does here is to set the @Configuration classes and list out the servlet mappings. Keeps the setup extremely simple.

That's true. I'll throw that in the answer. Thanks

Note
Rectangle 27 0

java How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml?


<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>
<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>
@EnableWebMvc
AnnotationConfigWebApplicationContext
web.xml

Hi, I am not using Spring MVC here, only JSF and Spring 3.1 RC1. Though I wrote this servlet in my web.xml but still got same error "no ContextLoaderListener registered". I then did entry for listener class of spring and then got other error of applicationContext.xml not found. I hope I am making my point clear.

I never received you edit request, but I updated my answer with an example of a full web.xml form the Spring official documentation.

In my web application config i had to have <load-on-startup>1</load-on-startup> in dispatcher servlet. Without this dispatcher have not be loaded

Note
Rectangle 27 0

java How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml?


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"};
    }
}

Bumping up an old question here, but with recent versions of Spring (v3.0+) now you can get rid of web.xml altogether, provided you are deploying your app on a web container that supports Servlet 3.0+.

If you do try to get rid of web.xml be sure you're using a container that supports Servlet 3.0 like Tomcat 7.0.57. P.S. Steve, nice work with the leading commas!

One can implement Spring's WebApplicationInitializer interface to do the same configurations that one would do in web.xml. This implementation class will be automatically detected by Spring 3.0+ app running on Servlet 3.0+ containers. If the set up is rather simple, you could instead use another class provided by Spring as shown below. All one does here is to set the @Configuration classes and list out the servlet mappings. Keeps the setup extremely simple.

That's true. I'll throw that in the answer. Thanks

Note