Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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: No WebApplicationContext found: no ContextLoaderListener registered?

Now if I give listener class in web.xml,

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

it gives me error,

/WEB-INF/applicationContext.xml not found

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?

like image 538
Mital Pritmani Avatar asked Nov 10 '11 06:11

Mital Pritmani


People also ask

How do I load a spring context file in web xml?

Spring can be easily integrated into any Java-based web framework. All you need to do is to declare the ContextLoaderListener in your web. xml and use a contextConfigLocation to set which context files to load. You can then use the WebApplicationContext to get a handle on your beans.

Is applicationContext xml required?

Applicationcontext. xml - It is standard spring context file which contains all beans and the configuration that are common among all the servlets. It is optional file in case of web app.

What is required to load the beans configured in applicationContext xml file?

Loading Beans from XML Configuration While there are multiple ways of doing this, the recommended way is to create a seperate configuration class to load this xml bean definition file. The key part of the definition is @ImportResource({"classpath*:applicationContext. xml"}) . applicationContext.

What is contextConfigLocation in web xml?

The initialization parameter contextConfigLocation tells Spring where to load configuration files. The <load-on-startup> tag tells the servlet container to load this servlet upon start up with highest priority.


2 Answers

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> 

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

further reading:

  • Spring 3.1 MVC Enhancements
  • Spring 3.1 MVC Namespace Enhancements And Configuration

EDIT as a "comments follow up" => to be Turing Complete:

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> 
like image 178
tolitius Avatar answered Sep 24 '22 00:09

tolitius


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+.

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.

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"};     } } 
like image 31
Angad Avatar answered Sep 25 '22 00:09

Angad