SpringIDE - Using Spring in Eclipse

The following article introduces SpringIDE - a set of plugins to simplify development of Spring based applications in Eclipse. I'll cover how to get started with Spring and how to write a simple Spring AOP example.

Spring in a Nutshell

For a consise introduction to Spring, this article on TheServerSide is a good start. In a nutshell, Spring allows you to use Plain Old Java Objects (POJO's) to build an application. You create an XML configuration file to 'wire up' any POJOs that act as beans. These beans can then reside in the Spring container. The most interesting part for me was Spring AOP (Aspect Oriented Programming), which provides 'interceptors' so that I can do something to change the state of the application before or after a particlar method is called.

This was a key feature for me - at the time I wanted to write some aspects to confirm that data could not be corrupted in some safety critical code in my application. Rather than using a seperate AspectJ compiler - the opportunity to have these checks available at runtime was perfect for me. Any sections of the code that I wanted to intercept were converted to POJOs which I then included in my Spring configuration file. Once I wrote up the interceptors, I was ready to test my application's stability thanks to Aspects.

SpringIDE

SpringIDE wasn't available when I wrote this code, so I had to write my configuration files in a plain old text editor, and had to validate them by trial and (a lot of!) error, running the application to find what I had done wrong. Thankfully, that job is a lot easier with the latest version of SpringIDE.

SpringIDE provides a user interface for Spring configuration files, built as a set of plugins on the Eclipse
platform. The IDE provides a wizard for creating Spring projects, and using the Graphical Editing Framework, displays graphs of all beans and relationships either within a single configuration file or within a set of configuration files. The files, or sets, are also validated as they are added/modified. There's also support for Spring AOP, so pointcut expressions are validated in your Spring configuration. You can also see a visualisation of your AOP configuration.


The Aim of this Example

Before we get started, I'm going to outline the aim of this example. I want to monitor each time a method is called in a particular object. While doing this, I should have the opportunity to change the input that the method gets. I'll show how Spring helps us to do this, and I will use the SpringIDE tools to implement the example.

Installing SpringIDE

Before starting ensure that you have the following installed:

Once Eclipse is up and running, use the Eclipse Software Update wizard to go to the SpringIDE update site to download the latest version (http://springide.org/updatesite-dev).

 

0
Average: 3 (3 votes)

James is a DZone Zone Leader and has posted 231 posts at DZone. You can read more from them at their website.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

vsudip replied on Thu, 2008/07/31 - 4:57am

Hi,

I am not able to download the springID update, so caould not process furthe.Can anybody help regarding this?

gihrig replied on Tue, 2008/08/19 - 5:55pm

Over all, a very helpful tutorial

There seems to be a growing recognition among tutorial authors that Spring is in need of a light-weight introduction. This is a very good sign as it suggests that Spring may truly be accessible to the 'every-day' Java developer, shattering the common belief that frameworks (including Spring) are for enterprise gurus. (Silly developers ;-)

This is the third basic Spring tutorial I have attempted, and the first I have been able to get working. Thanks - I now have reason to believe that I too will realize the benefits of Spring.

This tutorial does have a few issues that had me wondering if I was ever going to get the tutorial, and Spring, to work. Here are the problems I encountered and the solutions that got it working for me.


Creating a New Project

There is no instruction, beyond "use the New Spring Project wizard" on how to start this wizard. Some familiarity with Eclipse is assumed. Of course, a greater knowledge than this will be required to work with Java and Spring later on. Still File->New->Other opens the screen shot shown in the tutorial.

No Project Name is provided. A project name of SpringTest is expected in later steps. Not knowing this caused me a lot of confusion and frustration, when I mistook "SpringTest" to mean ...com/eclipsezone/spring/test.

The paragraph on required libraries could be clearer. The required libraries and their locations (as of Spring 2.5.5) are:

spring-framework-2.5.5/dist/spring.jar
spring-framework-2.5.5/lib/jakarta-commons/commons-logging.jar
spring-framework-2.5.5/lib/cglib/cglib-nodep-2.1_3.jar

Object Creation

The text has no indication where to put the ISampleBean interface code. The following screen shot on creating the SampleBean class shows the Source folder and Package which describe this.

.../SpringTest/src//com/eclipsezone/spring/test/ISampleBean.java

The SampleBean class is incorrectly named "SamplebeanImple" in the screen shot. It is later referred to as SampleBean.


Create a New Bean Definition File

Placing this file in the project root directory "SpringTest" is vital. 
Not knowing this caused me a lot of confusion and frustration, when I mistook "SpringTest" to mean
.../com/eclipsezone/spring/test.

The correct action is: Right click the project root folder, "SpringTest", and select New->Other->Spring->Spring Bean Definition to create the Spring Bean Definition file.

There is a minor typo following the XSD namespace declarations screen shot.

... As we are only using on configuration file, Should be:
... As we are only using one configuration file,


Add Method Interceptor

The code will not compile as listed for class Interceptor. The
"@Override"
annotation is wrong and should be removed. 

Not a problem with the tutorial, but I was unable to get the beanDefinition  xml editor to change the value associated with "value" elements.

e) property
a) name - interceptorNames
e) list
e) value - interceptor

