facebook

Spring Scaffolding Support in MyEclipse


1. Scaffolding Applications with Spring

One of the goals of MyEclipse with Spring support is to help developers write less code from scratch and use code generation more broadly, and MyEclipse offers the richest set of code generation options available anywhere. MyEclipse can generate software components for Spring MVC, Spring Web Flow, Spring Security, REST, JAX-WS, Adobe Flex, Google Web Toolkit (GWT), JPA, DAO, iPhone, and JSF 2.0/Primefaces. As an alternative to writing software components from scratch, MyEclipse helps developers generate software components that are specific to their project. In many cases the software components may be used as-is, or they may be used as a starting point for further development. Furthermore the developer can edit the MyEclipse templates to further customize how the software components are generated.

Note: Spring scaffolding requires a MyEclipse Spring or Bling subscription.

MyEclipse with Spring Scaffolding support generates within a matter of minutes a Spring application that includes:

      • A JPA entity corresponding to domain model
      • A DAO for managing the JPA entity
      • Finder methods (JPA named queries) in the DAO based on domain  model fields
      • A service with fully implemented CRUD operations for managing  domain model
      • A controller with fully implemented request handlers for  supporting web application
      • Spring annotations and configuration files
      • CRUD JSP pages using Spring Form tag library and JSTL
      • Layout managed user interface using Sitemesh
      • Client-side validation implemented Spring JS with DOJO
      • CSS for UI styling
      • JUnits for every service and controller
      • SpringSource certified code and configuration files
      • Generated code that follows Spring Recipes

1.1 Spring Security

Spring Security provides a set of security  services that integrate with Spring-based applications, and MyEclipse makes it easy to bootstrap an application with Spring Security  support. The Scaffold Spring Security wizard is designed to guide you  through the bootstrapping process. In addition to selecting a security  realm, you can also specify which services and URLs need to be  secured.

    • Database – using a DB security realm configured to use a scaffolded set of database tables (users and authorities)
    • LDAP – using LDAP security realm
    • In-Memory – using an in-memory security realm

The goal of the scaffolding wizard is to get you set up and running on the most commonly used security realms. However, Spring Security has support for many more security realms/options than are supported by the Spring Security wizard. If your application requires one of the supported security realms, then the wizard does most (if not all) of the bootstrapping and configuration work.

If your application requires a security realm this isn’t supported by the scaffolding wizard, then the wizard can still help you with basic Spring Security bootstrapping, and the detailed configuration will be left to you to complete. In this case select a similar realm, and then modify the boostrapped configuration (see the security context file) with other configurations. For example, say you want to use Open-ID, which isn’t supported by scaffolding wizard. Select the In-Memory option from the scaffolding wizard, and then you can tweak the security context file with Open-ID support.


2. CRUD Scaffolding

Scaffolding refers to the superset of MyEclipse’s broad code generation functions, from generating a specific set of requested software components to generating fully implemented ready-to-run applications. Besides the obvious benefits of reducing application development effort and sparing developers from some of the chores of development, scaffolding is also frequently used for rapidly developing functional prototypes, learning new technologies, and application modernization projects.

The ability to quickly generate a full application is very beneficial to prototyping because you can iterate on a domain model and regenerate a functional prototype in minutes. The prototype can be used with business analysts as a tool for elaborating requirements and as the starting point for development. MyEclipse helps many developers learn new technologies by generating contextual reference projects, which are personalized projects that are based on their own data model. This is a considerably better approach than hunting for sample projects that are often incomplete, don’t follow modern best practices, and are based on trivial data models. MyEclipse can also be used to jumpstart an application modernization effort by leveraging application artifacts from the current system for generating a significant portion of the application components for the new system.

The MyEclipse Spring scaffolding function uses the Create-Read-Update-Delete (CRUD) application pattern for generating applications that allow end users to manage application data. While CRUD isn’t the only application pattern, it’s fairly typical. Not all web applications are satisfied solely by the CRUD application pattern; however developers find that the generated application artifacts lend themselves to being easily reused, customized, and extended. This is particularly true when the components of scaffolded applications conform to industry best practices.

CRUD applications are tied to a domain model used as the input into the scaffolding engine. The domain model can exist in many forms, and the Spring Scaffolding wizard supports the use of Java beans, JPA entities, database tables, and Spring DSL Domain Objects as inputs. The output of scaffolding is a web application implemented with Spring MVC, Spring Web Flow, JavaServer Faces (JSP 2.0), Adobe Flex, Google Web Toolkit (GWT), and/or iPhone web clients.

