MyEclipse Logo

MyEclipse for Spring 8.6: Spring Annotators

How to use Spring Annotators for creating a new Spring controller

Abstract

This tutorial provides an overview of using the Spring Annotators in MyEclipse for Spring.


Table of Contents

1. Introduction to Annotators
2. Goal
3. Prerequisites
4. Create a new Java Class
5. Configure as Spring Controller
6. Conclusion
7. Additional Developer Resources

1. Introduction to Annotators

Annotators are part of the code assistants that are available in MyEclipse for Spring, and annotators help developers with annotation-based development. While most JAVA developers understand how to technically use JAVA annotations, it's not always clear which annotations are available for a specific technology and when a particular annotation should be use. 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.

MyEclipse for Spring 8.6 contains two new sets of annotators. The Spring Annotator helps Spring developers annotate Spring controllers, services, components and repositories. The JAX-WS Annotator is for configuring JAX-WS web services.

Code Assistants

Figure 1. Code Assistants


2. Goal

This tutorial is going to walk you through using the Spring Annotator for implementing a Spring controller. You will start with a standard Java class that will be added to a scaffolded project, and you will use the Spring Annotator for Spring-enabling the Java class.

While this tutorial is only focusing on Spring controllers, the Spring Annotator can also be used for Spring services, components and repositories. You may also be interested in the JAX-WS Annotator for configuring JAX-WS web services.

3. Prerequisites

The prerequisites needed to complete this tutorial are:

4. Create a new Java Class

First we will create the Java class. It doesn't matter how the Java class was created. It could be hand-coded or generated by Spring MVC scaffolding. For this tutorial we will create a new Java class.

  1. Use Eclipse to create a standard Java class. Right-click on the project, and choose New > Class

    New > Java Class

    Figure 2. New > Java Class


  2. Add the following code to the new Java class. Please note that there's nothing specific to Spring in this code.

    Example 1. MyController Java Code

    package org.customerapp.web;
    
    import org.customerapp.dao.CustomerDAO;
    import org.customerapp.service.CustomerService;
    
    public class MyController {
    
       public CustomerService custService;
    
       public CustomerDAO custDAO;
    
       public String doSomethingOne(){
          return "";
       }
    
       public String doSomethingTwo(String xyz){
          return "";
       }
    }
    

  3. Use Eclipse to generate getters and setters for custService and custDAO. (Source-->Generate Getters and Setters...)

    Generate Getters and Setters

    Figure 3. Generate Getters and Setters


5. Configure as Spring Controller

Now that we have our Java class, let's use the Spring Annotator to handle the Spring-specific annotation and configuration of the Java class.

  1. While the MyController class is open in the Eclipse Java editor, switch to the Spring Annotator.

    Switch to Spring Annotator

    Figure 4. Switch to Spring Annotator


    The Spring Annotator is implemented as an Eclipse View. If the View isn't visible, you can add it using the Window-->Show View option.

  2. From the Spring Annotator specify how you would like to stereotype the new Java class. For this tutorial select Controller.

    Switch to Spring Annotator

    Figure 5. Switch to Spring Annotator


    The moment you select Controller, the Java class is immediately annotated with the @Controller annotation.

  3. The Spring annotator will now let you further configure the Java class with annotations that are relevant to Spring controllers. If you highlight the class name (MyController) in code or the annotator's outline view, the configuration panel will show you a list of available class-level annotations. Type MyNewController for the controller name, and select singleton for the scope. As you configure the Java class using the annotator, the Java code is immediately updated to reflect the configuration.

    Class-level configuration

    Figure 6. Class-level configuration


    In this example the MyController class is being annotated as a Spring Controller. The bean name is MyNewController, and the scope of the controller is singleton. If desired, you can switch to the Transactional tab to configure and specify the transactional properties for the controller.

  4. The Spring annotator will also let you further configure the class variables with annotations that are relevant to Spring controllers. If you highlight the custService variable declaration in code or the annotator's outline view, the configuration panel will show you a list of available variable-level annotations. Select @Resource and specify CustomerService as the name. As you configure the variable using the annotator, the Java code is immediately updated to reflect the configuration.

    Variable-level configuration

    Figure 7. Variable-level configuration


    In this example the variable is being configured to be injected by Spring with a bean that's named CustomerService, which is one of the originally scaffolded Spring Services (from scaffolding tutorial).

  5. The Spring annotator will also let you further configure the class methods with annotations that are relevant to Spring controllers. If you highlight the doSomethingOne() method in code or the annotator's outline view, the configuration panel will show you a list of available method-level annotations. Select @RequestMapping, specify "/dosomething.html" for the URL, and select GET and POST as the methods. As you configure the method using the annotator, the Java code is immediately updated to reflect the configuration.

    Method-level configuration

    Figure 8. Method-level configuration


    In this example the doSomethingOne() method is being configured as a request handler. It's being bound to the "/dosomething.html" URL, and specifically to GET and POST methods. If desired, you can switch to the Transactional tab to configure and specify the transactional properties for the request handler.

  6. The Spring annotator will also let you further configure method parameters with annotations that are relevant to Spring controllers. If you highlight the doSomethingTwo() method in code or the annotator's outline view, the configuration panel will show you a list of available method-level annotations. Select @RequestMapping, specify "/dosomethingelse.html" for the URL, and select GET and POST as the methods. Next you can configure the individual method arguments, including how the method argument is mapped (@RequestParm or @ModelAttribute) and the parameter name (if different from the method argument name). As you configure the method using the annotator, the Java code is immediately updated to reflect the configuration.

    Method parameter configuration

    Figure 9. Method parameter configuration


    In this example the doSomethingTwo() method is being configured as a request handler. It's being bound to the "/dosomethingelse.html" URL, and specifically to GET and POST methods. The xyz method argument is configured as a request parameter (@RequestParam) and bound to the username request parameter.

6. Conclusion

This tutorial walked you through the use of the Spring Annotator for creating Spring controllers.

You may also want to try the Spring MVC Scaffolding and JAX-WS Annotator tutorials which are available in the Eclipse help system and online (under MyEclipse for Spring Education Materials).

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

7. 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:

Developer Resources