IntelliJent Spring

One of the most impressive features of IntelliJ IDEA has been its support for the Spring Framework. IDEA 7 had great Spring support, and IDEA 8 expanded it to include Spring 2.5. What makes Spring a dream with IDEA is how well it understands the Spring grammer and configuration files. I’ll give some basic samples so you can see what I’m talking about.

Let’s start with some very basic beans implemented as interfaces and implementation classes. They are:

This gives us a very basic interface for a Car and simple implementation. Not quite HelloWorld, but close. The Driver interface and implementation look like this:

Again, a very simple implementation. As you have probably guessed, we’re going to define two beans and inject the Car implementation into Driver. Just to make things interesting, we’ll also use a properties file so we can do some token substitution. Our file, called driver.properties, looks like this:

Now lets get to work and implement the Spring XML configuration file. With the above classes implemented, one of the great features of IntelliJ is great support for the p: namespace. IntelliJ has auto-completion for the beans properies, as you can see in this image:

As you can see, since we gave the name of the implementation class, the parser picked up the names of the setters and made them available via the p: namespace. Of course, there is also full auto-completion of the class name, so no copy-paste is needed.

The same auto-completion is available for properties in the imported properties file. Here is the import statement and the auto-complete pop-up as we’re filling in the property for the Driver’s name:

As you can see it enumerated both the property names and their values from the properties file configured in the property-placeholder.

The Spring support also extends from the back source to the configuration. With the Spring XML configuration file in the project, IntelliJ will detect it and provide hints in the code for Spring beans, including quick navigation between the code and the configuration. Here is what the Car interface now looks like with the Spring configuration:

As you can see, IntelliJ puts a “bean” marker in the left margin next to classes or interfaces declared in the Spring configuration file. Clicking the marker will open the Spring configuration file, highlighting the configuration line for the bean. You’ll also notice the little “I” in a circle. This is a basic IntelliJ feature which means a class has implemented the method and can navigate directly to the implementation. If multiple classes implement the interface, you can choose which one to navigate to, like this:

Clicking the little “I” next to the line declaring the Car interface brings up a dropdown allowing you to pick which implementing class you want to navigate to. The same applies for the method declarations.

Spring also has quick navigation of bean properties. For example, in my Spring configuration file, I have declared my car bean like this:

If we take a look at the class file for CarImpl, you’ll see that IntelliJ has placed a marker next to the setter methods to indicate they are Spring properties being set in the configuration file:

Clicking the property marker takes you directly to the definition of the property in the configuration file. The reverse is also true. Using CTRL + click on a property in the configuration file takes you to the implementation in the class file.

It is all this goodness that makes working with Spring such a pleasure in IntelliJ IDEA 8, and this only scratches the surface of how powerful a code editor it really is. Give it a try and enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *


*