facebook

Enabling and Using Spring DSL

This tutorial walks you through bootstrapping a project for Spring and Spring code generation by enabling Spring DSL and using the Service Spring DSL abstraction. The skills learned in this tutorial can be easily applied to the other abstractions, as well. In this tutorial, you will learn how to:

  • Initialize a project for Spring DSL
  • Create a model package
  • Create a service and operation
  • Implement a service method
  • Enable JAX-WS and DWR

 Spring Scaffolding requires a MyEclipse Spring or Bling subscription.


1. Initialize a Project

  1. Create a MyEclipse Web project called MySpringProject.
  2. Right-click MySpringProject, and select MyEclipse>Spring DSL>Enable Spring DSL.


    Enabling Spring DSL
  3. Click Next to continue through the introduction page.
  4. Accept the defaults for enable and configuring code generation for the web layer of a Spring application, and click Next.

    The source, resources and content folders can be any folder in the “target project”. If the folders don’t exist, they will be created. In the case of the source and resources folders, they will also be configured as Eclipse source folders in the project. Since this project is a web project, the content folder is set automatically to the content folder specified in the New Web Project wizard.


    Initializing the web layer
  5. The next three pages are nearly identical to the Web Layer Generation page; however these panels are used to configure the other layers of a Spring web application. By default the other layers are also enabled, and the source and resource folders are the same as the web layer. Accept the defaults, and click Next.
  6. Accept the default project configuration options, and click Next.


    Configuration options
  7. Accept the default Spring Capabilities settings, and click Next.

    Adding Spring capabilities
  8. Accept the runtime dependency configuration, and click Next. panel is for adding Spring and dependent libraries to the current project. 

    While you can add and remove libraries from the project by hand, this page can bootstrap the project with a full set of libraries typically needed by a Spring application. In addition to deciding which libraries are added, you can also specify which version of Spring (2.5 or 3.0) that you want to use and how you want the libraries added to the current project. Spring 2.5 and the use of Eclipse classpath containers is the default.


    Runtime dependencies
  9. Click Finish on the Summary page. All the necessary source code for using the web service is generated.

    Summary page
Note: While we reviewed every panel, it’s worth noting that to bootstrap the project, you can simply accept all defaults by clicking Finish on the first page of the wizard.

2. Review the Output

With just a few clicks there was a lot done to your new web project. This section details some of the changes  to your project.

The first thing you will notice is that multiple classpath containers were added to the project. You now have all the libraries necessary for a Spring web application.

Classpath containers

If you want to see the libraries represented by a specific classpath container, expand the container to see a list of libraries. For example, if you expand the Spring 2.5 Core Libraries container, you see the list of libraries corresponding to Spring 2.5 Core. While it’s beyond the scope of this tutorial, with MyEclipse for Spring you can also customize the libraries that are in each of these classpath containers.

Container libraries – Spring 2.5 code libraries

Per the default configuration of the wizard, the generated folder was created and set up as an Eclipse source folder. This is the folder where generated Java source code is stored.

Generated folder

The resources folder was also created and set up as an Eclipse source folder. This is the location into which all application configuration files are generated.

Resources folder

You will notice that various files were generated into the resources folder. Below is a summary of the generated files.

FileDescription
hibernate.propertiesHibernate configuration file
jta.propertiesJava Transaction API (JTA) configuration file; by default generated to use Atomikos
log4j.properties

Log4J configuration file

MySpringProject-dao.properties

Data Access Objects configuration file

MySpringProject-dao-context.xml

MySpringProject-security-context.xml

MySpringProject-service-context.xml

MySpringProject-web-context.xml

A set of Spring context files for Spring configurations that are manually added by a developer.

META-INF/persistence.xml

Java Persistence API (JPA) configuration file.


The web content folder was modified to support Spring web applications.

FileDescription
appengine-web.xmlContains application ID and version and lists static and resource files
spring.tldDescribes tags in the library and contains information about the tag library
spring-form.tldDescribes library tags used in context of Java Server Pages
web.xmlAdded references to Spring servlet RequestContextListener, ContextLoaderListener, DispatcherServlet and ResourceServlet.

The Spring DSL view was added to the project, and it can be seen using the Project Explorer in the MyEclipse Spring perspective. The Spring DSL shows a hierarchical view of the project’s Spring DSL artifacts, and double-clicking it opens the editor for configuring the Spring DSL and managing Spring DSL artifacts.

Note: The Spring DSL view is not visible unless you open the MyEclipse Spring perspective.

Spring DSL view

With your Spring web project bootstrapped, now you can start  developing your Spring web application.


4. Create a Model Package

