Introduction IDE 2.0 plugin.
also read:
Spring Framework
Spring Framework is a light weight framework which provides Inversion of Control or Dependency Injection and many other utilities through many sub projects. Spring modules like Context, DAO, ORM, AOP and many more provide standard ways and templates to use Java SE features. Spring IDE 2.0 is a tool to facilitate writing applications using Spring Framework easily.
Prerequisites
I have used the latest available stable versions of the tools to demonstrate and explain the features of Spring IDE 2.0. To summarize all the tools needed for working on the steps I have explained, we will need the following installed.
- Operating SystemWindows, Linux, Mac any operating system may be used as we will be doing platform independent stuff. I have used CentOS 4 and Gnome windows manager for the demonstration images.
- Java SE 6.0You will have to make sure Java is installed before you attempt any of these steps. I used Java SE 6.0 version.
- Eclipse 3.3 Europa with WTPEclipse 3.3 version also called as Europa release with WTP is available as a single bundle. I used Eclipse with WTP included as some Spring IDE features need WTP installed. Minimum version of Eclipse IDE for Spring IDE plugins is 3.2.x, anything equal to or above 3.2.x should work. Minimum version of the Web Tools project is 1.5.x and this is also a requirement for the Eclipse 3.2.x release.
- Spring Framework 2.0.6 Spring Framework is needed obviously as we will be developing sample application for Spring Framework. We will just need some library JAR files in our projects from the Spring Framework, and hence no installation as such is needed.
- Spring IDE 2.0This article explains steps to install Spring IDE 2.0 on Eclipse3.3 with WTP as a plugin.
Installing Spring IDE 2.0
We will need the Spring IDE update site ZIP file which can be downloaded from the Spring IDE update URL
There are two ways of installing Spring IDE 2.0 on Eclipse.
- Using Update Site DirectlyThis method is described step by step in the Spring IDE Install page in Spring IDE Wiki. This wiki page is very slow currently and requires considerable time to get loaded fully in browser.
- Using Site Archive downloaded as a ZIP file from update URLWe will follow the second way. Using the first way is very similar and only some of the first steps need to be changed to use that option, which can be figured out easily. Download the ZIP file and remember the location where the file is stored.
Installing Spring IDE Plugin in Eclipse
Open the Help menu and click on the Software Updates option. You will see Find and Install menu, choose this menu.
You will see the feature updates dialog box. Here we will choose “Search for new features to install” radio button, and then click the Next button.
Update sites to visit Dialog
You will see the Update sites to visit Dialog box. Here we will choose “New Archived site” button.
Select Local Archive Site Dialog
You will see the Select Local Archive Site Dialog box. Navigate to the directory where you have stored the downloaded Spring IDE 2.0 update site ZIP file. Select the ZIP archive and click OK to proceed.
Edit Local Site Dialog
You will see the Edit Local Site Dialog box. Ensure the file selected is correct and don’t change anything here we will keep the contents in this dialog box as they are and will click the OK button.
Coming back to Update sites to visit Dialog
You will see the Update sites to visit Dialog again with one new entry for the Spring IDE update site ZIP archive. This entry will be selected by default. Now click the Finish button.
Search Results Dialog
You will see the Search Results Dialog listing all the available features in the update site ZIP archive. We will select all the check boxes to install everything. The second option installs the sources for Spring IDE plugin which is not needed for normal working of the Spring IDE plugin. Click the Next button to proceed.
Feature License Dialog
You will see the Feature License Dialog. Accept the license by selecting the I accept radio button. Click Next to proceed.
Installation Location Dialog
You will see the Installation Location Dialog. here we can see the default location where the selected plugins will be installed. Here we also get a chance to change the location if we want to change it. We will keep the default location chosen by Eclipse as it is and click Finish.
Update Manager Progress Dialog
You will see the Update Manager Progress Dialog with the progress for the extraction of the ZIP files and storing the plugin JARs in temporary directory.
Feature Verification Dialog
After extracting the plugin JARs from the ZIP archive Eclipse will show the Feature Verification Dialog. Click the “Install All” button to install all the plugin features available in the update archive.
Restart Choice Dialog
You will see the Restart Choice Dialog. Choose the Yes button to restart the Eclipse and get the plugin installed properly.
Verifying our Spring IDE 2.0 Plugin Installation
After Eclipse starts again we need to verify whether our plugin got installed successfully. We will do this by choosing the Windows menu and the Preferences option of Eclipse.
Preferences Dialog
We can see Spring option in the left hand side item nodes in the preferences dialog. Choose the Spring option and it will expand to show three sub items.
- AOP Support
- Beans Support
- Web Flow Support
This confirms that our Spring IDE plugin has been installed successfully in Eclipse.
New Project Wizard Check
We can also check the plugin features by observing the new project wizard dialog by choosing File Menu, New Option and selecting Other from the New sub menu.
We will see the Select a Wizard dialog box where we can see Spring as a node and three options listed in Spring node
- Spring Bean Definition
- Spring Project
- Spring Web Flow Definition
Show Views Dialog Check
Spring IDE 2.0 also installs view features which can be verified by choosing the Window menu Show View option and choosing the Other option from the sub menu.
Show View Dialog box will be shown with Spring as a node with three items listed below the Spring node
- Bean Cross References
- Spring AOP Event Trace
- Spring Explorer
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.
[code lang=”java”]
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;
}
}
[/code]
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.
Wizard provides a way to add the newly created bean definition file in any of the existing config sets. Config set is a Spring IDE way to logically group the bean definition files. We don’t have any config sets created yet so this box is shown empty we will just click the Finish button to obtain the basic bean definition template.
The bean definition wizard will create a file and will add the default template for the bean definition in that file. The topmost <beans> element is already added to the bean definition file. Now we will have to add our required <bean> definitions to make real use of this file.
[code lang=”java”]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi=""
xsi:
</beans>
[/code]
Spring IDE extends the functionality of the XML editor in Eclipse. The file demo-beans.xml can be visualized in the XML editor and edited using the visual tabular elements.
We can also type elements directly in the Source tab of the editor.
We can add elements through the outline view by right clicking on the elements and choosing Attribute or Child option to create a new Attribute or new Child element.
One important feature provided by the Spring IDE is code completion in the source editing of bean definition file . Let us add one bean definition for our Developer POJO class.
[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi=""
xsi:
<bean id="developer">
<property name="name" value="Shekhar"></property>
<property name="experienceYears">
<value>5</value>
</property>
</bean>
</beans>
[/code]
After typing some characters of Developer class name use the CTRL+Spacebar keyboard shortcut to invoke the code completion feature and Spring IDE provides recognition to the java elements in the bean definition file, so we get the full class name with package to choose from.
While typing property element in the bean definition you can see how Spring IDE provides code complete feature for the Java elements, this time for the properties of the POJO.
Add Spring Framework Dependencies
For this project to work we will need Spring Framework dependencies. The JAR files needed in the classpath of this project to run this project are
spring.jar
This is the main dependency JAR file. The spring framework distribution contains this file in the dist directory.
commons-logging.jar
Spring framework internally used commons-logging API and hence we will need this file as a dependency for the spring JAR file. This file is available in the lib/jakarta-commons directory of the spring distribution.
log4j-1.2.14.jar
We will use Log4j for the logging purposes. If we don’t include this file Spring will default to Java logging mechanism through the commons-logging API. With this JAR file the log4j mechanism will be plugged to the commons-logging API.
Right click the project name in the project explorer view and choose New > Folder option. Give name “lib” to the folder. This will create a folder named lib in the main project directory. Add these three JAR files in the project in the lib directory through local file system. Then refresh the Eclipse project by choosing refresh option in the context menu of the project. These files will be shown in the project explorer now. Select all the three files and right click on them. On the context menu choose Build path > Add to build path option.
This will show all the libraries in the referenced Libraries node of the project explorer view and the lib folder node will be shown empty. Don’t worry Eclipse have not deleted the files they are just being shown in a different node to indicate they are in build path now.
Viewing Bean Definitions in Spring Explorer
Spring IDE has provided a separate Spring Explorer to show only spring related artifacts. To see the Spring Explorer right click on the demo-beans.xml bean definition file and choose the Spring Explorer option from the context menu.
By double clicking on the nodes of the Spring Explorer view you can open the corresponding Java Bean file or the bean definition file.
Spring Explorer opens in the bottom portion of the Eclipse Workbench where the console and problems views are shown. Let us shift the Spring Explorer to accompany the Project Explorer and it will look like a Spring project Explorer now.
Bean Cross Reference View
You can see the bean definitions in a cross referenced way through the beans cross reference view. To open this view choose the Windows Menu, Show View option and selecting the Beans Cross Reference view from the Spring Node in the Show View Dialog Box. The beans cross reference view will also be shown in the bottom portion where console view is shown. Let us keep this view accompanying to the outline view as shown in the image below.
Bean Cross Reference View will not show anything unless you use the “link open editors with contents in Navigator” button available on the view top right portion.
Graphical representation of Spring Beans
Spring IDE provides a Graphical Visualizer for the bean definitions. Right click the demo-beans.xml file shown in Spring Explorer or the Bean Cross Reference view and close “Open Graph” option from the context menu.
Graph view for the beans definition file will be shown in the editor pane of the Eclipse Workbench. This graphical representation provides a picture of the complete bean definition file.
Let us add one more POJO to our project. This time we will write a class which references our Developer class.
[code lang=”java”]
package com.company.demo;
/**
* @author Tushar Joshi
*
*/
public class SoftwareCompany {
private Developer leadDeveloper;
private Developer developer;
public Developer getLeadDeveloper() {
return leadDeveloper;
}
public void setLeadDeveloper(Developer leadDeveloper) {
this.leadDeveloper = leadDeveloper;
}
public Developer getDeveloper() {
return developer;
}
public void setDeveloper(Developer developer) {
this.developer = developer;
}
}
[/code]
SoftwareCompany class has two properties of Developer type with names leadDeveloper and developer. We want these properties to be injected from the spring mechanism through the bean definition file.
Our completed bean definition file will be like this
[code lang=”java”]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi=""
xsi:
<bean id="developer">
<property name="name" value="Shekhar"></property>
<property name="experienceYears">
<value>5</value>
</property>
</bean>
<bean id="softwareCompany"
class="com.company.demo.SoftwareCompany">
<property name="leadDeveloper">
<ref bean="developer" />
</property>
<property name="developer">
<bean>
<property name="name">
<value>Venkat</value>
</property>
<property name="experienceYears">
<value>2</value>
</property>
</bean>
</property>
</bean>
</beans>
[/code]
I have used one reference property pointing to the earlier created developer bean and other property with a new developer bean definition in place. When we see the Graph for this bean definition file we will see this graph view.
Let us add a launcher class for these beans and a main method to invoke the spring bean factory mechanism. We will add DemoApp class to the project with a main method.
[code lang=”java”]
/**
*
*/
package com.company.demo;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
/**
* @author Tushar Joshi
*
*/
public class DemoApp {
private static final Logger logger = Logger.getLogger(DemoApp.class);
/**
* @param args
*/
public static void main(String[] args) {
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"demo-beans.xml"));
SoftwareCompany company = (SoftwareCompany) beanFactory
.getBean("softwareCompany");
logger.info("Lead Developer: " + company.getLeadDeveloper().getName());
logger.info("Developer : " + company.getDeveloper().getName());
}
}
[/code]
The main method obtains a BeanFactory reference through the XmlBeanFactory concrete class. We pass object of ClassPathResource object created by passing the name of the bean definition file. This way we can keep the beans-demo.xml in the classpath and the spring framework will locate it automatically through the class path searching mechanism.
We will need a minimal log4j.xml file to define the way we want the output of the application to be presented. Let us add a log4j.xml file to the src folder of the project.
[code lang=”xml”]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC
""
"">
<log4j:configuration xmlns:
<appender name="console">
<layout />
</appender>
<root>
<level value="info" />
<appender-ref
</root>
</log4j:configuration>
[/code]
Note the level of the root element is kept at INFO for demonstration purpose. Running this class as a Java Application will provide output like
[code]
INFO – Loading XML bean definitions from class path resource [demo-beans.xml]
INFO – Lead Developer: Shekhar
INFO – Developer : Venkat
[/code]
also read:
Conclusion
Spring IDE provides features like Spring Explorer, Beans Cross Reference, Graph View and Code completion in the bean XML editor hence making the life of a Spring Framework user easier. It provides intuitive ways to visualize the beans definition file and helps increase the understanding of user about the loaded beans through spring inversion of control mechanism.
If you have any questions on the spring framework integration with JMS,.