Eclipse: open declaration/resource for spring beans and contexts - eclipse

While watching one of my colleagues work in IntelliJ, I was jealous to note that he could Ctrl/Command+Click on the name of a spring bean, context, or resource in either a Java annotation or a Spring context file and IntelliJ would open the backing declaration or file.
I've installed the STS suite of plugins for Eclipse, but this has only given me the ability to open declarations for beans referred to in a spring context file. E.g., command+click only works in my context file for: <bean ... ref="bean-name"/>.
Specifically, things I can't open the declaration/resource for automatically are:
In my context file: <import resource="classpath:config/anotherContext.xml"/>, I cannot open the resource anotherContext.xml. The open resource dialogue helps, but doesn't tell me which one my classpath is actually giving me.
In my Java file: #ContextConfiguration({"classpath:/config/yetAnotherContext.xml"}), I cannot automatically open the resource of yetAnotherContext.xml.
In my Java file: #Resource(name = "another-bean-name"), I cannot open the bean declaration. This one is especially difficult to discover in Eclipse, I haven't figured out an easy way to discover the bean declaration short of full search.
Surely there's a way in Eclipse to automatically discover declarations or resources for spring beans and contexts as they are used in both Java annotations and context files?

I found a solution for my first example, specifically, finding a spring resource imported into a spring context file in this StackOverflow question. Specifically, in the project properties under Spring->Beans Support, select the checkbox for Enable support for <import /> element in configuration files.
This also gives the ability to open bean declarations for beans defined in imported spring resource files.
I have yet to discover similar functionality for Java files that wire in Spring resources and beans via annotations.

Related

How to explain the different behaviour of spring in eclipse and standalone application

We have a web application using spring framework which works well. We try to use the same spring context in a standalone application built with maven. We managed to get the context by launching the application within eclipse, a main class instantiates the Spring context with the following lines:
ApplicationContext ap = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
But when we build a jar with maven and launch it with a command line we encounter errors. Here is the command line:
java -jar application.jar
The jar contains a META-INF that defines the classpath generated by maven-jar-plugin.
First spring failed on this error
Unable to locate NamespaceHandler when using context:annotation-config
We solved the problem by merging the different spring.handler files from maven spring jars and putting the result in our application.jar. This is a common solution to this problem but this doesn't explain why this failed in standalone mode but worked in a web app and in eclipse. Eclipse and standalone use the same code (java and spring configuration files).
Now we are facing a second problem, some beans instantiations fail. Some of our beans are declared with XML, some other beans are declared with annotations. Beans declared with annotations are not instantiated in standalone mode, traces in constructors show it, so instantiation of XML beans referencing these beans fails. We didn't solve this problem yet.
We don't understand why we observe a different behavior in eclipse or in standalone mode. What can explain the difference ? How does eclipse call java, how does-it set the classpath ?
Thanks for any response,
Mickaƫl

How to jump to Spring context XML file from Java Class in Eclipse

I well read the question How to find Spring XML definition from Java Class in Eclipse but it does not seem to answer similar question from me.
I would like to jump from Java class in editor to the spring context xml file.
If I highlighted a class declaration in editor, by pressing a shortcut key or clicking on some icon, it should open the corresponding spring context xml which have the bean initialization definition and jump to the line which initializes the bean "< bean id=... >". If I highlighted a set method in editor, it will do something similar but jump to where the property is set "< property name=method_name... >"
I am using Eclipse Java EE IDE for Web Developers Indigo SR2. I have installed Spring IDE Developers and SpringSource. I have added Spring nature to the project, added context xml to the project property Spring Beans Support Config Files, and could see the "S" on file icon decorator of the context file and java class, but I could NOT find a way to jump from the Java class to the spring context xml.
My colleague is using IntelliJ and they could see a bean icon in their editor next to class declaration. I would hope to see something similar on Eclipse. Ideally, not only class level, but also next to set methods.
Any advice will be much appreciated!
Thanks
Gerry
The desired action is provided by the Spring Tools (Spring Tool Suite), which can be installed the usual way, i.e. as Feature, or via the Eclipse Marketplace.
your current project needs the "spring project nature"
moreover, your build must include the "spring project builder"
and, in your project configuration project>properties, "Spring" tab, you need to define all your Spring XML files.
In Eclipse Mars, all of this will be setup automatically, when importing a Maven project and using the M2E plugin. Moreover, there is also a setting in the "Spring" tab, which allows to autodetect your Spring XML files. Anyhow, the spring project configuration needs to parse and evaluate those XML files during the incremental build of your project. So far for the prerequisites.
The actual function is called "open spring bean"
By default, there is no keybinding for this operation, so either
invoke it by name: press ctl-3 and start typing the name "open spring bean"
set up a custom key binding: press ctrl-shift-L and ctrl-shift-L again, to get the keybindings editor. Use the search to get to the function "open spring bean", make sure you select "in windows" as scope and add the key binding you like (e.y. I used ctrl-y, which is good to type with the left hand while using the mouse with the right hand).
This function will give you a Eclipse typical quick-hover-and-search box, where you can start typing. If the cursor is on the class name of the bean implementation Java class, the view will be populated already and you just need to hit enter.
<Rant>
This is such a crucial and basic feature. Why the Spring and Eclipse folks are incapable to set up and document the very basics in a way easy to discover is beyond me. No wonder every idiot thinks that IntelliJ is vastly superior, which it is not. In reality, both are on equal footing and have their strengths and weaknesses here and there.
</Rant>
I don't know if SpringSource or such have any specific keys for these, but good old Ctrl-shift-R (Navigate -> Open Resource) can be used to search for and open non-Java files in the workspace (like .xmls and .properties), for classes you can use Ctrl-shift-T (Navigate -> Open Type). When the search window opens, just start typing file- or class name (you can also use wildcards like * and ?).
Use Alt+Shift+B if you are using Spring Tools Suite.