A Model Package is used for name spacing and grouping related Spring DSL components. Model packages correlate directly to Java packages, and Spring DSL artifacts created in a model package are generated into a matching Java package. Before creating a service you need create a model package. 

  1. Right-click the Spring DSL folder, and select New>Model Package.

    Adding a model package
  2. Type org.acme.services as the name of the new model package, and click Finish. The new model package is added to the Spring DSL view.

    New model package

    5. Create a Service and Operation

    A Service is used for defining service layer components and managing related files. Services get generated into multiple Java files, and the principal Java file is an @Service annotated Spring component, a specialized stereotype for service layer components. An Operation is a Spring DSL artifact that defines methods for a Service, and they are Spring DSL representations of Java methods. Operations use inputs and outputs for exchanging data, and they are generated as methods directly into the respective class.

    1. Right-click the org.acme.service model package, and select New>Service.

      Adding a service
    2. Type MyService as the name of the new service, and click Finish. The new Service is added to the Spring DSL view.
    3. Double-click the Service to open in the editor.

      The Service editor is used for configuring a specific instance of a Spring DSL service. There are various tabs at the bottom of the editor for configuring different aspects of the service.

      Service Editor
    4. Right-click MyService, and select New>Operation.

      Adding an operation to the service
    5. Type doSomething as the name of the new operation, and click Finish. The new Operation is added to the Spring DSL view, and the Operation editor automatically opens for configuring the operation. The Operation editor provides configuration options specific to operations.

      Operation editor

      6. Review Generated Code

      If you click the service in the Spring DSL view, the Generated view displays the files generated for you.

      Generated view

      By default a Spring DSL service generates a service interface, a service implementation class (annotated with Spring @Service), and a JUnit test class. All Java artifacts generated from a Spring DSL artifact are listed in the Generated view. Double-clicking a Java artifact from the Generated view opens the Java file in the editor.

      Note: The Generated view is a view in MyEclipse with Spring support,and it’s included by default in the MyEclipse Spring perspective.If you are using a different perspective, then you might need to add the Generated view to your workspace.

      As mentioned earlier, every service (DSL) generates a service implementation class, a service interface, and a service JUnit test class.

      The Spring framework supports the @Service annotation, which is a specialized stereotype for service layer components. The service implementation class is generated with aJava package name (based on model package), all relevant Spring import statements, all relevant Spring annotations (include @Serviceannotation), and Java methods for each operation created for the service.

      MyServiceImpl.java – service implementation class

      Notice the doSomething() Java method isn’t implemented. That’s where you implement the desired functionality. MyEclipse has created the skeleton code with all the required Java and Spring configurations, but the implementation code is your responsibility. You know what you want the service method to do, and you can implement the method directly in the Java code.

      Here’s the generated service interface. As you add new operations to the service, the service interface is automatically updated for you. That’s one less thing for you to have to worry about.

      MyService.java – service interface

      Here’s the generated JUnit test class. It is generated with all required JUnit and Spring boilerplate code, and only thing left for you to do is to implement the service tests. As new operations are added to the Service, new test methods are automatically added to the test class.

      MyService.java – service JUnit class

      Note: If you look at the generated service operations for a scaffolded application, you will notice they are fully implemented for you. On the other hand, when using the Spring editors to create new service operations, the Java methods are generated, but the body of the Java methods are not. That’s a fundamental difference between the scaffolding and Spring editors functionality.


      7. Implement the Service Method

      You are ready to implement the service method. There is really nothing special here. Just code the implementation of the service method generated for you.

      Open the MyServiceImpl.java file, and add some code to the service method.

      Service method code


      8. Enable JAX-WS

      By default a service doesn’t support web service invocation. The JAXWS Web Service tab is for configuring the web service options for a Service.

      1. Open the Service editor for MyService by double-clicking the MyService artfact in the Spring DSL view.
      2. Switch to the JAXWS Web Service tab, select the MyService web service, and select the Publish Web Service option. This exposes the service as a JAX-WS web service.

        JAX-WS configuration
      3. Save the file.

      When you deploy your application, the service is available as a JAX-WS SOAP web service. If you click the service in the Spring DSL view, the Generated view displays two additional JAX-WS related files generated for you.

      Generated view


        9. Enable DWR

        By default a Service doesn’t support JavaScript/JSON invocation. The DWR tab is for configuring the DWR options for a Service.

        1. Open the Service editor for MyService by double-clicking the MyService artfact in the Spring DSL view.
        2. Switch to the DWR tab, select MyService, and select the Publish option. This exposes the service via JavaScript/JSON using DWR.

          DWR configuration
        3. Save the file.

        The application has been configured to make this service available to JavaScript/JSON clients (i.e., AJAX applications).

        Note: There is a DWR runtime issue caused by a library version mismatch for a dependent Java library. Please see the MyEclipse for Spring Forums for a very easy solution.