Over a million developers have joined DZone.

Springtime for NetBeans and Java SE

· Java Zone

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

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.

 

The Java Zone is brought to you in partnership with JetBrains.  Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}