Spring IDE / STS : Can I get autocompletions of beans from other files?

When writing XML beans and using "ref=" everything works well as long as beans are declared in one file. But when bean is in other file, I can't get any autocompletion.
Doesn't work in Spring Tools Suite 2.8.1 or Eclipse 3.7.1 with Spring IDE plugin.
Either import the other file or create a config set that groups all the files.
Spring/Eclipse 'referenced bean not found' warning when using <import>?

editing spring xml source with eclipse/sts - missing "mark occurrences" and "jump to bean in another file"?

I'm working with Eclipse/STS and editing spring bean definition XMLs with the "Source" editor, as I dislike the GUI editors. I'm missing these features:
mark occurrences - highlight all occurrences of the word I'm standing on (the same as in the java editor)
Jump to bean (with F3 or ctrl-click), if its defined in another spring XML file
Am I missing a plugin? Should I work with the GUI editors?
Thanks, Ido
Jump to bean with F3 should work, given the project is configured properly. That is:
the project holding the Context XML definitions needs the "spring project nature"
all the XML config files need to be announced for STS: in the project properties, use the "Spring" tab and from there "beans support" and add all config files. (This results in entries in your /.settings/.springBeans, which can be shared via SCM with the other team members)
all prerequisite projects providing Java classes for those beans need to be declared as project dependencies, in case you're working with a multi module project.
Besides that, the "Spring Beans references search" is quite helpful: Place the cursor on a bean name and hit CTRL-SHIFT-g to get an outline of all references to this bean name (including the Java class and the bean definition in XML)

Spring Explorer not showing beans from bean definition file

I just downloaded the Springsource Tool Suite, and I created a simple bean configuration file called myDefinition.xml and placed it in the root directory of the Spring Project. However, the Spring Explorer is not showing any data. I'm using eclipse 3.5 and the latest SpringSource all-in-one installation.
Also, if I specify invalid property names in the configuration file, I get no error. Autocomplete, however, is working.
Thoughts?
In the Spring Explorer tab in Eclipse, right click on your project > properties. Then go to Spring > Beans Support and select the Config Files tab. You can then select files or scan a directory and that should add them. I haven't found a way to autoscan a directory so I don't have to do this whenever I add a new spring bean file. The beans that appear in that list are stored in the .springbeans file.
Just want to add that if you want to generate a diagram of a webflow of an existing project, you have to right-click said project in Eclipses Project Explorer and "Add Spring Project Nature" by using the Spring Tools entry of the context menu.
There currently (as of 2012-04-10) seem to be problems with the Spring Explorer view when using a purely annotation driven bean definition (i.e. using Java annotations to wire everything up instead of using XML files).
There is a workaround though, by providing a minimum XML file that declares scanning for components.
http://forum.springsource.org/showthread.php?118928-Spring-Explorer-with-Java-based-Web-Container-configuration
Unfortunately, this procedure is still flawed, missing some components.
Just to add Claudio030
Make sure you have JEE perspective selected for your project, otherwise it won't show in Java perspective.