MyEclipse Spring CRUD scaffolding


2.1 Spring MVC

Spring MVC is a web framework from the creators of the Spring. While it isn’t the only Spring-based web framework, it is one of the mostly commonly used web frameworks.

Spring MVC supports multiple approaches for implementing the web layer, and Spring 2.5 added support for an annotation-based programming model. The annotation-based approach has quickly become the predominant way of using Spring MVC, and it’s generally regarded as the best practice in Spring MVC development. The scaffolding functions of MyEclipse generate the web layer using the annotation-based programming model.


2.2 Spring WebFlow

Spring Web Flow is a project from Spring for simplifying the development of web applications. While Spring Web Flow is compatible with Spring MVC, Spring Web Flow uses flows instead of controllers for implementing the web layer of an application. Since Spring Web Flow has the ability to manage application states, it is best suited for situations where the activity being performed by the end-user spans multiple page requests; all the pages need to be treated as a unit of work.

Flows are a group of related steps for accomplishing a task in an application. Some of the benefits of using Spring Web Flow are:

  • XML-based flow definitions – The flow of the application is defined in XML. No custom Java is needed to implement a flow.
  • Expression language support – An expression language lets you leverage logic from other layers of the web application. Logic that a developer would typically put into a controller that isn’t handled by Spring Web Flow can be expressed in XML using the expression language.
  • State management – Variables can be scoped (many scopes are supported), and Spring Web Flow automatically handles the clean up of those variables when they are out-of-scope.
  • Modularization of flow logic – Flows can be re-used from other flows.
A Flow is responsible for orchestrating the task (steps) and supporting the user events associated with the task. Instead of the MVC-based approach of creating one or more controllers and multiple request handlers for implementing a conversation, with Spring Web Flow you define a flow with states and expressions. Instead of the MVC-based approach of defining request mappings, with Spring Web Flow you define events. Spring Web Flow also lets you easily separate your logic for handling a user event (on event id) from the pre-load logic (on entry) for a particular state.


2.3 JavaServer Faces (JSF 2.0)

JavaServer Faces (JSF) is a standardized, component-based, and event-driven web framework for building user interfaces for Java web applications. Its goals are to simplify the development of user interfaces using XML-based view template language that can easily integrate with back end code written by Java developers. Primefaces is a component library for JSF 2.0, and it provides a rich set of UI components that can be easily used with JSF.


2.4 Adobe Flex

Adobe Flex is a free open source development kit for building rich internet applications (RIA) that deploy consistently on all major browsers, desktops, and operating systems by leveraging the Adobe® Flash® Player and Adobe AIR® runtimes. Spring-Flex BlazeDS Integration is a top-level Spring project with the purpose of making it easier to integrate Adobe Flex applications with Spring-based software components. 


2.5 Google Web Toolkit (GWT)

Google Web Toolkit (GWT) is a software development kit for building rich internet applications that run inside an HTML/JavaScript browser. The goals of GWT are to simplify the development of rich AJAX applications by using Java to implement the applications, which ultimately is compiled into JavaScript applications.

The scaffolding capabilities of MyEclipse with Spring support include two GWTrecipes.


2.6 iPhone

The iPhone web client is a web application that is suitable for iPhone web browsers. The foundation for the application is a Spring MVC client with the JSP pages generated using iWebKit for giving the application an iPhone look and feel.


3. Application layering

Application layering is a very common architecture approach in the application development field. Application layering consists of separating application code into discrete layers that have a distinct responsibility in the application, and each layer is isolated from other layers. The following diagram shows the most common application layers in a web application.

The Spring framework has support for stereotype annotations, which designate the layer/role of any annotated Java classes, and the annotations enable auto-detection of the classes. The next few sections briefly introduce each layer and describe the relevant Spring stereotypes for each layer.

Since application layering is prevalent and has direct support  from the Spring framework, MyEclipse advocates the use  of application layering and also organizes its code generation/scaffolding  capabilities around application layering.


3.1 Web Layer

The web layer is also referred to as the UI  layer. The web layer is primarily concerned with presenting the user  interface and the behavior of the application (handling user  interactions/events). While the web layer can also contain logic, core  application logic is usually located in the services layer.

There are a lot of options regarding the implementation of the  web layer. In regards to MyEclipse, some web layer  implementations might not be directly related to Spring; however they  could integrate with a Spring service layer and data access layer.

Here are some web layer examples (that also happen to be scaffolded by MyEclipse for  Spring):


3.2 Service Layer

