Datasheet
BeanFactory factory = (BeanFactory) context;
CalculateSpring calc =
(CalculateSpring) factory.getBean(“opsbean”);
calc.execute(args);
}
public void execute(String [] args) {
long op1 = Long.parseLong(args[0]);
long op2 = Long.parseLong(args[1]);
wtr.showResult(“The result of “ + op1 +
ops.getOpsName() + op2 + “ is “
+ ops.operate(op1, op2) + “!”);
}
}
The preceding highlighted code creates an ApplicationContext. This context is created and provided
by the Spring container. In this case, the actual implementation of
ApplicationContext is called
ClassPathXmlApplicationContext. The beans.xml descriptor is supplied as a constructor argu-
ment. Spring’s
ClassPathXmlApplicationContext looks for the instructions for wiring the beans
together in the
beans.xml file, which can be found in the classpath.
ApplicationContext is a BeanFactory
An ApplicationContext in Spring is a type of BeanFactory. A BeanFactory enables you to access
JavaBeans (classes) that are instantiated, wired, and managed by the Spring container.
Although there are other
BeanFactory library classes in Spring, the ApplicationContext class is the
most frequently used one because it provides a lot of valuable extra features — including support for
internationalization, resource loading, integration with external context hierarchies, events publishing,
and much more.
Providing the Spring Container with Wiring Instructions
The ClassPathXmlApplicationContext constructor takes as an argument the context descriptor file
or the bean’s wiring file. This file is named
beans.xml in the example case presented here, but you can
use any name you want as long as it has the
.xml extension, as it is an XML file. This beans.xml file is
the configuration file describing how to wire together objects. The
beans.xml file is shown here.
The BeanFactory classes are examples of the factory method design pattern. This
design pattern enables a framework to provide a means for creating objects with-
out knowing ahead of time the type of object that will be created. For example,
the
BeanFactory in the preceding example is used to create an instance of
CalculateSpring, a class that BeanFactory has no knowledge of.
11
Chapter 1: Jump Start Spring 2
01612c01.qxd:WroxPro 10/31/07 10:42 AM Page 11