Right clicking the value element provides no "Edit Attribute" function.

The solution I used was to open the beanDefinition.xml file in Source view with the tab at the lower left corner of the editor window, and paste in the code from the tutorial.


Spring Explorer

The paragraph following the Show View screen shot would benefit from a little reorganization:

In this view you can easily add in Config Sets or single Config Files. If you right click on the Config File and choose View Graph, you get to see a nice representation (using Eclipse's GEF) of your Spring configuration file ...

Suggest:

If you right click on the Config File in this view and select Properties, you can easily add in Config Sets or single Config Files.  Choose Open Graph, and you get to see a nice representation (using Eclipse's GEF) 
of your Spring configuration file ...

--------------------

The bottom line is, I got my first working understanding of Spring AOP and Interceptors from this tutorial and I am grateful for that.

Four stars (would be worth five with the above issues fixed).

Thanks!

-Glen

William Willems replied on Mon, 2008/11/17 - 8:30am

Very nice intro to start using the features provided by the Spring IDE. I like it that it is pretty high level and shows some AOP stuff.

GZI replied on Mon, 2009/04/27 - 4:38am in response to: vsudip

Hi. please try http://springide.org/updatesite/.

swatimalla replied on Wed, 2009/09/02 - 1:30am

Hello EveryOne,

I tried to download springIDE from the follwoing link  (http://springide.org/updatesite-dev).When i click on this link it gives an error back saying

<Error>
<Code>NoSuchKey</Code>
<Message>The specified key does not exist.</Message>
<Key>release/IDE-dev</Key>
<RequestId>536B0F024044807C</RequestId>

<HostId>
30YvYelUJVb04zlb6uM+2FSFx+xK8/+xh2x6/Z/VLvQwhzqL0fOeaIdFrDhgXs18
</HostId>
</Error>

Please can someone send me the plgin if some body has it..

I would really apprecitae if some one can send me a reply back..

Thanks,

swati

sachintodkarus@... replied on Fri, 2009/10/30 - 12:23pm

Hi , I am getting the following error when I try to run this sample. I am new to java so apologize if this is silly. INFO: Bean factory for application context [org.springframework.context.support.FileSystemXmlApplicationContext@15eb0a9]: org.springframework.beans.factory.support.DefaultListableBeanFactory@72ffb Oct 30, 2009 1:25:04 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@72ffb: defining beans [com.eclipsezone.spring.test,com.eclipsezone.spring.aop.Interceptor,proxyBean]; root of factory hierarchy Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proxyBean': FactoryBean threw exception on object creation; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class java.lang.String]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class java.lang.String at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:127) at java.security.AccessController.doPrivileged(Native Method) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:116) at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:91) at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1288) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:217) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:880) at com.eclipsezone.spring.test.Tester.main(Tester.java:22) Thanks, Sachin

sachintodkarus@... replied on Fri, 2009/10/30 - 1:05pm in response to: sachintodkarus@yahoo.com

Please ignore this request I found the problem Thanks, Sachin

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.