MyEclipse Logo

MyEclipse for Spring 10.0: Spring DSL Tutorial (Part 2 of 2) - Use the Spring DSL

How to use Spring DSL editors for developing Spring application components

Niel Eyde

Skyway Software, Inc.

Table of Contents

1. Introduction
2. Goal
3. Prerequisites
4. MyEclipse Spring Perspective
5. Create a Model Package
6. Create Service and Operation
7. Review Generate Code
8. Implement Service Method
9. Enable JAX-WS SOAP
10. Enable DWR
11. Conclusion
12. Additional Developer Resources

1. Introduction

MyEclipse for Spring offers many ways to accelerate Spring web application development, including Bootstrapping gets your project setup with dependencies and configurations, Scaffolding generates full (or partial) web applications that are SpringSource certified and adhere to Spring best practices, and Code assistants assist with Spring/JAX-WS annotation-based programming. These features are covered in separate tutorials, and they are for developers that want the source code to be the primary development artifact and prefer code-oriented development accelerators.

MyEclipse for Spring also has a feature that is geared toward developers that want to accelerate Spring application development by using an abstraction layer (model-driven development). The abstraction layer is cumulatively referred to as a domain specific language, or DSL, and the Spring DSL defines eleven abstractions. Each Spring DSL abstraction manages/generates one or more Spring web application source artifacts (Java code or XML configuration files), and each abstraction has its own dedicated editor for configuring instances of the abstraction.

During the course of application development using the Spring DSL, a developer will add instances of relevant Spring DSL artifacts to their project. They will use the respective editors to configure the artifacts according to their requirements, and MyEclipse for Spring will generate the source code according to the configuration. This offers tremendous speed and agility to application development because the developer doesn't have to get caught up in the details of Spring or keeping related artifacts synchronized. This is all accomplished without limiting the developer from maintaining the application artifacts using the standard Eclipse Java editors or XML editors. Some of the abstractions (i.e domain object, DAO) can be used to generate fully implemented application components that are ready-to-run and don't require additional development. Other abstractions (i.e. controller, service, component) are focused on skeletal generation and configuration for Spring, and the actual implementation of the generated Java classes and Java methods is done via Java coding.

2. Goal

This tutorial is going to walk you through using one of the Spring DSL abstractions called Service. The Service will be created and configured using the Spring editors, and the Service abstraction will be generated into multiple Java files, including skeleton code that will be further developed using the standard Java editing capabilities of MyEclipse. Finally the Service will be enhanced to support SOAP (JAX-WS) and Javascript/JSON (DWR) access.

While this tutorial is only focusing on one of the eleven abstractions in the Spring DSL, the skills learned in this tutorial can be easily applied to the other abstractions. Refer to the MyEclipse for Spring reference guide for a list of all Spring DSL artifacts and detailed descriptions of associated editors.

3. Prerequisites

The prerequisites needed to complete this tutorial are:

  • Download MyEclipse for Spring 10.0

  • MyEclipse Web Project - enabled with Spring code generation capabilities (see Project Initialization tutorial)

4. MyEclipse Spring Perspective

This tutorial is going to be performed using the MyEclipse Spring Perspective. While this perspective is optimized for using the Spring DSL and Spring Editors, all the capabilities are also available in other perspectives.

5. Create a Model Package

A Model Package is used for namespacing and grouping related Spring DSL components. Model packages correlate directly to Java packages, and Spring DSL artifacts created in a model package will be generated into a matching Java package.

Before creating the service you need create a model package. When Spring code generation capabilities were added to the MyEclipse project, a Spring DSL view was added to the project that is visible from the Eclipse Project Explorer. This element shows a hierarchical view of the project's Spring DSL artifacts, and it can be used for interacting with the Spring DSL.

  1. Right-click on the Spring DSL, and choose New > Model Package

    Figure 1. New > Model Package


  2. Using the New Model Package wizard, specify org.acme.services as the name of the new model package, and click Finish.

  3. The new model package will be added to the Spring DSL view.

    Figure 2. Spring DSL View with new Model Package


