Creation of application context using the class XmlWebApplicationContext is shown below:
Listing. Context creation using XmlWebApplicationContext class
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
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
<?xml version="1.0" encoding="UTF-8"?>
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.
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”);
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.
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.
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: 3549