Eclipse Zone is brought to you in partnership with:

I've been a zone leader with DZone since 2008, and I'm crazy about community. Every day I get to work with the best that JavaScript, HTML5, Android and iOS has to offer, creating apps that truly make at difference, as principal front-end architect at Avego. James is a DZone Zone Leader and has posted 639 posts at DZone. You can read more from them at their website. View Full User Profile

Spring IDE and the Spring Tool Suite - Using Spring in Eclipse

01.10.2014
| 502002 views |
  • submit to reddit

DZone is proud to present this complete overhaul and update of the most popular article ever written on Javalobby.  Previously titled "Spring IDE - Using Spring in Eclipse," this article from 2008 is still one of the highest ranked pages on Google from DZone.  Originally written by Javalobby Zone Leader, James Sugrue, this article has been mostly rewritten by Martin Lippert, a developer in the SpringSource tools division.  Enjoy the 2014 version!

This article is an introduction to Spring IDE and the Spring Tool Suite – a set of plugins to simplify the development of Spring-based applications in Eclipse. Let’s start by looking at the Spring Framework and how we can use it to write a few simple Spring applications.


Spring in a Nutshell

For a concise introduction to Spring, read the reference documentation for Spring 4. It contains a nice introduction to the core concepts as well as a comprehensive overview and in-depth look at all of the various features that come with the latest version of the Spring core framework. In addition to the core framework, the Spring IO platform documentation gives you an additional overview about how to use both the execution layer and the foundation layer components to build modern enterprise applications.

