FeaturesPluginsDocs & SupportCommunityPartners

Introduction to the Spring Framework in NetBeans IDE


This document shows you how to install the Spring Framework and run it in NetBeans IDE. The Spring Framework is a popular open source application framework that can make J2EE development easier. It consists of a container, a framework for managing components, and a set of snap-in services for web user interfaces, transactions, and persistence.

This tutorial takes advantage of Craig MacKay's Spring NetBeans Module, and documents the flash demo he has prepared that guides you through the installation process. At the end of this tutorial, you will have a module installed in your IDE that provides the following support for the Spring Framework:

  • Framework Support (enables the Spring Framework as an option when creating a new web application)
  • Spring XML Catalog (provides Spring related XSD and DTD's to enable XML code completion)
  • Integrated Library Management (installs libraries required for the Spring Framework, as well as additional required libraries)

For more information on the Spring Framework, visit http://www.springframework.org. For a more fine-grained explanation of how Spring Framework artifacts behave and interact with other objects in an application, visit the official Spring Framework Reference Documentation, or consult the Spring Framework API.

For details on the Spring NetBeans Module, see http://spring-netbeans.sourceforge.net. If you are familiar with the Spring Framework, you are welcome to contribute to the development of the Spring NetBeans Module.

Note: This document uses the NetBeans IDE 5.5 Release. The current release is NetBeans IDE 6.1, which includes the Spring Framework. If you are using NetBeans IDE 6.1, see Introduction to the Spring Framework.

Expected duration: 20 minutes


The following topics are covered below:


Getting the Software

Before beginning, make sure you have the following software installed on your computer:

  • NetBeans IDE 5.5 (download)
  • Java SE Development Kit (JDK™) version 5.0 or higher (download)
  • The Spring NetBeans Module (download)

Notes:


Installing the Module

Begin by installing the Spring NetBeans Module in the IDE:

  1. Launch NetBeans IDE and choose Update Center from the Tools menu. The Update Center wizard opens. Under Select Location of Modules, select the Install Manually Downloaded Modules (.nbm Files) option. Click Next.
  2. In Step 2, Select Modules to Install, click the Add button located to the right of the window, and browse to the location where you downloaded the module. Select the module, click OK, then click Next.
  3. Notice that Spring Framework Support is listed under the Include to Install pane:

    Spring Framework Support listed under the Include to Install pane

    Click Next.
  4. Accept the license agreement that displays, and the Update Center begins automatically downloading the module into the IDE. Upon completion, click Next.
  5. In Step 4, View Certificates and Install Modules, click the Include checkbox to the left of the Spring Framework Support. When the Unsigned Module alert box pops up, simply click Yes - this module can be trusted. Then click Finish.
  6. Finally, when the Restart the IDE window displays, make sure the Restart the IDE option is selected and click OK. The IDE needs to be restarted in order for changes to take effect.

Creating a New Project

Now that the module is installed, you can create a new project for a web application using the Spring Framework:

  1. Choose New Project (Ctrl+Shift+N) from the IDE's File menu. Under Categories select Web. Under Projects select Web Application. Click Next.
  2. In Project Name, type in SpringWebApp. From the Server drop-down list, select the server you plan to work with. Leave all other settings at their defaults and click Next.
  3. In Step 3, the Frameworks panel, notice that we just installed the Spring NetBeans Module, Spring Framework 2.0 is now listed:

    Spring Framework 2.0 displayed in the Frameworks window of the New Web Application wizard

    Select the corresponding checkbox and click Finish. The IDE creates a project template for the entire application, including all metadata, as well as the project's Ant build script. You can view the template structure from the Projects window. Also note that the project's top level index.jsp file, provided as an entry point to the application, opens by default in the Source Editor.

Running the Project

Let us see what happens when we try running the project in the IDE:

  • Right-click the project node and choose Run Project (alternately, press F6). The IDE automatically starts your server if it is not already running, and deploys the application to it. The application runs and employs the Spring MVC DispatcherServlet using the configuration data contained in dispatcher-servlet.xml. Note any output displayed in the IDE's Output window (Ctrl+4). The generated output should complete with a BUILD SUCCESSFUL message:

    Output window displaying information when running the project

    The IDE's default browser starts up, and you see the generated output of the welcome page view (/WEB-INF/jsp/index.jsp):

    index.jsp output in browser

Browsing the Project's Structure

Now that you know the project can be deployed and run successfully, look at how the Spring Framework is integrated in the project's structure:

  • In the Projects window, completely expand the Web Pages > WEB-INF node. Note that the following Spring Framework artifacts have been created for you:

    view of project structure in Projects window

    Spring Framework artifacts:

    classes/ehcache.xml:
    allows fine-tuning of ehcache settings, which might be needed for persistence-related features (e.g. if ehcache is used with Hibernate).

    classes/log4j.properties:
    reads the log4jConfigLocation context parameter and reads the log4j configuration specified as the value. This is useful during development and allows you to fine-tune log output.

    jsp/: contains JSP pages used to render views for the application.

    tld/: contains tag library definition files

    applicationContext.xml:
    defines beans which make up the application context for the framework and provides their availability to all dispatcher servlets.

    dispatcher-servlet.xml:
    defines beans used for specifying controllers and Spring MVC related features.

    jdbc.properties:
    contains configuration data which can be applied to values of other XML documents.

    web.xml:
    defines application listeners and paths to XML files that specify beans.

  • Expand the Libraries node. Note that the Spring Framework JAR files have been included in the new project:

    Spring JAR files listed under the Libraries node






    Note: From the Libraries node, you can also add new libraries and JAR files. To do so, choose the appropriate action from the Libraries node's right-click menu.

    To remove any JAR files, choose Remove from the right-click menu of a selected JAR file. Alternatively, you can make any changes to libraries by accessing the Project Properties dialog (choose Properties from the right-click menu of the project node, then under Categories, select Libraries).

You can now begin developing the application according to your needs. For example, you can start modifying the application context (application-context.xml) to add new services and DAO objects, or add new controllers to the application's dispatcher servlet (dispatcher-servlet.xml).


Next Steps

This concludes the Introduction to the Spring Framework in NetBeans IDE tutorial. This document demonstrated how to install the Spring Framework into NetBeans IDE, and introduced you to the IDE's interface for developing in the framework.

You are encouraged to continue learning about the Spring Framework by working through other tutorials in NetBeans IDE, such as Thomas Risberg's Developing a Spring Framework MVC Application Step-by-Step tutorial.

For related and more advanced tutorials on netbeans.org, see the following resources:



top


Bookmark this page

del.icio.us furl simpy slashdot technorati digg
Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Virtual Box - full virtualizer  Open ESB - The Open Enterprise Service Bus Powered by