Spring Book – Chapter 4 – Configuration Styles

XmlWebApplicationContext

Creation of application context using the class XmlWebApplicationContext is shown below:

Listing. Context creation using XmlWebApplicationContext class

The path is relative to the web application deployed in the server. Similar to ClassPathXmlApplicationContext and FileSystemXmlApplicationContext, XmlWebApplicationContext can be used to configure application context from multiple files as follows:

Listing. Context creation using XmlWebApplicationContext from multiple configuration files

In a web application this can also be easily achieved by declaring the configuration files as context parameter in the web applications web. xml web deployment descriptor. For loading the configuration files split into multiple files, we need to declare these in the web.xml as follows:

Listing. Declaring multiple configuration file in web.xml file of a web application

Best Practice: It is good to separate application beans from infrastructure beans. Also it’s good to segregate beans in different files rather than using one single bulky file containing whole lot of bean configuration.

Other ways of creating ApplicationContext

Bootstrapping ApplicationContext using files using various classes by Spring can also be achieved in an easy manner. We can mix the methods discussed in the preceding section to achieve the desired result in your application. Below are the two ways by which to achieve this in easy manner, one using prefixes and other by using wildcards.

Using prefixes

The various types explained above has the following prefixes attached that can be used to bootstrap the application context in your application:

  • ClassPathXmlApplicationContext          – classpath:
  • FileSystemXmlApplicationContext         – file:
  • XmlWebApplicationContext          – http:

The following example demonstrates the use of prefixes:

ApplicationContext context = new ClassPathXmlApplicationContext(“sample/app-context.xml”,”file:infrastructure-context.xml”);

Using wildcards

For an easy way of loading all the bean definition files, wildcards are supported by Spring. The below example shows this:

ApplicationContext context = new ClassPathXmlApplicationContext(“classpath*:sample/*-context.xml”);

The above bootstrapping of application context loads all the bean definition files inside the folder sample in the classpath ending with –context.xml.

Annotation-based configuration

Using only XML configurations can create delay in system development and maintenance due to huge and complicated configuration files. To solve this issue, Spring Framework came up with configuration using annotation. Spring 2.0 provided annotations for transaction management or persistence handling such as @Transactional, @Required, @PersistenceContext/@PersistenceUnit. From Spring 2.5, annotations directly related to Spring configuration xml are introduced such as bean or dependency definition. Spring 3 has started to provide JSR-330 (Dependency Injection for Java) Annotation, a standard annotation for dependency injection as well as Spring-specialized annotation.

Lifecycle

Figure 4-5. Annotation based configuration lifecycle

Spring container detects the bean using the @Component annotation declared in the class level. All the stereotype annotations like @Repository, @Service etc., which are explained in the below sections of this chapter in turn has @Component annotation. Once this is done the BeanFactoryPostProcessor’s defined in the application are called. Soon after this, the actual bean instantiation and dependency injection happens using the @Autowired annotation on the constructors and then injection of objects for all the fields and methods having @Autowired annotation. If BeanPostProcessor’s are defined, these are called soon after this.

Complete explanation of various annotations will be out of scope of this section and this book, Instead,  I focus on the various annotations that are most important the context of Spring Framework. Annotations can be divided into Bean Management, Dependency Injection and Life Cycle based on usage.

Page Visitors: 4529

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 *