Spring Book – Chapter 4 – Configuration Styles

When Should You Use XML?

Use XML for more static beans which rarely changes. I would suggest using this to configure infrastructure beans because rarely these beans are changed programmatically after Spring container loads it from the XML configuration.

If you want single place to edit your entire application configuration, this is apt style.

Since XML is known to most of the developers nowadays, it can be understood and leveraged easily by everyone in the team.

This style can be used for configuring third-party and your own classes with ease.

Even with so many options of bean configuring, it still lacks more configuration options.

If you are a person who hates XML, this style is not for you.

When Should You Use Annotation?

I would suggest using annotations to configure beans which frequently changes. I wouldn’t say to use only annotations, as it could be that your application configurations are spread across your entire application source code. This means that you wouldn’t have a single place to look into the configuration as a whole. It’s good to isolate certain parts of your application and make it as a candidate for this style of configuration taking into consideration its pros and cons.

This style can be used for rapid development as it requires fewer configurations.

Disadvantage associated to it is that the configuration is spread across the entire code base of your application.

The biggest drawback is that, this style cannot be used for third-party classes. Only your own classes can be used.

When Should You Use Java?

If you want full control over bean instantiation and its configuration, this style of configuration is apt for your application.

If you want the configuration still outside of your main business logic classes, and also have full control over your beans instantiation, this is good style for your application.

It integrates well with your legacy code and third-party classes.

At the time of writing this book, there is no support for showing wiring of objects in STS using this style.

Mixing Configuration Styles

You have the ability to mix and match various configuration styles in your application. According to the various advantages and disadvantage discussed in the previous section, you can decide on the style required in your application.

Listing. Example of mixing configuration style

Declare your infrastructure beans in XML as below:

Use the declared infrastructure bean in your java configuration as below:

Summary

Spring’s configuration can be written in XML, annotations and in Java. With Spring’s 3.1 version, you even have a provision of doing your complete configuration in Java and can avoid XML completely from your application.

You can mix and match various configuration styles according to your application requirement.

You now have an understanding of the lifecycle of each of the configuration styles in detail along with appropriate pictorial representation.

You have learned about various XML configurations that you can use to configure various objects in your application.

If you want to use annotation style, this chapter provides explanation of various Spring annotations and standard Java annotations along with examples. Since component scanning is very important in annotation driven configuration style, I have explained this in detail with examples as required.

Finally, I’ve listed the best-practice configuration styles to help you decide on the correct configuration requirements for your application.

Page Visitors: 4528

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 4 – Configuration Styles

  1. Hi there,

    There is one mistake repeated several times here. The value of the propertyInteger is being set using “ref” instead of “value” and you may want to remove the spaces before and after some of the values/properties to avoid others new to the framework copying and pasting and getting tripped up.

    Apart from that, thanks for making the material available for free.

    1. Sure will do the necessary changes. I wrote the material using MS Word and after that when i copied, these spaces automatically came in.

      Thanks for pointing it out.

      Regards
      Tomcy John

Leave a Reply

Your email address will not be published. Required fields are marked *