6. Create 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 on the org.acme.service model package, and select New > Service

    Figure 3. New > Service


  2. Using the New Service wizard, specify MyService as the name of the new service, and click Finish.

  3. The new Service will be added to the Spring DSL view, and the Service editor will be automatically opened for configuring the service.

    Figure 4. Spring DSL View with new Service


    Figure 5. Service 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.

  4. Add an operation to the Service, by right-clicking on MyService, and select New > Operation

    Figure 6. New > Operation


  5. Using the New Operation wizard, specify doSomething as the name of the new operation, and click Finish.

  6. The new Operation will be added to the Spring DSL view, and the Operation editor will be automatically opened for configuring the operation.

    Figure 7. Spring DSL View with new Service


    Figure 8. Operation Editor


7. Review Generate Code

Now let's review what was generated for you.

  1. If you click on the Service in the Spring DSL View, the Generated view will show you all the files that were generated for you.

    Figure 9. Generated View


    By default a Spring DSL Service will generate a Service interface, a Service implementation class (annotated with Spring @Service), and a JUnit test class. All Java artifacts that are generated from a Spring DSL artifact will be listed in the Generated view.

  2. Double-clicking on a Java artifact will open the Java file in Eclipse's Java editor.

    Figure 10. Generated View


  3. Now let's briefly examine each generated files. 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 a Java package name (based on Model Package), all relevant Spring import statements, all relevant Spring annotations (include @Service annotation), and Java methods for each operation created for the service.

    Figure 11. MyServiceImpl.java - Service Implementation Class


    You will notice that the doSomething() Java method isn't implemented. That's where you implement the desired functionality. MyEclipse for Spring 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 will be automatically updated for you. That's one less thing for you to have to worry about.

    Figure 12. MyService.java - Service Interface


    Here's the generated JUnit test class. It is generated with all the 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 will be automatically added to the test class.

    Figure 13. MyServiceTest.java - Service JUnit Class


8. Implement Service Method

Next we're going to implement the service method. There's is really nothing special here. Just code the implementation of the service method that was generated for you.

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

    Figure 14. Method implementation


You're done. You have a fully implemented Spring @Service. If that's all you needed, you could stop here. However I encourage you to take a look at some of the other cool stuff you can do with MyEclipse for Spring.

9. Enable JAX-WS SOAP

By default a Service doesn't support web service invocation, but next we're going to add JAX-WS support to MyService. The JAXWS Web Service tab is used for configuring the web service options for a Service.

  1. Open the Service Editor for MyService by double-clicking on the MyService artfact in the Spring DSL view.

  2. Switch to the JAXWS Web Service tab, and check the Publish Web Service option.

    Figure 15. JAX-WS configuration


    That's it, your done! When you deploy your application, the service will be available as a JAX-WS SOAP web service.

  3. If you click on the Service in the Spring DSL View, the Generated view will show you that two additional JAX-WS related files are now generated for you.

    Figure 16. Generated View


    .

10. Enable DWR

By default a Service doesn't support Javacript/JSON invocation, but next we're going to add DWR support to MyService. The DWR tab is used for configuring the DWR options for a Service.

  1. Open the Service Editor for MyService by double-clicking on the MyService artfact in the Spring DSL view.

  2. Switch to the DWR tab, and check the Publish option.

    Figure 17. JAX-WS configuration


    That's it, your done! The application has been configured to make this service available to Javascript/JSON clients (i.e AJAX applications).

11. Conclusion

As mentioned earlier this tutorial is only focused on Service, one of the many Spring DSL abstractions. You've now had first hand experience with using the Spring DSL and associated editors to rapidly create a Spring service and associated files. You've also configured the Service to be accessible via SOAP using JAX-WS and Javascript/JSON using DWR.

While this tutorial focused on creating services and operations using the Spring Editors, it's perfectly acceptable to create or modify Services using the Eclipse Java editor. Any changes you make to the Service will be automatically synchronized with the DSL. You may find that you prefer doing certain things with the Spring editors and other things with Java editors, and that is supported by MyEclipse for Spring.

What was your experience with this tutorial? Share your experience with us by completing a very brief survey.

12. Additional Developer Resources

Thank you for you interest in MyEclipse for Spring. If you are interested in learning more, the following developer resources are available: