Simple Spring Beans Example
Let us explore the Spring IDE 2.0 features through a sample application. Spring IDE has contributed
Spring Project Wizard in the New Project option of Eclipse. We will start our example application with
a demo project which will use Spring Project Wizard.
Open the new project dialog box by choosing File Menu > New Option and Choose the
Spring Project Node below the Spring Node in the Wizards tree view. Click Next to proceed.
You will see the New Spring Project Dialog box. This is just like the New Java Project Dialog box
in a simplified version. Here type the project name as "springide-demo" and keep all the options at
their default values. Now click Finish button.
This will create a new project with Spring Project Nature in the Eclipse IDE. Spring Nature
provides visual indicators in the Package Explorer view which will be shown in Java Perspective.
Note the S symbol attached to the project name with the default project icon. This is Spring IDE's
way to indicate the Spring Project Nature applied to this project.
Create a package named "com.company.demo" and one class in this package "Developer" with two private
variables with their setter and getter methods. You can generate the setter getter methods through the
eclipse source menu using the generate getter and setters option.
/**
*
*/
package com.company.demo;
/**
* @author Tushar Joshi
*
*/
public class Developer {
private String name;
private int experienceYears;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getExperienceYears() {
return experienceYears;
}
public void setExperienceYears(int experienceYears) {
this.experienceYears = experienceYears;
}
}
The class when opened in Eclipse code window will look like as below. This class is a Plane Old
Java Object (POJO). Spring Framework leverages the use of POJO through declarative beans configuration
files. We will use this POJO which will be injected in our application through Spring Classes using the bean
definition file.
Spring IDE provides a wizard for creation of the beans configuration file. Choose the New options
in the File menu again and the Other option to select the Spring Node and the Spring Bean Definition node
to start the Spring Bean configuration wizard. Click Next to proceed.
New Spring Bean Definition file dialog will be shown where you will have to type a name
for the bean definition / configuration file. Type name demo-beans.xml in the File Name
text box. Choose "src" folder to keep the bean definition file. Click Next to proceed.
The spring bean definition file wizard included the beans XSD by default while creating
the template for the bean definition file. For additional schemas you will have to choose
the schema from the given list of available spring schemas. We will keep all the options
unchecked as we want to write a simple spring beans application which requires only the
core beans XSD which is by default added in the template.
|