Home > Articles > Programming > Java

  • Print
  • + Share This
From the author of

Unzipping and Building the Example Application

Unzip the application to a folder, such as C:\springapp. I'll assume that you've already opened a DOS console to set up the CLASSPATH. So, if you change directories to the folder where you installed the example application, there should be a file called build.xml in that folder. Run the build command:


The above command should create a new folder called build.

Change directories to the build folder and run this command:

java HelloWorldSpring

If your configuration setup is correct, you should see program output like that in Listing 2.

Listing 2 The Spring application program output.

Building the application beans
May 24, 2008 3:59:51 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\springapp\build\beans.xml]
This is a configurable message
Provider message This is a configurable message

The simplicity of the program output belies the complexity of what's happening behind the scenes. Let's look at the structure of this program. Listing 3 illustrates the main program.

Listing 3 The main program.

public class HelloWorldSpring {
  public static void main(String[] args) throws Exception {
    System.out.println("Building the application beans");
    BeanFactory factory = getBeanFactory();
    View modelRenderer = (View) factory.getBean("renderer");
    HelloWorldModel provider = (HelloWorldModel) factory.getBean("provider");
    System.out.println("Provider message " + provider.getMessage());
  private static BeanFactory getBeanFactory() throws Exception {
    BeanFactory factory = new XmlBeanFactory(new FileSystemResource(
    return factory;

The first thing to notice is the small size of the program—just a few lines to render a view object in two different ways! The use of a BeanFactory object allows the code to manage any type of bean. Remember that the Spring Framework creates beans for use by your application. You specify the beans you want and their interrelationships, and the Spring Framework creates those beans for you. This arrangement helps you to avoid much of the grunt work of wiring together application objects. The central idea of Spring is that the framework frees you to focus on your business logic, leaving the infrastructural issues to the container.

In web applications, we tend to use Spring application contexts rather than the BeanFactory. However, both approaches are entirely viable. The instance of BeanFactory is used to build the required beans. How does this work? Using the accompanying file beans.xml, as shown in Listing 4.

Listing 4 The beans.xml metadata file.

  <bean id="provider" class="HelloWorldModel">
      <value>This is a configurable message</value>
  <bean id="renderer" class="StandardOutView">
    <property name="model">
      <ref local="provider"/>

Notice in Listing 4 that we have two beans, provider and renderer. Both of these beans are used in the application, as illustrated earlier in Listing 2. The Spring container builds these beans from the data contained in the beans.xml file. You don't need to perform any complicated construction—the beans are just created ready for use!

  • + Share This
  • 🔖 Save To Your Account