Spring Configuration Spring Configuration xml xml

Spring Configuration


More importantly than where, the question should be for you: what exactly is this 'configuration data'?

From the docs:

the Spring IoC container consumes a form of configuration metadata; this configuration metadata represents how you as an application developer tell the Spring container to instantiate, configure, and assemble the objects in your application.

Configuration metadata is traditionally supplied in a simple and intuitive XML format, which is what most of this chapter uses to convey key concepts and features of the Spring IoC container.

However, you can also use annotations or Java-based configuration to provide the configuration metadata for your POJOs.

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

As Tnem already mentioned, here you can find how to instantiate a container in different scenarios.

And what is this IoC container?

IoC (inversion of control) and DI (dependency injection) are terms coined by Martin Fowler, regarding Spring see the first section of the docs.

I encourage you to read the whole reference if you want to get into development with Spring.


More interesting than the pure name is how do you split the files (and give each part a name).

If you have a Standalone or Webapplication with out tests, then you can put all configuration in one file. - But having no test should not be a opinion.

Lets assume you have a web application with tests.

Then you should split the configuration in two files, one for the pure java (without the web suff) configuration and one that contains all the other stuff for the WEB application.

I personaly perfer to name it applicationContext.xml and webmvc-config.xml.The default name for the web configuration file (if no specifed for the Dispatcher Servlet) would be /WEB-INF/<servletname>-servlet.xml)

I locate the applicationContext.xml in classpath:/META-INF/spring directory and the webmvc-config.xml in WEB-INF/spring. That location is the style of Spring Roo. It works, but every other folder would work too. Because I use maven the exact location of the files are:

  • /src/main/resources/META-INF/spring/applicationContext.xml
  • /src/main/webapp/WEB-INF/spring/webmvc-config.xml

The core applicationContext.xml is loaded with the org.springframework.web.context.ContextLoaderListener, and the webmvc-config.xml by the Dispatacher Servlet. web.xml:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath*:META-INF/spring/applicationContext*.xml</param-value></context-param><!-- Creates the Spring Container shared by all Servlets and Filters --><listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><servlet>    <servlet-name>CFMA-SpringProject</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>        <param-name>contextConfigLocation</param-name>        <param-value>/WEB-INF/spring/webmvc-config.xml</param-value>    </init-param>    <load-on-startup>1</load-on-startup></servlet>

Now you start to write your tests, for the business logic of your application, without loading all the web stuff. But in most cases that is not enougth. For example you want to run some fast tests with an Inmemory Database while you run the normal Application with an persistent Database like MySql (please don't blame me for that sentence), or you want to use a jndi configured db in production and a "normal" configured one for tests. So what you need is two different configuration. To write not every thing twice, the easiest way is to split the applicationContext.xml in two files:

  • applicationContext.xml for the core stuff without the db stuff that differs to the tests
  • applicationContext-db.xml for the productive db configuration (for example jndi-lookup for db connection and LocalContainerEntityManagerFactoryBean for MySql)

(Now you understand the pattern of contextConfigLocation in the web.xml)

For the Tests you need now two files (you can write it in one file, but I prefer two). * testContext-h2DbConfig.xml The file that is the testing sibling of applicationContext-db.xml, but with the test database and without jndi. * textContext.xml This file in the one referenced by @ContextConfiguration in you test cases. This file contains only the imports of the configuration you need for the tests. In this case it is:

<import resource="classpath:/META-INF/spring/applicationContext.xml" /> <import resource="classpath:/META-INF/spring/testContext-h2DbConfig.xml" />

Because I use spring, both files are located in /src/test/resources/META-INF/spring/testContext.xml

If you have other aspects of your spring configuration, where the test and the productive configuration differs (for example schedulers), then you can split it in the same way.

I hope you understand how splitting, naming convention and location work together.


Well, if there is an "official" name at all, then I guess it should come from the Spring guys themselves. And Spring's tool of choice to set up conformant Spring projects is Roo, looking there we find SRC_MAIN_RESOURCES/META-INF/spring/applicationContext.xml (with SRC_MAIN_RESOURCES being a placehiolder for src/main/resources in a Maven2 project).