lishman levelup
«previous  next»


Getting Started
Form Processing
IoC Container
Hibernate ORM



Spring IoC Container Configuration


We can improve the overall structure of our application by creating a separate configuration file for each tier. So far, we have world-servlet.xml but, as the name suggests, this should only provide configuration details for the presentation layer.

Business Logic Configuration

A second file called applicationContext.xml is created in the WEB-INF directory, with the following contents:
<beans>

  <context:component-scan base-package="levelup.world.domain"/>

</beans>
The configuration is very simple because all we require of Spring is to locate our service components and make them available for autowiring.

web.xml

We tell Spring where to find the new configuration file by adding the following entries to web.xml.
<web-app>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

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

  ..

</web-app>
ContextLoaderListener loads the configuration file(s) specified in the contextConfigLocation context parameter into Spring's root context. DispatcherServlet then loads the servlet configuration into a separate context, with the root context as its parent. This means that the settings in the servlet context can refer to, and even override, the settings in the root context.

Tip
Tip - we introduce a configuration file for the data layer in Hibernate Object Relational Mapping with Spring MVC.
»