Spring Application Configuration XML based


This is a series and it would make a lot of sense if read in a sequence. You can find all the posts under the heading “Spring Beginners to Professionals“. At this point I believe it is appropriate to introduce Spring application configuration. In this post, we will also need the concepts learnt so far. I have made sure that you won’t need anything which is not discussed so far.

Writing a Spring Application Configuration

A set of one or more configuration metadata makes up a Spring configuration. I have described the configuration metadata in this post.  The configuration metadata can be an XML file or a Java class annotated with @Component or @Configuration annotations.

XML based Spring Application Configuration

A typical empty configuration metadata in XML looks like

The above sample clearly indicates that the configuration metadata is a holder of beans, that is why the root element is beans.  If you are using an IDE like Eclipse you will be able to see using content assist that, the beans schema allows you to add five types of element mentioned below:

  • description
  • import
  • bean
  • alias
  • beans

Out of the above five, the positions of description and beans element are fixed in the configuration metadata.  The description element if exists, has to be the first element in the file. It is used to provide a description for your application.

The import element is used to import another configuration metadata. It accepts a resource URL. The bean element is used to define one bean metadata, the alias element is used to provide an alias for an already defined bean name and the beans element is used to define another configuration metadata in this metadata, the only condition is that it should be the last element in this file.

So a configuration metadata with all these elements would look like below:

Bean Instantiations in XML

In the previous post, we learnt about three methods of instantiating a bean. Here we will see how is that performed in the XML.

Instantiating a bean using constructor

Here is how we add a bean metadata to the configuration metadata. We can do this by adding a bean element like  <bean class="in.techieme.client.OrderClient"> </bean>

If you notice the above definition, you can actually define a bean by just providing a fully qualified class name. We can also provide a lot more detail in this definition. For e.g.: a bean id or name which is a string and then you can always retrieve this bean with the name. In case you don’t specify a name, Spring will generate a name using its DefaultBeanNameGenerator (more on name generation strategies later)

It is a good practice to name your beans, it makes it easier to uniquely retrieve your bean from the container when required. Here, we can add the name orderClient to our bean.

Instantiating a bean using static factory method

In this case you need to define a java class which has static factory method to create the bean of the type we need.

Then you define the bean in the XML and specify the factory method as shown below:

This bean doesn’t create an instance of OrderClientFactory type, but produces OrderClient objects. You are free to define other static factory method which can return objects of type OrderClientFactory as well. The flexibility of using factories is that you get to process some custom logic before returning the bean from the factory method.

Instantiating a bean using instance factory method

Similar to static factory, you can also use instance methods as factories to create beans. The advantages remain the same as static factory method. The fundamental difference being that you cannot invoke the instance factory method until you have an instance of this factory class.

You need to create a factory class and declare an instance factory method as below:

And now we can define the factory bean first, so that spring can make an instance of this bean available to invoke the factory method.

And then we can create as many beans as required providing the factory bean and factory method as shown here:

Notice the difference between static factory and instance factory methods. In instance factory method, you need to provide a factory bean as well as a factory method, where as in static factory method, there is no need to create a factory bean because the factory method is static and can be invoked without the instance of a Factory class.


This is what a Spring XML configuration metadata contain. There can be lot many beans and we will definitely talk about them in the coming posts. In the next post, we will learn how to do the same thing using only annotations.

You can write Spring application configurations with XMLs, Annotations or a mix of both. Let us explore them in coming posts. Stay connected