Monday, March 24, 2008

Spring support in Netbeans IDE 6.1 Beta

(This post expects some knowledge of Spring Framework, writing beans configuration file and using Spring Framework. This post highlights the support for Spring Framework 2.5 in Netbeans IDE 6.1 Beta)

Netbeans IDE 6.1 Beta support Spring Framework 2.5 out of the box. The support is not much for a Java SE console application but we can still see what Netbeans offers for Spring Framework support.

I will start with a new Java SE console application through the new project wizard. I will name my project SpringDemo and will fill the appropriate fields of the Name and Location dialog box.

Netbeans IDE created a project for me with SpringDemo as my main class with a boiler plate main method.

The project yet do not contain any support for the Spring Framework 2.5. I will open the project properties dialog box by choosing the File menu "SpringDemo properties" option.

I will have to choose the Libraries node to add Spring libraries to the project.

After clicking the Add Library button Netbeans offers a list of libraries and we can see Spring Framework 2.5 Library is already available in this version of Netbeans IDE.

I chose the Spring Framework 2.5 Library and it is shown in the Compile Time Libraries of my project.

We can see the library references in the Projects panel now in the Libraries node.

Now I will add one spring beans configuration file. Spring work either with XML configuration file or Java annotations. I will use a beans configuration XML file for this project.
I right clicked on the Source Package node and chose New > Other option.

In the Choose File Type dialog box I chose the XML node and XML Document entry from the file type list.

I named the file as demo-beans. The Netbeans wizard automatically appended the XML extension and also showed me the path where this file will be stored. I have kept the file in the source folder directly so I can use this with a relative path and class path loader in Spring.

Netbeans IDE offers many options for the XML generation. I will choose the XML schema option which will need the URL of the Schema. I know the schema URL for Spring Framework 2.5 is

I provided the schema URL for the Spring Framework 2.5 from Spring Framework site. I selected the beans element as the root element and spring as prefix for the namespace.

I used default fields for the XML Model Generator Options.

Now I could see the demo-beans.xml file in the Project panel. There is a spring style leaf decoration to the icon for the file demo-beans.xml. This shows that the Netbeans IDE has identified this file as a Spring Configuration File.

I will create two classes to check the Spring support. First class will be a Developer class with just two private fields name and experienceYears. I will generate the setters and getters for these fields from the Netbeans IDE Source generate setter and getter option.

The second class I will write is SoftwareCompany. I will keep two Developer reference variables in this class. My intentions are to create Developer as well as SoftwareCompany objects through the demo-beans.xml spring configuration file.

I will create the BeanFactory by using the XmlBeanFactory and ClassPathResource classes and will try to fetch the software company object from the spring API. This will need a properly configured demo-beans.xml spring configuration file.

With all the classes written the Projects panel shows like this. Now the only task remaining is to write the demo-beans.xml spring configuration file. To provide classpath support in the spring bean configuration file I must tell Netbeans IDE about the existence of the demo-beans.xml file and ask it to use it as spring bean configuration file.

To configure the spring files I opened the project settings dialog box again.

Now I can see the Spring Framework node in the Categories tree. When this node is selected Netbeans presents configuration files section. I will use the Add File button to configure beans configuration file.

Now Netbeans IDE is ready to provide me support for the auto completion of class paths also in my demo-beans.xml file. As it is a XML file support for XML schema was already there. Now with the setting I just did there is support for completion of class names in the editor as well.

First I will have to remove the spring: prefix which I applied while creating the XML file. Netbeans IDE applies a prefix ns1 by default. I typed spring in place of ns1 so the XML file has spring: prefix applied. I will just remove that prefix as I have found the spring support is not getting applied with the prefixed version of XML file.

After the prefix is removed the file will look like:
To see to auto completion support I started typing the first bean element. The XML editor provided me support for completing the bean element as well as writing the id="developer" path. The real support can be seen when I started typing the class name for the class attribute. Now the IDE is providing me the completion for Java class found in my project.

I completed the beans configuration file with the following contents. There is one developer bean defined. There is a software company bean defined with one reference bean and one direct bean. The usual stuff we always do in the spring configuration files.

Now when I build the project and run the main program I could see the output of the program.

The output was as expected and the Netbeans has provided me support for writing the Spring Beans Configuration file. The support for visualization of these files is still not there. The dist folder now contains JAR for my project as well as a lib folder with the required JAR files which must be shipped with my JAR. The lib folder contains commons-logging and spring-2.5 JAR files.

There are some issues with the Spring support also. When I tried to add a configuration file from an Eclipse imported project Netbeans does not recognize the file as configuration file. There must be some problem with the recognition logic. I must report this as an issue in the Netbeans Issuezilla. I will post the procedure to post a issue in Netbeans issuezilla here next time.

with regards
Tushar Joshi, Nagpur


  1. Hi Tushar,

    Would you like to contribute this to Community Docs?

    Reply soon.

    Varun Nischal
    NetBeans Community Docs
    Contribution Coordinator

  2. Hi Tushar,

    Thanks for your help here about Spring.Can you share about Hibernate also? Please, If you have any documents or any helpful docs about Hibernate in NetBeans IDE 6.5... Can you share with me please ??

    My name is Venu,
    my Id :

    Once again thanks for your sharing about spring.

    Thanks & Regards

  3. Hi Venu,

    Thanks for the comment. I will surely blog about the Hibernate support here till then I suggest you watch this video tutorial:

    this JPA tutorial

    and this wiki page

    with regards
    Tushar Joshi, Nagpur

  4. hi joshi,
    your tutorial is very useful. but i get the following error. can also provide me a way to download your sample code
    thanks and regards

  5. Sorry the error is
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - cannot find symbol
    symbol : class BeanFactory
    location: class

  6. Hello Tushar,

    This blog is really useful for me ,
    before reading this i was supossing that spring can be used with only with web applications only it turned my view, also it improved my knoledge of netbeans too.

    Thanks for the post :).


  7. Hello Tushar! i follwed ur XML configuration file but its not working in my netbeans IDE6.5.1.
    pls tell me soon

  8. hi Joshi,
    its very useful to learn Spring for the beginers. The above said program provide the output in browser or else. how can i get the output for the above project? can u give me
    some more theoretical intro to Spring.

  9. hi joshi nice job,
    i am new to spring.
    ur way of presentation is very good i ever seen before in other sites.

    give any suggestions, good web links to learn spring.