Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

What Is an Application Context?

Inside a Spring application are beans, and these beans interact with each other to provide the application services. The interacting beans are also called collaborators, and you can define them using application contexts. This is the process of "wiring" your beans together. Listing 2 shows an example of an application context.

Listing 2 Application context for the tutorial service.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="actionDAOImpl" class="com.mycompany.hr.service.ActionDAOImpl">

In Listing 2, I define a new bean called actionDAOImpl, which is backed by the following class:


What's the purpose of the application context? It provides a place in which you can define one or more beans for use in your application. Listing 3 illustrates the use of the actionDAOImpl bean defined in the application context.

Listing 3 Accessing the application context.

public class StubHumanResourceService implements HumanResourceService {

private static final Log logger = LogFactory.getLog(StubHumanResourceService.class);

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

public void bookHoliday(Date startDate, Date endDate, String name) {
// An ApplicationContext is just a BeanFactory
BeanFactory factory = context;

ActionDAOImpl actionDAOImpl = (ActionDAOImpl)factory.getBean("actionDAOImpl");


logger.info("Booking my next holiday for [" + startDate + "-" + endDate + "] name [" + name + "] ");

The StubHumanResourceService class is the implementation of the interface called HumanResourceService. Listing 3 is the endpoint of the web service. What's going on inside the code? Let's take it apart.

Notice the instantiation of the object context. This is the application context where the collaborators are defined and wired together. Once the context is loaded, I have access to all the beans defined inside it. In effect, my beans have been injected into the container!

It's worth thinking a little about this container-injection mechanism. The collaborators are defined using XML in the application context file. This file is referred to as metadata; that is, it goes together with the Java code to form what becomes the executable entity. But bear in mind that the notion of defining Java code inside an XML file is very powerful. This is part of the reason why this mechanism is called inversion of control. By defining code elements outside of your Java files, you are truly inverting control. Once you grasp this concept, you're well on your way to gaining a strong understanding of Spring.

After the context is loaded, the bookHoliday method instantiates an object of BeanFactory. Using the latter, we can access a fully configured instance of our new bean, ActionDAOImpl. After this, we can invoke the methods of the bean; in other words, we can call the placeholder method actionDAOImpl.doSomething().

  • + Share This
  • 🔖 Save To Your Account