The service layer, which is also referred to  as the logic layer or business layer, represents the core  functionality of the application being built. The web layer is the primary consumer of the  functionality provided by the service layer. The Spring framework  utilizes the @Service annotation to  designate service layer components. In addition to helping organize  application logic, an additional benefit of using a service layer is  that it can be exposed using other interfaces. For example, if  application logic needs to be available to other applications, it can  be exposed as web services using JAX-WS. If the web layer is an AJAX  application, the services can be exposed as JavaScript/JSON services  using DWR.


3.3 Domain Layer

The domain layer is represented by a  domain model, a distinct set of inter-related application objects that  embody the functionality and characteristics of the system being  built. In a web application this typically consists of Java Beans (POJO) and/or JPA entities. The Spring component stereotype (@Component) is also non-entity beans  that are going to be managed by the Spring framework.


3.4 Data Access Layer

The data access layer handles the persistence  of the domain model to and from a database (or some other persistence  mechanism). The Spring repository stereotype (@Repository) is suitable  for classes that are going to handle the persistence.


4. Code Assistants

Code Assistants refers to a set of capabilities in MyEclipse to help developers in their day-to-day development activities. The first set of code assistants are Annotators. While most JAVA developers understand the technical use JAVA annotations, it’s not always clear which annotations are available for a specific technology and when a particular annotation should be used. For example, the Spring framework has excellent support for annotation-based programming; however there are a lot of supported annotations. Some annotations are applicable to all Spring stereotypes, and other annotations are only applicable to specific stereotypes. Annotators support annotation-based development by helping a developer understand what annotations are available and in what context the annotation should be used.

For more information on annotators, see Spring Annotators, JAX-WS Annotators and JPA Annotators.

CodeAssistants
Code assistants


5. Spring DSL Model

MyEclipse with Spring support 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 artifact (Java code or XML configuration files), and each abstraction has its own dedicated editor for configuring instances of the abstraction.

metamodel
Spring DSL model

