Chapter 1. Spring Generation

Table of Contents

1. Generating Spring applications using meta-data programming and code generation

1. Generating Spring applications using meta-data programming and code generation

PROBLEM

The Spring Framework is one of the most popular JAVA frameworks. While the Spring Framework and associated Spring technologes reduce complexity, developing Spring applications requires familiarity with Spring development concepts and configuration.

SOLUTION

The solution is to provide an abstraction layer on top of Spring that generates Spring-based artifacts and configuration files, and additionaly use modelling to implement parts of your solution. Skyway Builder is an Eclipse-based, code generation tool for accelerating the development of Spring applications, specifically Rich Internet Applications (RIAs) and Web Services. Spring provides a framework that abstracts the complexities of enterprise software, and Skyway Builder adds an even higher layer of abstraction (a Spring DSL) to provide a generation framework for Spring.

HOW IT WORKS

The abstraction is accomplished by employing a metadata model to capture the specifications from the developer, which in turn drives the code generation process. For defining the specifications, Skyway Builder provides Eclipse-based tooling which includes the use of editors, wizards, and modeling artifacts. The Skyway Generation Framework then converts the metadata into generated code and Spring application artifacts.

Skyway Builder implements a DSL for Spring that describes all the major components of a Spring application. This is the metadata model:

Figure 1.1. Spring DSL Model

Spring DSL Model

Application development using Skyway Builder is accomplished by using (assembling and configuring) Spring DSL artifacts:

Within Skyway Builder, there are graphical (and abstract) representations of application components. These components have direct correlations to Java, Spring, JPA, and JEE artifacts.

Summary of Spring DSL Artifacts

  • Projects are the top level artifact within the developer's workspace. All other Spring DSL artifacts must be made within a Skyway modeling project.

  • A Model Package is a Spring DSL artifact for namespacing and grouping related Spring DSL components. A model package can contain any top-level Spring DSL elements, including other model packages. Model packages correlate directly to java packages (and UML packages), and Spring DSL artifacts created in a model package will be generated into a matching Java package.

  • A Domain Object is a Spring DSL artifact that defines the domain model of the application. By default a domain object is generated into a plain old java object (POJO). If a domain object is associated with a data access object, then the domain object is annotated as an @Entity (JPA) annotated class, and it's associated with a primary key class (@IdClass). Fields can be added to domain objects by using the basic data types, and a domain object can have relationships to other domain objects.

  • A Data Access Object (DAO) is a Spring DSL artifact used to separate data access logic from application logic. A DAO manages the persistence of domain objects, and it is generated into @Repository annotated Spring components, a specialized stereotype for data access layer components. A DAO can also contain predefined queries called Named Queries.

  • A Named Query is a Spring DSL artifact creating predefined queries for the data access objects (DAOs). Named queries are defined using either SQL or JPQL (Java Persistence Query Language), and Named Queries are generated into @NamedQueries and @NamedQuery annotations in Spring @Repository components.

  • A Service is a Spring DSL artifact that defines a service layer component, and it gets generated into a @Service annotated Spring component, a specialized stereotype for service layer components. A Service contains a set of Operations which are generated into Service methods.

  • A Web Controller is a Spring DSL artifact that defines the web layer of an application using Spring MVC. Web Controllers get generated into a @Controller annotated Spring component, a specialized stereotype for web layer components. A Web Controller contains a set of Operations which are generated into methods for handling web requests.

  • An Operation is a Spring DSL artifact that defines methods for both Services and Controllers. Operations represent application logic, and they use inputs and outputs for exchanging data. Operations are generated directly in the respective Service and Controller classes, and they are generated slightly differently to account for the different implementations needed for the Spring stereotypes.

  • An Actions is a Spring DSL artifact for implementing operations using model-driven development. They are used to implement service and controller operations in Java using drag-n-drop steps that are sequenced together into functionality.

  • A Component is a Spring DSL artifact that defines a generic container for data. A component gets generated into an @Component annotated Spring component, a generic stereotype for Spring managed components with configurable scope. Components are typically used as data transfer objects (DTO) and form backing objects.

  • A Flow is used to implement the web layer of an application using Spring Web Flow.

  • Java Server Pages (JSP) are used to implement the Presentation layer of an applicaiton. JSPs are used for both Spring MVC and Spring Web Flow applications.

  • An Exception is a Spring DSL artifact that defines custom exceptions. Exceptions are generated into exception classes in Java.

The follow table shows the relationship between the Spring UML stereotypes, Spring DSL, Spring/JPA components.

Table 1.1. Spring/Java, Spring DSL, and UML Stereotype Cross Reference

 Web LayerService LayerData Layer

Spring Stereotypes, Annotations and Interfaces

@Controller

@Component

@RequestMapping

@RequestParm

@ModelAttribute

Spring Validation

@Service

@Transactional

@Repository

Java Annotations and Interfaces

@WebService (JAX-WS)

@WebMethod (JAX-WS)

@WebParm (JAX-WS)

Direct Web Remoting - DWR

Exceptions

@Entity (JPA)

@Id / @IdClass (JPA)

@NamedQueries (JPA)

@NamedQuery (JPA)

@XMLType (JAXB)

@XMLElement (JAXB)

Spring DSL

Web Controller

Component

URL Mapping

Service

Exception

Domain Object

Data Access Object

Named Query

UML Stereotype for Spring

<<WebController>>

<<Component>>

<<RequestMapping>>

<<ModelAttribute>>

<<Validator>>

<<BusinessService>>

<<TransactionalOperation>>

<<Exception>>

<<DomainObject>>

<<Id>>

<<DataAccessObject>>

<<NamedQuery>>