Skip to end of metadata
Go to start of metadata


Based on the sample example, this appendix describes how to use Spring in more detail.

The name of the sample projected described in the appendix is HelloSpring and was created in the Eclipse.

HelloSpring project is a simple program that prints a greeting message. It prints the greeting message defined in the bean by importing the bean (greetingService) defined in the bean configuration file (applicationContext.xml) from the Spring container (BeanFactory).

HelloSpring Implementation

1. Creating HelloSpring project


1-1. Click Menu – File – Java Project
1-2. Project name: Enter HelloSpring in
1-3. Click the Finish button

2. Implementing sample Java file


Implement the following class and interface.

2-1. GreetingService interface

This is the interface that defines the service.

Right-click the HelloSpring project's src directory and click New – Interface.

Enter com.altibase.hello for Package: and GreetingService for Name:.

When the GreetingService.java file is created, write the source as follows.

GreeingService.java

2-2. GreetingServiceImp class

This class implements the GreetingServie interface.

Right-click the HelloSpring project's src directory and click New-Class.

Enter com.altibase.hello for Package: and GreetingServiceImp for Name:

Click Add button next to Interfaces:, enter GreetingService, and then click the OK button.

When the GreeingServiceImp.java file is created, write the source as follows.

GreeingServiceImp.java

3. Defining bean configuration 


Define bean for GreetingServiceImp in the bean configuration file (applicationContext.xml).

Right-click the HelloSpring project and click New – File.

Create applicationContext.xml in File name:.

Write the following in the applicationContext.xml file.

applicationContext.xml

greetingService is the name of the bean. This can be used by importing the bean with the corresponding name in the java program.

In the class, specify the full name of the GreetingServicelmp class in charge of the service, and put the message to be set in the member variable greeting defined in GreetingServicelmp in the property.

4. Defining HelloApp class


As the main class, get the bean defined GreetingServiceImp from the BeanFactory and call the service method (sayGreeting()).

Right-click the HelloSpring project's src directory and click New-Class.

Leave the Package: part blank and enter HelloApp in Name:.


When the HelloApp.java file is created, write the source as follows.

HelloApp.java
The source applicationConext.xml above is the bean configuration file defined above, and greetingService represents the bean name for the GreetingServiceImp class defined in applicationContext.xml.

5. Adding related jar files


Add Spring related jar file to HelloSpring.

Right-click on HelloSpring project and click Properties – Java Build Path – Add External in Libraries

Click JARS to add spring.jar file and common-logging.jar file among Spring Framework jar files.

Please refer to the description of DriverManagerDataSource for the location of the file.

6. Running the HellSpring project

After clicking the HelloSpring project, run Run from the menu or click the "R Run" button.

The Hello string is printed in the console window.


  • No labels