The Spring DSL defines 11 abstractions. Each abstraction has a direct correlation to one or more Java, Spring, and JPA artifacts and generate into a distinct set of code or configuration artifacts.

    • Spring DSL – used for project-wide configuration of Spring DSL code generation.
    • Model Package – used 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 Spring DSL artifacts created in a model package are generated into a matching Java package.
    • Web Controller – used for defining the web layer of an application using Spring MVC and managing related files. Web Controllers are generated into @Controller annotated Spring components, a specialized stereotype for web layer components.
    • Service – used for defining service layer components and managing related files. Services get generated into @Service annotated Spring components, a specialized stereotype for service layer components.
    • Component – used for defining a general-purpose Spring managed Java bean and managing related files. A component gets generated into @Component annotated Spring components, a generic stereotype for Spring managed components with configurable scope.
    • Operation – used to define methods for Controllers, Services and Components. Operations represent application logic, and they use inputs and outputs for exchanging data. Operations are generated directly in the respective Controller, Service, and Component class, and they are generated slightly differently to account for the different implementations needed for the Spring stereotypes.
    • Domain Object – used to define the domain model of the application and managing related files. 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.
    • Data Access Object (DAO) – used to separate data access logic from application logic and managing related files. 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.
    • Named Query – used for defining 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.
    • Exception – used to define custom exceptions and managing related files. Exceptions are generated into exception classes in Java.
    • Flow – used to implement the web layer of an application using Spring Web Flow.

    Editors

    Each Spring DSL component has an associated editor. Spring DSL-based development consists of a developer creating instances of the Spring DSL components, and configuring them using their respective editors. When code generation is invoked, the code and configuration files managed by the respective component are generated according to their configuration.

    A great way to jump start learning about the Spring DSL and Spring Editors is to combine the “Enable Spring DSL” feature with the “Scaffold Spring CRUD application” feature. When you only use scaffolding, the Spring DSL or Spring Editors aren’t available in the project. However, if you run the Enable Spring DSL wizard before running the scaffolding wizard (thus enabling Spring DSL and adding Spring DSL view to your project), the scaffolding wizard creates corresponding Spring DSL artifacts (model packages, controllers, services, etc…) for all scaffolded application components. They appear in the Spring DSL view of your project, and you can maintain and enhance the scaffolded artifacts using the Spring DSL and Spring Editors.

    Bootstrapping

    During the course of application development using the Spring DSL, a developer adds instances of relevant Spring DSL artifacts to their project. They use the respective editors to configure the artifacts according to their requirements, and MyEclipse generates the source code according to the configuration. This bootstrapping 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.

    Various  capabilities (some optional) can be added to a project,  including:

      • adding Spring application runtime dependencies.
      • bootstrapping the project with required Spring configurations.
      • enabling Code Generation Builder for automated code generation.
      • enabling Code Synchronization Builder for automated reverse engineering of Spring/Java artifacts.
      • enabling the Spring DSL, including enabling Spring DSL view in the project.
      • applying Spring Nature to the project for viewing and editing Spring configuration via Spring Elements node.
      • enabling code generation by application layer (web, service, domain and DAO).
      • configuring target projects and folders for code generation.
    Note: MyEclipse with Spring support will also bootstrap your project during CRUD scaffolding; however the scaffolding wizard won’t set up your project for code generation or Spring DSL support. If you want both scaffolding and Spring DSL support, then you should first enable Spring DSL, and then run Spring scaffolding.

    5.1 Spring DSL View

    When Spring code generation facets are added to an Eclipse project, a Spring DSL view is added to the project that is visible from the Project Explorer when viewing using the MyEclipse Spring perspective. This element shows a hierarchical view of the project’s Spring DSL artifacts, and it can be used for interacting with the Spring DSL, including:

    • configuring project level code generation settings (Spring DSL double-click, or right-click, and select Open from the menu)
    • opening Spring DSL editors (Artifact double-click, or right-click, and select Open from the menu)
    • creating new Spring DSL artifacts (right-click, and select New from the menu)
    • scaffolding (right-click, and select Scaffold Spring CRUD Application From)

    The Spring DSL view closely resembles the Java source code view in Eclipse. Therefore, it is very intuitive and easy to learn. The artifacts are organized by model packages, which correlate directly to Java packages. Under a model package, there are one or more Spring DSL artifacts. As mentioned earlier, a Spring DSL artifact is an abstraction that manages one or more Java code artifacts. The Spring DSL view lets you see your project at the abstraction level. If you want to see you project at the code level, then look at the source code.

    dsl_view
    Spring DSL view of a  scaffolded project

    5.2 Spring DSL Editors

    Each Spring DSL artifact has a specific editor for configuring the artifact, which subsequently influences the generation of the code and configuration files that are managed by the Spring DSL artifact. More information about the specific editors can be found in the Spring DSL section.


    6. Menus

    This section describes the various menus related to MyEclipse with Spring support.

    6.1 MyEclipse Menu

    When you right-click an Eclipse Project, the MyEclipse menu contains the following menu items. The top five menu items in the following screenshot are available only in MyEclipse with Spring support.

    myeclipsemenu
    MyEclipse menu

    6.2 Spring DSL Menu

    When you expand the Spring DSL menu folder (right-click, and select MyEclipse>Spring DSL), the menu contains the following menu items related to development using the Spring DSL. These menu items are available only in MyEclipse with Spring support.

    springdslmenu
    MyEclipse>Spring DSL menu

      • Enable Spring DSL: Runs the Enable Spring DSL wizard.
      • Disable Spring DSL: Disables the Spring DSL. This turns off all code generation and code synchronization performed by the DSL.
      • Update Spring DSL from Source: Runs the reverse engineering process to (a) update pre-existing Spring DSL artifacts from updates made to Java code or (b) create new Spring DSL artifacts from the Java source files. When a project has the Spring DSL enabled, an Eclipse builder monitors code changes in the project and automatically keeps the Spring DSL in sync. However, there could be occasions where you want to force the synchronization (i.e., for changes made outside of Eclipse).


    7. FAQ

    Is the Spring DSL required for all MyEclipse for Spring functions?
    No, the Spring DSL is required only when you want to leverage the Spring DSL abstractions (model-driven approach) for simplifying application development and use the associated Spring DSL Editors. The CRUD scaffolding function is not dependent on the Spring DSL.

    What specifically gets managed/generated for each Spring DSL abstraction?
    Each Spring DSL artifact manages one or more code-based artifacts. As you modify the Spring DSL artifact using its associated editor, all managed code artifacts are updated accordingly. All managed code artifacts are listed in the Code Generation tab of the respective Spring DSL editor (i.e., Service, Controller). This tab shows you all code artifacts that are generated, and each entry in the table represents a code artifact that is generated.

    What’s the difference between annotators and editors?
    Generally, they serve the same goal of helping you configure your Spring or JAX-WS artifacts. The Spring DSL editors are specifically related to using the Spring DSL, which provides code and configuration file management. Annotators, on the other hand, are independent of the Spring DSL and help you while editing code. If you want to use an abstraction layer that manages your code, then we recommend you use the Spring DSL and the associated editors. If you only want help with annotations, the we recommend you use the annotators.


    8. Resources