XmlWebApplicationContext
Creation of application context using the class XmlWebApplicationContext is shown below:
Listing. Context creation using XmlWebApplicationContext class
1 |
ApplicationContext context = new XmlWebApplicationContext("/WEB-INF/application-context.xml"); |
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
1 |
ApplicationContext context = new XmlWebApplicationContext("/WEB-INF/application-context.xml","/WEB-INF/infrastructure-context.xml"); |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5"> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/application-context.xml, /WEB-INF/infrastructure-context.xml</param-value> </context-param> </web-app> |
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: 4349

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
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.
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