title: NoSuchBeanDefinitionException in Spring Application
body: <p>I am creating an application where I am using REST. Currently my Controller is being implemented using a <code>ServiceMockImplementation</code>, but I intend to use a database (rather than hard coding the values of my Contacts). The restful client works fine (and runs on the server) when used by the mock implementation but the moment I add my DAO implementation, it crashes and gives me a <code>NoSuchBeanDefinitionException</code> even though the bean "contactDao" is annotated as <code>@Repository("contactDao")</code> in my <code>DAOImplementation</code> class.</p>
  
  <p>Here is my <code>RestfulClientMain</code>:</p>
  
  <pre><code>public class RestfulClientMain {
  
      private static final String URL_GET_ALL_CONTACTS = "http://localhost:8080/Contact/contacts";
      private static final String URL_GET_CONTACT_BY_ID = "http://localhost:8080/Contact/contacts/{id}";
      private static final String URL_CREATE_CONTACT = "http://localhost:8080/Contact/contacts/";
      private static final String URL_UPDATE_CONTACT = "http://localhost:8080/Contact/contacts/{id}";
      private static final String URL_DELETE_CONTACT = "http://localhost:8080/Contact/contacts/{id}"; 
  
      public static void main(String[] args) {
  
          GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
          ctx.load("classpath:restful-client-app-context.xml");
          ctx.refresh();
  
          Contacts contacts;
          Contact contact;
          RestTemplate restTemplate = ctx.getBean("restTemplate", RestTemplate.class);
  
          // Test retrieve all contacts
          System.out.println("Testing retrieve all contacts:");
          contacts = restTemplate.getForObject(URL_GET_ALL_CONTACTS, Contacts.class);
          listContacts(contacts); 
  
          // Test retrieve contact by id
          System.out.println("Testing retrieve a contact by id :");
          contact = restTemplate.getForObject(URL_GET_CONTACT_BY_ID, Contact.class, 1);
          System.out.println(contact);
          System.out.println("");
  
          // Test update contact
          contact = restTemplate.getForObject(URL_UPDATE_CONTACT, Contact.class, 1);
          contact.setFirstName("Kim Fung");
          System.out.println("Testing update contact by id :");
          restTemplate.put(URL_UPDATE_CONTACT, contact, 1);
          System.out.println("Contact update successfully: " + contact);
          System.out.println(""); 
  
          // Testing delete contact
          restTemplate.delete(URL_DELETE_CONTACT, 1);
          System.out.println("Testing delete contact by id :");
          contacts = restTemplate.getForObject(URL_GET_ALL_CONTACTS, Contacts.class);
          listContacts(contacts);
  
          // Testing create contact
          System.out.println("Testing create contact :");
          Contact contactNew = new Contact();
          contactNew.setFirstName("JJ");
          contactNew.setLastName("Gosling");
          contactNew.setBirthDate(new Date());
          contactNew = restTemplate.postForObject(URL_CREATE_CONTACT, contactNew, Contact.class);
          System.out.println("Contact created successfully: " + contactNew);       
  
      }
  
      private static void listContacts(Contacts contacts) {
          for (Contact contact: contacts.getContacts()) {
              System.out.println(contact);
          }   
          System.out.println("");
      }
  
  }
  </code></pre>
  
  <p>Here is my <code>ContactController</code>:</p>
  
  <pre><code>import javax.servlet.http.HttpServletResponse;
  import org.slf4j.*;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.*;
  import org.springframework.web.context.request.WebRequest;
  import com.aucklanduni.spring.webservices.domain.*;
  import com.aucklanduni.spring.webservices.service.ContactService;
  
  
  @Controller
  @RequestMapping(value="/contacts")
  public class ContactController {
  
      final Logger logger = LoggerFactory.getLogger(ContactController.class);
  
  
      @Autowired
      private ContactService contactService;
  
      @RequestMapping(method = RequestMethod.GET)
      @ResponseBody
      public Contacts listData(WebRequest webRequest) {
          return new Contacts(contactService.findAll());
      }   
  
      @RequestMapping(value="/{id}", method=RequestMethod.GET)
      @ResponseBody
      public Contact findContactById(@PathVariable Long id) {     
          return contactService.findById(id);
      }
  
      @RequestMapping(value="/", method=RequestMethod.POST)
      @ResponseBody
      public Contact create(@RequestBody Contact contact, HttpServletResponse response) {
          logger.info("Creating contact: " + contact);
          contactService.save(contact);
          logger.info("Contact created successfully with info: " + contact);
          response.setHeader("Location",  "/contacts/" + contact.getId());
          return contact;
      }
  
      @RequestMapping(value="/{id}", method=RequestMethod.PUT)
      @ResponseBody
      public void update(@RequestBody Contact contact, @PathVariable Long id) {
          logger.info("Updating contact: " + contact);
          contactService.save(contact);
          logger.info("Contact updated successfully with info: " + contact);
          //return contact;
      }   
  
      @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
      @ResponseBody
      public void delete(@PathVariable Long id) {
          logger.info("Deleting contact with id: " + id);
          Contact contact = contactService.findById(id);
          contactService.delete(contact);
          logger.info("Contact deleted successfully");
      }   
  
  }
  </code></pre>
  
  <p>Here is my ContactMockImplementation. Currently you can see it includes hardcoded values but as comments I have included what the code should actually be doing once this error is resolved. That is interacting with DAO (to retrieve the Contacts from the Database).</p>
  
  <pre><code>import java.util.*;
  import org.joda.time.DateTime;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.*;
  import com.aucklanduni.spring.webservices.domain.*;
  
  @Service("contactService")
  @Component
  public class ContactServiceMockImplementation implements ContactService {
  
      private Contacts _contacts;
  
      //private ContactDao contactDao;
  
      public ContactServiceMockImplementation() {
          Contact contact1 = new Contact();
          contact1.setId(1L);
          contact1.setVersion(1);
          contact1.setFirstName("Clint");
          contact1.setLastName("Eastwood");
          contact1.setBirthDate(new Date());
  
          Contact contact2 = new Contact();
          contact2.setId(1L);
          contact2.setVersion(1);
          contact2.setFirstName("Robert");
          contact2.setLastName("Redford");
          contact2.setBirthDate(new Date());
  
          Contact contact3 = new Contact();
          contact3.setId(1L);
          contact3.setVersion(1);
          contact3.setFirstName("Michael");
          contact3.setLastName("Caine");
          contact3.setBirthDate(new Date());
  
          List&lt;Contact&gt; contactList = new ArrayList&lt;Contact&gt;();
          contactList.add(contact1);
          contactList.add(contact2);
          contactList.add(contact3);
  
          _contacts = new Contacts(contactList);
      }
  
      @Override
      public List&lt;Contact&gt; findAll() {
          return _contacts.getContacts();
          //return contactDao.findAll();
      }
  
  //  @Override
  //  public List&lt;Contact&gt; findByFirstName(String firstName) {
  //      List&lt;Contact&gt; results = new ArrayList&lt;Contact&gt;();
  //      
  //      for(Contact contact : _contacts.getContacts()) {
  //          if(contact.getFirstName().equals(firstName)) {
  //              results.add(contact);
  //          }
  //      }
  //      return results;
  //  }
  
      @Override
      public Contact findById(Long id) {
          Contact result = null;
  
          for(Contact contact : _contacts.getContacts()) {
              if(contact.getId() == id) {
                  result = contact;
                  break;
              }
          }
          return result;
          //return contactDao.findById(id);
      }
  
      @Override
      public Contact save(Contact contact) {
          return contact;
          //return contactDao.save(contact);
      }
  
      @Override
      public void delete(Contact contact) {
          // TODO Auto-generated method stub
          //contactDao.delete(contact);
      }
  
  }
  </code></pre>
  
  <p>This is my <code>ContactDao</code>- which is what is giving me an error.</p>
  
  <pre><code>import java.util.List;
  import javax.annotation.Resource;
  import org.hibernate.SessionFactory;
  import org.slf4j.*;
  import org.springframework.stereotype.*;
  import org.springframework.transaction.annotation.Transactional;
  import com.aucklanduni.spring.webservices.domain.Contact;
  
  @Repository("contactDao")
  @Component
  @Transactional
  public class ContactDaoImpl implements ContactDao {
  
      private Logger _logger = LoggerFactory.getLogger(ContactDaoImpl.class);
  
      private SessionFactory _sessionFactory; 
  
      public SessionFactory getSessionFactory() {
          return _sessionFactory;
      }
  
      @Resource
      public void setSessionFactory(SessionFactory sessionFactory) {
          this._sessionFactory = sessionFactory;
          _logger.debug("SessionFactory class: " + sessionFactory.getClass().getName());
      }
  
      @Transactional(readOnly=true)
      public List&lt;Contact&gt; findAll() {
          List&lt;Contact&gt; result = _sessionFactory.getCurrentSession().createQuery("from Contact c").list();
          return result;
      }
  
      public List&lt;Contact&gt; findAllWithDetail() {
          return _sessionFactory.getCurrentSession().getNamedQuery("Contact.findAllWithDetail").list();
      }
  
      public Contact findById(Long id) {
          return (Contact) _sessionFactory.getCurrentSession().
                  getNamedQuery("Contact.findById").setParameter("id", id).uniqueResult();
      }
  
      public Contact save(Contact contact) {
          _sessionFactory.getCurrentSession().saveOrUpdate(contact);
          _logger.info("Contact saved with id: " + contact.getId());
          return contact;
      }
  
      public void delete(Contact contact) {
          _sessionFactory.getCurrentSession().delete(contact);
          _logger.info("Contact deleted with id: " + contact.getId());
      }
  
  }
  </code></pre>
  
  <p>When starting the Server, this is the error that I get:</p>
  
  <pre class="lang-none prettyprint-override"><code>ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDao': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as 
  </code></pre>
  
  <p>I am currently stuck as I am not too sure how I will be able to link my ContactService with my ContactDao (rather than use the MockImplementation methods). Any help will be much appreciated. Thanks!</p>
  
  <p>Here is my restful-client-app-context.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
      http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"&gt;
  
      &lt;jdbc:embedded-database id="dataSource" type="H2"&gt;
          &lt;jdbc:script location="classpath:schema.sql"/&gt;
          &lt;jdbc:script location="classpath:test-data.sql"/&gt;
      &lt;/jdbc:embedded-database&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
          &lt;property name="sessionFactory" ref="springSessionFactory"/&gt;
      &lt;/bean&gt;  
  
      &lt;!-- &lt;bean id="contactDao" class="com.aucklanduni.spring.webservices.service.ContactDao"&gt;&lt;/bean&gt; --&gt;
  
      &lt;tx:annotation-driven/&gt;
  
      &lt;context:component-scan base-package="com.aucklanduni.spring.webservices" /&gt;
  
      &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /&gt;
  
      &lt;bean id="springSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
          &lt;property name="dataSource" ref="dataSource"/&gt;
          &lt;property name="packagesToScan" value="com.aucklanduni.spring.webservices.domain"/&gt;
          &lt;property name="hibernateProperties"&gt;
              &lt;props&gt;
                  &lt;prop key="hibernate.dialect"&gt;org.hibernate.dialect.H2Dialect&lt;/prop&gt;
                  &lt;prop key="hibernate.max_fetch_depth"&gt;3&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.fetch_size"&gt;50&lt;/prop&gt;
                  &lt;prop key="hibernate.jdbc.batch_size"&gt;10&lt;/prop&gt;
                  &lt;prop key="hibernate.show_sql"&gt;true&lt;/prop&gt;
              &lt;/props&gt;
          &lt;/property&gt;        
      &lt;/bean&gt;
      &lt;bean id="restTemplate" class="org.springframework.web.client.RestTemplate"&gt;
          &lt;!--  &lt;constructor-arg ref="httpRequestFactory"/&gt;--&gt;
          &lt;property name="messageConverters"&gt;
              &lt;list&gt;
                  &lt;bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"&gt;
                      &lt;property name="marshaller" ref="castorMarshaller"/&gt;
                      &lt;property name="unmarshaller" ref="castorMarshaller"/&gt;
                      &lt;property name="supportedMediaTypes"&gt;
                          &lt;list&gt;
                              &lt;bean class="org.springframework.http.MediaType"&gt;
                                  &lt;constructor-arg index="0" value="application"/&gt;
                                  &lt;constructor-arg index="1" value="xml"/&gt;
                              &lt;/bean&gt;
                          &lt;/list&gt;
                      &lt;/property&gt;
                  &lt;/bean&gt;                
              &lt;/list&gt;
          &lt;/property&gt;    
      &lt;/bean&gt;
  
      &lt;bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"&gt;
          &lt;property name="mappingLocation" value="classpath:oxm-mapping.xml"/&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>Latest Log Error:</p>
  
  <pre><code>[mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,contactController,contactDao,contactService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,castorMarshaller,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@50086ff0
  INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts/{id}],methods=[DELETE],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.aucklanduni.spring.webservices.restful.controller.ContactController.delete(java.lang.Long)
  INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts/],methods=[POST],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.aucklanduni.spring.webservices.domain.Contact com.aucklanduni.spring.webservices.restful.controller.ContactController.create(com.aucklanduni.spring.webservices.domain.Contact,javax.servlet.http.HttpServletResponse)
  INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts/{id}],methods=[PUT],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public void com.aucklanduni.spring.webservices.restful.controller.ContactController.update(com.aucklanduni.spring.webservices.domain.Contact,java.lang.Long)
  INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.aucklanduni.spring.webservices.domain.Contacts com.aucklanduni.spring.webservices.restful.controller.ContactController.listData(org.springframework.web.context.request.WebRequest)
  INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contacts/{id}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public com.aucklanduni.spring.webservices.domain.Contact com.aucklanduni.spring.webservices.restful.controller.ContactController.findContactById(java.lang.Long)
  INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1201f5bc: defining beans [mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,contactController,contactDao,contactService,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,castorMarshaller,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@50086ff0
  ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.aucklanduni.spring.webservices.service.ContactDaoImpl.setSessionFactory(org.hibernate.SessionFactory); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
      at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:633)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
      at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:651)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:602)
      at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:665)
      at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:521)
      at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:462)
      at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
      at javax.servlet.GenericServlet.init(GenericServlet.java:158)
      at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
      at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1197)
      at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
      at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5210)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5493)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
      at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
      at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
      at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:670)
      at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1839)
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
      at java.util.concurrent.FutureTask.run(FutureTask.java:262)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
  </code></pre>
  
  <p>Here is my web.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
      &lt;!-- The definition of the Root Spring Container shared by all Servlets and Filters --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/app/root-context.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;!-- Configure the Web container to use Spring's DispatcherServlet. DispatcherServlet will 
           forward incoming requests and direct them to application controllers. The Spring container 
           that is used by the The DisplatcherServlet is initialised by the XML file specified by
           the "contextConfiguration" initialisation parameter (i.e. "restful-context.xml"). --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;contactsService&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;/WEB-INF/app/restful-context.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;!-- Ensure that the servlet executes incoming requests that match the pattern "/*". This means
           that if the application is hosted within a Web container at path "Contact", and if the 
           Web container's domain name is "http://localhost:808", then the servlet will process any
           requests of the form: http://localhost:8080/Contact/, 
           e.g http://localhost:8080/Contact/contacts.   --&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;contactsService&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>restful-context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt;
  
      &lt;mvc:annotation-driven&gt;
          &lt;mvc:message-converters&gt;
              &lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/&gt;
              &lt;bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter"&gt;
                  &lt;property name="marshaller" ref="castorMarshaller"/&gt;
                  &lt;property name="unmarshaller" ref="castorMarshaller"/&gt;
              &lt;/bean&gt;
          &lt;/mvc:message-converters&gt;
      &lt;/mvc:annotation-driven&gt;
  
      &lt;context:component-scan base-package="com.aucklanduni.spring.webservices.restful.controller,
          com.aucklanduni.spring.webservices.service" /&gt;
  
      &lt;bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"&gt;
          &lt;property name="mappingLocation" value="classpath:oxm-mapping.xml"/&gt;
      &lt;/bean&gt;     
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>root-context.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"&gt;
  
      &lt;!-- Root Context: defines shared resources visible to all other web components --&gt;
  
  &lt;/beans&gt;
  </code></pre>