Getting started with Spring

Very first activity involved in any learning a new technology is to do the project setup right and testing it. Hence, we start with the “Hello, Spring!” application using Eclipse IDE.

In this section, let us do everything from the scratch. I assume that you have the Eclipse IDE downloaded and started. Also, if you have not done yet, please download the Spring binaries from here.

Once you extract the zip file, you should see this folder structure:

 

1

The “libs” folder contains all the jars required by a Spring application. Note that there are *javadoc.jar and *sources.jar files in the same folder, which we don’t need to include in a project. You can move them to a different folder for convenience. Rest of the files are Spring binaries. Let’s create a “User Library” for these binaries in Eclipse.

From the “Window” menu, choose “Preferences” and select “Java/Build Path/User Libraries”. Click “New…” button and type in a name like “Spring 4.0.5 Library”.

2

Select the “Spring 4.0.5 Library”, click the “Add External JARs…” button to locate the Spring binaries and add all of them to the library. Note that all the jars may not be required for this application, but we need them for later work.

You also need another JAR from Apache Software Foundation, “commons-logging.jar”, which you can get it from here.

Let’s create a new Java project. From the Eclipse menu system, select “File/New/Java Project”. (If you don’t see this option, it is probably because of the perspective. Change the perspective to “Java” from the menu system “Window/Open perspective/Other../Java”).

In the dialog box you got, make the following changes:

  1. Project name -> “Getting started with Spring”.
  2. JRE -> Use default JRE

and click the “Next” button.

In this dialog box, we can select the user-library we created a while ago. Click the “Libraries” tab and from the list of buttons available, click the “Add Library…” button. This opens up another dialog box, with a list of available libraries. Select “User Library” and click “Next”. Now you should see our “Spring 4.0.5 Library” with a checkbox. Select the checkmark and click “Finish” button.

We are now back at the new Java Project settings dialog box. Just click “Finish” to come out of there. You should see a new Java Project in the package explorer, like this one:

3

Let’s code.

Create an interface called vinod.service.HelloService:


package vinod.service;

public interface HelloService {
    public String getMessage(String username);
}

and an implementation class for the same: vinod.service.HelloServiceBasicImpl

package vinod.service;

public class HelloServiceBasicImpl implements HelloService {

    @Override
    public String getMessage(String username) {
        return String.format("Hello %s, how are you doing?", username);
    }

}

Spring requires an XML file containing information about the beans it is supposed to be aware of. In the later sections, you will also be learning how to wire beans with each other so that they work in a collaborative fashion. The name of the XML file can be anything. Let’s create a “beans.xml” in the “src” directory

<?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-3.0.xsd">

    <bean id="bean1" class="vinod.service.HelloServiceBasicImpl" />
    
</beans>

One last thing to do – Write a program to test our setup: vinod.client.Main

package vinod.client;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import vinod.service.HelloService;

public class Main {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");

        HelloService service = ctx.getBean(HelloService.class);

        String msg = service.getMessage("Vinod");
        System.out.println(msg);
    }
}

That’s all. Just run the code you should see your result just like this:

4