Refactoring, Roo ITDs‚ and leaving Roo

Một phần của tài liệu Manning spring roo in action (Trang 81 - 86)

Now we’ll discuss how you can work with your ITDs. There are three techniques you need to learn to make the most out of the Roo development platform: push-in refac­

toring, pull-out refactoring, and removing Roo from your project.

Each of these techniques is supported from the SpringSource Tool Suite. IntelliJ also allows you to perform these operations, but we will focus on STS in this discussion.

We’ll begin by discussing push-in refactoring.

2.4.1 Push-in refactoring

Push-in refactoring is used to customize and replace the default behavior of Roo’s aspects. The technique moves a method or attribute from a Roo project to the Java source file. From there, the developer can modify the method code directly. Push-in refactoring is used for many purposes. Here are a few:

 Sorting the data to be retrieved to feed a list from the Roo JPA ITD

 Adding custom server-side validations to your Spring MVC controllers

49 Refactoring, Roo ITDs‚ and leaving Roo

 Customizing the way your entity is created, setting code-based defaults or pre- populating data from a web service

To perform push-in refactoring using the SpringSource Tool Suite, make sure STS is showing the Roo-generated ITDs (see section 2.3.4). Switch to the Package Explorer view using Window...> Show View...> Package Explorer.

You’ll implement a new business requirement for your task manager. Every time you mark a task complete, you’ll prepend the task text with (completed) so that it is obvious you’ve done your work.

ABOUT OUR APPROACH Because of the fact that JPA doesn’t guarantee the order of setters called when persisting an entity, you can’t implement this cus­

tomization using the entity setter method, setCompleted(). Instead, you’ll implement this in the service tier as you’re updating your task, and manually set the data on the way to the repository, thus skipping any strange JPA imple­

mentation logic issues.

Open up the Java source folder org.rooinaction.taskmanager.service, and expand the TaskServiceImpl_Roo_Service.aj aspect. Expand the ITD to show the member methods. Right-click on the updateTask(Task): void method, as shown in figure 2.10.

You’ll then see the Push In Intertype Declaration dialog, as shown in figure 2.11.

If you selected the method correctly, only the Task.setCompleted method will be pushed in. If, on the other hand, you clicked on the ITD itself and not a method or member variable, the entire ITD will be pushed in. This dialog is a chance to review your pending changes.

Figure 2.10 The refactoring push-in menu

Figure 2.11 Refactoring in STS using the push-in dialog

You can preview your changes by clicking the Preview > button, as shown in figure 2.11.

Click OK to perform the operation, as shown in figure 2.12.

Once the refactoring is complete, you’ll see the method vanish from the ITD, and appear in TaskServiceImpl.java. This will only happen if your Roo shell is running.

If nothing happens, fire up the shell. Now you can customize it to prepend your mes­

sage to the task text:

public Task updateTask(Task task) {

if (Boolean.TRUE.equals(task.getCompleted())) {

task.setDescription("(completed) " + task.getDescription());

}

return taskRepository.save(task);

}

Figure 2.12 Previewing the push-in

Refactoring, Roo ITDs‚ and leaving Roo 51 And there you have it. You’ve just extracted the default implementation of update- Task from the TaskServiceImpl_Roo_Service.aj ITD and pushed it in to the Task­

ServiceImpl.java service implementation.

2.4.2 Verify refactoring

Let’s run the application now, this time on tc Server within STS. To do so, just drag the project from the Package or Project Explorer bar to the VMWare Fabric tc Server Developer Edition server, either v2.5 or 2.6. This is located in the Servers view. You can then right-click on the server and select Start. Once the server is started, you can right-click on the application name in the server pane, and select Open Home Page.

If you execute your taskmanager web application again and complete a few tasks, you’ll see the completed message appear when you click on the checkbox and update your task.

WHAT IF I DON’T USE STS? If you don’t download STS, you’re missing out on a productive IDE. Although Eclipse can be clunky at times, STS installs several plug-ins that make developing Roo applications a snap, including AspectJ refactoring techniques, the Spring MVC namespace editor, and Maven editing support.

But if you don’t use STS, you can just cut and paste the code from your AspectJ ITDs, and use your editor’s search/replace feature to remove the Classname. prefix from the elements. Fire up the Roo shell and allow it to remove the methods from the ITD. Roo can then remove the ITD completely if you remove the annotation that created it, such as @RooWebScaffold.

2.4.3 Pulling code out to ITDs

You can actually create your own ITDs and pull code out of your class file into them.

This works as long as the code you’re pulling out isn’t supposed to exist because of the presence of another Roo annotation. You can’t push in your JavaBean setters and get­

ters and then pull them out to another aspect without removing the original @Roo- JavaBean annotation, for example.

To create your own ITD, you just use the File> New> Aspect menu item in STS. The ITDs you generate need to be marked as privileged aspect ITDs—you can just swap the public keyword with privileged. Once the ITD is created, you can use the Pack­

age Explorer to select a method of a Java class, then use the right-click menu to select Refactor> Pull Out ITD. Figure 2.13 shows the pull-out dialog.

Once finished, the method will be extracted into the AspectJ ITD you selected.

2.4.4 Leaving Roo behind

As much fun as you might have with Spring Roo, there may come a time when you want to remove it from your project. Perhaps you aren’t comfortable with supporting an AspectJ-based application platform, or you’re using Roo to prototype your project, but then will switch over to a traditional Spring application architecture.

Figure 2.13 Pulling out Java code to an ITD

Leaving Roo, because it’s a compile-time platform, is as simple as several steps:

1 Right-click on the project in the STS Package Explorer and select Refactor >

Push-In.

2 Accept the settings to remove all ITDs.

3 Edit the Maven pom.xml file and remove the aspectj-maven-plugin from the configuration.

As outlined in the Roo installation guide, you can leave the Roo annotations in the source code. That way, if you decide to re-install Roo and continue coding, you don’t have to redefine the annotations. You may remove the Roo annotations if you wish. If you do so, you can actually remove all of the Roo configuration, including AspectJ support, from the Maven pom.xml file by removing the aspectj-maven-plugin plug- in configuration entry from the build/plugins stanza.

DON’T FORGET TO SWITCH YOUR TRANSACTION CONFIGURATION IF YOU REMOVE ASPECTJ If you remove AspectJ from your Maven build, you’ll also want to switch to the standard Spring proxy mode of providing transaction support by removing the italicized mode="aspectj" entry from the application­

Context.xml fragment below:

<t:annotation-driven mode="aspectj"

transaction-manager="transactionManager"/>

Other configuration settings are no longer relevant either, such as the AspectJ Maven Plugin, and skipping the scan of AspectJ ITDs, because they’ll no longer exist in the project. Remove what you wish, unless you think at some point you may want to resume using Roo on your project.

Resources 53 Now let’s review what you’ve learned about working with the Roo shell, aspects, and the Eclipse IDE.

Một phần của tài liệu Manning spring roo in action (Trang 81 - 86)

Tải bản đầy đủ (PDF)

(406 trang)