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
| 485932 views |
  • submit to reddit

Using Spring Boot and the new Getting Started Guides

If you’ve seen the new Spring Boot project, then you know how easy it is to get started with Spring using Spring Boot.  There is a direct integration for that in the tooling starting in version 3.4.0. You can go ahead and create a new Spring Boot project by selecting the “New -> Spring Started Project”. It will open a simple wizard for you that lets you choose among various options for what to get on the classpath of the new Spring project and it creates a simple main Application class that you could use to startup the application.



Since Spring Boot takes care of starting up an embedded Tomcat instance, you don’t have to deploy your application to a server that you configured inside your IDE anymore. Simply starting the project as a general Java application will work just fine.

In addition to using the very basic (and purist) Spring Boot approach, you could also use the new Getting Started Guides (http://spring.io/guides) directly from within the tooling. To do that, you select the “New -> Import Spring Getting Started Content” wizard and go from there. It lists all the available guides that you can find on the http://spring.io website and lets you import them into your IDE quickly (alongside the documentation inside the IDE using a browser view). This provides you with a nice learning experience when getting started with Spring in general or when diving into certain features of Spring.



Configuring an Existing Project

If you are already a Spring developer and want to benefit from the Spring tooling in your IDE, you will want to enable the tooling for any existing projects in your workspace. Just select your project, open the context menu, and select “Spring Tools -> Add Spring Project Nature”. Once you’ve done that, the IDE treats this project as a Spring project and enables all the Spring-related features for it.

Once the project in Eclipse or the Spring Tool Suite has the Spring nature enabled, you can go ahead and configure the used Spring bean config files. To do that you just go to the project preferences and select “Spring -> Beans Support”.



There you can configure the Spring config files (either the Spring XML config files or the @Configuration classes if you are using the pure annotation-based method). This lets the tooling know about your config files and tells the tools where to start the Spring-specific analysis of your project.

Some of those config files are recognized and configured automatically, since the tooling knows about the usual names and the usual locations for those files (especially for web apps). If they are not showing up automatically, please go ahead and configure them manually (and probably file a bug with the Spring IDE developers to let them improve the automatic discovery of those config files).


Working with the Spring Tooling

The tooling is what makes working with Spring in Eclipse a real pleasure. The Spring explorer view and the corresponding section of the project explorer give you a nice overview of your Spring project, and every time you change your code or your config files, it gets updated. It allows you to quickly navigate to the source code and helps you quickly understand the structure of your Spring project.

The most valuable aspect of the tooling is the support for editing and validating Spring configurations. It doesn’t matter if you use Spring XML config files or if you prefer the annotation-based configuration style.  The tooling gives you meaningful content-assist all over the place and assists you with additional, Spring-specific validations that help you identify problems, typos, and other issues as early as possible.

The most comprehensive support is available for working with Spring XML config files. When defining Spring beans in the XML config files, you get content-assist for all kinds of elements, including full assist for typing in the class of a bean, referring to other bean definitions, or referring to a class property in order to inject dependencies.



If a problem appears, the tooling assists you in fixing this problem by providing quick-fix solutions. That makes it easy to add, for example, a missing bean definition or to create a new property in the related Java class, if the config file refers to a missing property.



Validations take care of checking your Spring config files and they make sure that you don’t forget a bean definition. They also points you toward the better options for writing your Spring configs if it detects a common anti-pattern. This is extremely useful for autowired dependencies where you don’t know right away what gets injected at runtime, or whether something exists at runtime that will be injected into an @Autowired field or parameter. The tooling can check this for you and give you a warning if it cannot find anything that could get injected there.



A list of the available validations can be found in the global preferences via “Preferences -> Spring -> Project Validators”. That preference page allows you to enable or disable those validations as well.


Refactoring

Improving the structure of your code is always on your mind if you’re a professional developer, and today’s IDE provide nice refactoring support for these tasks. The Spring tooling, for example, integrates the Spring-specific configuration files into this refactoring support of your IDE. Whenever you rename a class in Eclipse, it gets updated in your Spring config files as well. The same happens if you change the name of a property in your Java class. If there is a reference to this property in one of your Spring config files, it gets updated as well.  In addition to that, it also allows you to rename beans themselves via a special “Rename Bean ID” refactoring, changing all references to that bean ID automatically.


Additional highlights

This article covers only the basic features and first steps for getting started with the Spring tooling for Eclipse. The tooling provides a lot more functionality than what is mentioned in this article. The graphical views and editors are especially powerful when working with Spring Integration, Spring Batch, and Spring Webflow. And there is more and more support emerging for annotation-based Spring configurations, although the general Java tooling in Eclipse is already extremely useful and supportive for those out-of-the-box.

If you want to learn more about the tooling and see what is new in the latest versions, please take a look at the new and noteworthy documentation that comes with every release:

http://static.springsource.org/sts/nan/latest/NewAndNoteworthy.html

More information can be found here:

http://spring.io/tools



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.