5.2 Application Contexts

An ApplicationContext should be used instead of a BeanFactory unless there is a really strong need to have a slightly smaller memory footprint. An ApplicationContext is a subclass of the BeanFactory interface so it has all the features it has and more. The ApplicationContext also supports automatic BeanPostProcessor and BeanFactoryPostProcessor registration. MessageSource support for resolving i18n (internationalized) messages, ApplicationEventPublisher support for broadcasting application events, and ResourceLoader support for resolving resources.

All ApplicationContexts implement the ResourceLoader interface. To use an available ResourceLoader, a class loaded as a Spring bean can implement the ResourceLoaderAware interface. The ResourceLoaderAware has a method signature of setResourceLoader(ResourceLoader resourceLoader). If only one Resource needs to be injected, an ApplicationContext already has a PropertyEditor registered to convert a String into Resource.

An ApplicationContext implements the MessageSource interface. The MessageSource interface has methods for resolving locale sensitive messages. The ApplicationContext delegates calls to the MessageSource interface, but needs an instance to delegate to. The ApplicationContext will look for a bean with the specific id of messageSource. If it can't find one in the current context it will continue to look in parent contexts to try to find one. If a messageSource bean isn't found, a StaticMessageSource will be used as the delegate.

Example 5.1. Defining a MessageSource

                
<bean id="messageSource"
      class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>messages</value>
            <value>exceptions</value>
        </list>
    </property>
</bean>
                
            

As your XML files grow in size, it's good practice to break them into areas of functionality. The import element can be used to import other configuration files. For a Spring Web Flow web application it might be good to have a Spring MVC config, Spring Web Flow config, Data Access config, and a Security config.

Example 5.2. Importing XML Configuration Files

Four different configuration files are imported and only a reference to this configuration file is necessary to load all of them. The other configuration files in this case are all in the same directory as this one, but they could have different relative or absolute paths.

                
<?xml version="1.0" encoding="UTF-8"?>
<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">
    <import resource="webmvc-config.xml" />
    <import resource="webflow-config.xml" />
    <import resource="data-access-config.xml" />
    <import resource="security-config.xml" />

</beans>