Geertjan is a DZone Zone Leader and has posted 437 posts at DZone. You can read more from them at their website. View Full User Profile

Springtime for NetBeans and Java SE

03.23.2008
| 5856 views |
  • submit to reddit

The Spring Framework is useful in multiple contexts. Typically, its usefulness in the area of web development is referred to as a starting point. However, let's create a small "hello world" application within the Java SE (i.e., desktop) environment. For these purposes, here I walk through a small introductory encounter with Spring through its Util schema.

In this context, I used NetBeans IDE 6.1 Beta, since this version of NetBeans IDE sports a set of features specifically for Spring, for the first time. In addition to its support for web applications, this set of features is also applicable to Java SE applications. To get started, here's my first Spring configuration file in a Java SE application. The Util schema gives me some collections classes that I can access from my Java code. In this case, I define a very simple list containing e-mail addresses:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">

<util:list id="emails">
<value>john@smith.org</value>
<value>jack@harry.org</value>
<value>peter@piper.org</value>
<value>pavel@prochazka.org</value>
</util:list>

</beans>

To help me while coding the above file, I have context-sensitive code completion to help me, throughout my XML tags:

And here's how I created the file itself, i.e., using a new template...

... which also lets me choose one or more namespaces, which results in the tags generated at the top of the file, so that I don't need to think about the header of the Spring configuration file at all:

Here's my simple Java class for accessing (and using) the above Spring configuration file:

package hellospring;

import java.util.ArrayList;
import java.util.Iterator;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class Main {

public static void main(String[] args) {
BeanFactory factory = new XmlBeanFactory(new FileSystemResource("src/hellospring/demo.xml"));
ArrayList list = (ArrayList) factory.getBean("emails");
Iterator it = list.iterator();
int count = 0;
while (it.hasNext()) {
count = count + 1;
System.out.println("Email " + count + ": " + it.next().toString());
}
}

}

Above, I use the Spring JARs to access the bean I defined in the Spring configuration file and then I treat the result exactly as I would treat any other ArrayList. In doing so, I am able to use Java code completion in my Spring configuration file, whenever I need it, specifically for class attributes:

And then I can simply Ctrl-click the class references, which then results in the class being opened in the editor:

Finally, I can organize my Spring configuration files. On the Project Properties node of Java SE applications, there's a new node that appears when the Spring JARs are on my classpath, for grouping my Spring configuration files:

Finally, for more info on Spring schema-based configuration, click here. And, if interested in the new Spring support in NetBeans IDE 6.1, see Improved Spring Framework Support in NetBeans 6.1: XML-Config Files in Ramon Ramos's blog.

Update: Also see Extending the NetBeans IDE Spring support.

 

Published at DZone with permission of its author, Geertjan Wielenga.

Comments

Kristian Rink replied on Wed, 2008/03/26 - 2:41am

Thanks for pointing this out in detail. Indeed, people have done quite a good job introducing Spring support to NetBeans, even though it is not completely on par with SpringIde/Eclipse. Pretty impressive for a first version, nevertheless.

Geertjan Wielenga replied on Wed, 2008/03/26 - 2:57am

I haven't covered everything in this article, not even close, though. There's much more support in the web tier than there is in the Java SE space. If there are specific things you're missing, please feel free to say so!

Kristian Rink replied on Thu, 2008/03/27 - 1:51am in response to: Geertjan Wielenga

Well so far I consider the things I have seen to be right the kind of tooling I'd like to have for my Spring projects - simple features that make everyday life easier in terms of wiring applications, features mainly related to the applicationContext.xml files. Still I am curious however what other features this tooling is likely to provide, gonna give it a more in-depth test drive later. A perfect solution of course would allow for using Spring, maven2, the NetBeans EJB tooling (which is excellent IMHO) and glassfish homogenously without losing features while throwing in one or the other technology... ;)

cheers,

Kristian

Comment viewing options

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