Using The Application Context In Spring

This article shows how to use the Spring Framework in it’s most basic usage. We use the ApplicationContext.xml file to define some Java beans so that we can retrieve them in the application. In this way we can reimplement the classes and use alternatively different implementations by just changing the configuration in the ApplicationContext.xml file.

Contents

  • 1 Why is this useful?
  • 2 the BatBotResourceFactory Class
  • 3 How to get the singleton
  • 4 Initializing the Application Context
  • 5 obtaining the Beans
  • 6 the Full code for the class
  • 7 the application context file

Why is this useful?

Imagine this situation: you want to develop a program of a decent size. This program is made of different components, which are independent and collaborate to the processing overall.

You can’t develop all the components at the same time, but once you finish the first component you’re going to need some dummy behaviour from the other components to test it.

What you can do is to create dummy implementations of the other components and wire them using the dependency injection from the Spring Framework. These dummy components will behave pretty much as the component you implemented expects, for example a DAO could return always the same hard coded object. So you can test the component in a pretty much realistic context.

Once you’re done with the component, you can start working on the second one and wire it in theapplicationContext.xml file. So that to test it you can just switch whatever implementation, without even recompiling the project.

Another situation where you can smoothly use Spring beans is when you have an application which has configurable components. You can deploy the different components by just wiring them in the application context.

For example you may have a software which supports the mySql Database, but you want to deploy it on a system with Oracle. You can simply reimplement the interface creating new DAO’s that support Oracle, then just wire them in the application context instead of the MySql DAO’s. If you need to switch back to MySql, just rewire the old DAO’s and you’re done. You don’t even need to recompile.

the BatBotResourceFactory Class

For this application I created a Factory class which is also a Singleton calledBatBotResourceFactory .

I did this to have a unique place to access the Spring beans, since I consider the beans provided by spring like external resources I don’t want my software to be too much dependent on the framework. In the whole program there are no links to Spring, so with this architecture I can easily completely remove the spring framework from my application by just reimplementing theBatBotResourceFactory.

How to get the singleton

The Singleton is simply implemented in the getSingleton method, which is declared public andstatic:

    private static BatBotResourceFactory batBotResourceFactory;
    public synchronized static BatBotResourceFactory getSingleton() {
	if (null == batBotResourceFactory) {
	    batBotResourceFactory = new BatBotResourceFactory();
	}
	return batBotResourceFactory;
    }

    private BatBotResourceFactory() {
	context = new FileSystemXmlApplicationContext(APPLICATION_CONTEXT_FILE);
    }

Note that the constructor for the class is declared private, this is a fundamental requirement for the singleton Class. Note also that the getSingleton method is synchronized.

This is the most normal lazy Singleton in the world.

Initializing the Application Context

Before using the application context we need to instantiate it via theorg.springframework.context.support.FileSystemXmlApplicationContext class from the Spring framework. This is an expensive process that involves to open and parse theapplicationContext.xml file.

This expensive operation is the reason why I created the factory method and made is as a Singleton.

 private ApplicationContext context;

 private static final String APPLICATION_CONTEXT_FILE = "applicationContext.xml";

  private BatBotResourceFactory() {
	context = new FileSystemXmlApplicationContext(APPLICATION_CONTEXT_FILE);
    }

obtaining the Beans

Once the org.springframework.context.ApplicationContext is created and saved in thecontext field. it can be used to retrieve the beans from the Spring framework. At this point it’s very simple:

All you need to do is to call the getBean Method

Object org.springframework.beans.factory.BeanFactory.getBean(String name) throws BeansException

The method is used as follows:

 public FileHelperI getFileHelper() {
	return (FileHelperI) context.getBean("fileHelperBean");
    }

the Full code for the class

package org.wikijava.network.batBot.delegates;

import java.util.logging.Logger;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.wikijava.network.batBot.delegates.interfaces.ActionExecutorI;
import org.wikijava.network.batBot.delegates.interfaces.FileHelperI;
import org.wikijava.network.batBot.delegates.interfaces.HttpHelperI;

public class BatBotResourceFactory {

    private transient static Logger log = Logger
	    .getLogger(BatBotResourceFactory.class.getName());

    private static final String APPLICATION_CONTEXT_FILE = "applicationContext.xml";

    ApplicationContext context;

    private static BatBotResourceFactory batBotResourceFactory;

    public static BatBotResourceFactory getSingleton() {
	if (null == batBotResourceFactory) {
	    batBotResourceFactory = new BatBotResourceFactory();
	}
	return batBotResourceFactory;
    }

    private BatBotResourceFactory() {
	context = new FileSystemXmlApplicationContext(APPLICATION_CONTEXT_FILE);
    }

    public FileHelperI getFileHelper() {
	return (FileHelperI) context.getBean("fileHelperBean");
    }

    public HttpHelperI getHttpHelper() {
	return (HttpHelperI) context.getBean("httpHelperBean");
    }

    public ActionExecutorI getActionExecutorI() {
	return (ActionExecutorI) context.getBean("ActionExecutorBean");
    }}

the application context file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
          "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
	<bean id="fileHelperBean" class="org.wikijava.network.batBot.delegates.XStreamFileHelper">
	</bean>
	<bean id="httpHelperBean" class="org.wikijava.network.batBot.delegates.HttpHelper">
	</bean>
	<bean id="ActionExecutorBean" class="org.wikijava.network.batBot.delegates.ActionExecutor">
	</bean>
</beans>

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.