In the simplest terms, Spring allows you to build an application using  Plain Old Java Objects (POJO's) and dependency injection, which helps you wire up the different components of your application. Most people prefer to use annotations, or Java Configuration and therefore stay on the pure Java side.  You can also use an XML configuration file to 'wire up' your POJOs that act as beans.  All of these methods are perfectly fine and supported by Spring.

The components that you define, the Spring beans, can then reside in the Spring container. In addition to the pure dependency injection mechanism, Spring provides a first-class integration for aspect-oriented programming (AOP) and comes with a huge set of service-level abstractions. Those are ready-to-use bean implementations that encapsulate certain technologies like transactions, persistence technologies, and many more. All this results in a programming model that lets you write your application independent of a concrete technology or a concrete runtime environment for deployment. This is why, many years after its release, Spring probably the most popular framework and environment for implementing Java enterprise applications.


Spring IDE (Eclipse Plug-in)

The Spring IDE is an open-source project that provides a set of plugins for the Eclipse IDE. These plugins make the Eclipse IDE Spring-aware. After installing the Spring IDE plugins, your IDE understands your projects from the perspective of the Spring framework and provides you with a wide variety of additional features that make it easier and more convenient to work with Spring projects in Eclipse.

The IDE provides various wizards for creating Spring projects and getting started with Spring if you’re a beginner.  Of course, it can also be used on existing Spring projects. Once you have a project configured to be a Spring project, you can let the IDE know which Spring configuration files your projects uses (either those XML config files or your Spring JavaConfig classes that use the @Configuration annotation). From there on, you can benefit from the Spring-specific tooling support, including:

  • Spring-specific content-assist and validation for your spring config files
  • Spring-specific refactoring support for your spring config files
  • Graphical visualization of your beans and their dependencies
  • Graphical diagram-like editors for Spring Integration, Spring Batch, and Spring Webflow
  • Advanced support for Spring aspects including pointcut visualization and navigation
  • Integration with AJDT and Eclipse Mylyn tooling
  • Support for Spring bean profiles and validation inside profiles
  • Direct integration of the ‘getting started’ guides from http://spring.io/guides
  • A number of example and reference applications to learn from
  • Support for creating and using Spring Boot projects right within your IDE
  • and more…

The Spring Tool Suite (Full Distribution)

While the Spring IDE project provides a set of plugins for the Eclipse IDE, the Spring Tool Suite comes as a ready-to-use distribution of the latest Eclipse releases with the Spring IDE components pre-installed.  This includes the tc Server integration for Eclipse (another IDE extension that is provided by Pivotal as an open-source project) and various other additions to Eclipse that turn the pure Eclipse IDE into a ready-to-use, best-of-breed environment for enterprise Spring application development. To download the Spring Tool Suite, go to: http://spring.io/tools


Lets get Started - Installing the Spring Tooling

There are two ways to get Spring-tooling enabled in Eclipse: Either you download the Spring Tool Suite distribution as a ready-to-use package (recommended) or you download the latest Eclipse JEE package from http://www.eclipse.org/ and start from there.  You might also have an existing Eclipse IDE running on your machine that you just want to extend with the Spring tooling. The easiest way to install the Spring tooling into your existing (or freshly downloaded) Eclipse IDE is to go the Eclipse Marketplace and search for the “Spring Tool Suite” - and install from there.

If you would like to use Maven (which is used in many Spring projects) and you haven’t installed the Maven Integration for Eclipse yet, you should go ahead and do that. You can grab it from the Eclipse Marketplace or directly from the main Eclipse update repository, which is pre-configured in your Eclipse IDE.  Just look for the “Install New Software” wizard. It is also recommended that you install the Maven Integration for Eclipse WTP, which makes it easy to work with web projects using Maven inside Eclipse. If you decided to download the Spring Tool Suite distribution package, those extensions are already pre-installed for you.


Create a New Project

To create a new project, you just need to use the New Spring Project wizard to get going. If you don't do this, it's ok – you can just add a "Spring Nature" to your project at a later stage.



The wizard provides you with a few options for getting started with a new Spring project. For some of the common use cases, the Spring tooling provides a number of templates, like a simple Spring MVC application or some examples for using Spring Integration, just to name a few. After giving the project a name and selecting the template, the wizard usually allows you to customize a few settings in the project (like the package names).  Once that’s done, you are ready to go. This is an easy way to get started with a ready-to-use Spring project that already runs and does something for you.

The simple Spring MVC project example is a very popular one that gets you started with a more classic Spring web application, which includes:

  • Traditional Spring XML configurations for the web application
  • Component scanning for the controller implementations that use the @RequestMapping annotation in the source code
  • A traditional JSP page for the frontend, which is probably similar to the structure of many existing Spring web applications

If you are using the Spring Tool Suite distribution, you get a ready-to-use tc Server instance created and configured in your workspace. It allows you, for example, to deploy and run your freshly created Spring MVC app immediately.

If you would like to start with an empty project instead, you can do that, of course. For this case, there are a few simple templates provided by the wizard that help you do that. If you choose to use Maven for your project build and dependency management, you could choose the simple Maven web template. It doesn’t create any code for you, but it puts all the necessary Spring libraries on the classpath of the project.

If you started with the Spring MVC web application example, some of the features of the Spring tooling will immediately catch your eye. The project explorer gets a new section “Spring”, that shows you the structure and the beans of your Spring applications. Furthermore, it gives you an overview of the annotation-based stereotypes used within your project. In web applications, for example, it gives you a nice overview of the defined @RequestMapping methods with the ability to jump right into the code.



Next Page: Using Spring Boot and the new Getting Started Guides

Comments

Sudip Verma 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?

Glen Ihrig 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.

Giuseppe Zitelli replied on Mon, 2009/04/27 - 4:38am in response to: Sudip Verma

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

swati gudla 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

sachin todkar 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

sachin todkar replied on Fri, 2009/10/30 - 1:05pm in response to: sachin todkar

Please ignore this request I found the problem Thanks, Sachin

Freddy Cruz replied on Wed, 2009/12/16 - 5:17pm in response to: Glen Ihrig

Thanks for you support! I have 3 days reading the spring framework reference, and now...  i test my first project susscefully.

 I will go intro Spring world

Wp Moore-taylor replied on Thu, 2010/02/04 - 3:19am

Hi , I am getting the following error when I try to run this sample, any help would be good

 

INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@199f91c: defining beans [com.eclipsezone.spring.sample,interceptor,proxyBean]; root of factory hierarchy
3
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'proxyBean': FactoryBean threw exception on object creation; nested exception is java.lang.NoSuchMethodError: net.sf.cglib.proxy.Enhancer.setInterceptDuringConstruction(Z)V
    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 spring.test.Tester.main(Tester.java:26)
Caused by: java.lang.NoSuchMethodError: net.sf.cglib.proxy.Enhancer.setInterceptDuringConstruction(Z)V
    at org.springframework.aop.framework.Cglib2AopProxy.getProxy(Cglib2AopProxy.java:182)
    at org.springframework.aop.framework.ProxyFactoryBean.getProxy(ProxyFactoryBean.java:363)
    at org.springframework.aop.framework.ProxyFactoryBean.getSingletonInstance(ProxyFactoryBean.java:317)
    at org.springframework.aop.framework.ProxyFactoryBean.getObject(ProxyFactoryBean.java:243)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport$1.run(FactoryBeanRegistrySupport.java:121)

Phil Hanchet replied on Wed, 2010/08/25 - 7:37am

Brilliant - thanks so much for this James - huge help on a massive topic Phil Hanchet OTG

Comment viewing options

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