Home > Articles > Programming > Java

Using Eclipse AspectJ: Your First Steps

  • Print
  • + Share This
This chapter provides an introduction to the implementation of AspectJ, using a sample program for an insurance company.
This chapter is from the book

If you've been following along, then at this stage you should have a working Eclipse environment with AJDT installed. What we need now is an application we can use to do some AspectJ development. This chapter introduces a simple insurance application that we can use for this purpose. We show you how to convert the simple insurance Java project into an AspectJ project, discover all the places in the code to do with informing listeners whenever an insurance policy object is updated, and refactor all those code fragments into an aspect to give a modular implementation.

2.1 A Simple Insurance Application

Simple Insurers Inc. are considering going into business as no-frills, bargain-basement insurers. They have been developing a simple insurance application to underpin their new business venture and keep track of customers and policies. It is still early days, and the application is far from complete, but it implements just enough functionality to cover the first few user stories and get feedback from the internal customers to help plan the next iterations.

Figure 2.1 shows the Simple Insurance application project in Eclipse. It is just a regular Eclipse Java project at this stage. Notice in the Package Explorer (the view on the left side of the Eclipse window) that we have two source folders in the project, one named src and one named test-src. The src source folder contains the main application classes, and the test-src source folder contains the JUnit test cases.

Figure 2.1Figure 2.1 The Simple Insurance project in Eclipse.

If you installed the Eclipse AspectJ Examples plug-in following the instructions in Chapter 1, you can create the Simple Insurance project in your own workspace by clicking the New icon and selecting the Simple Insurance project from the Eclipse AspectJ Examples category (see Figure 2.2).

Figure 2.2Figure 2.2 Installing the Simple Insurance project into your workspace.

It is a good idea at this point to run the test suite and make sure that nothing is amiss. We will be working primarily with the insurance.model and insurance.model.impl packages where the domain classes can be found. As shown in Figure 2.3, expand the insurance.model package node inside the test-src source folder and select the file AllTests.java. From the context menu (right-click) select Run > JUnit Test. If all goes well, you should be able to click the JUnit tab to bring the JUnit view to the foreground, and see a successful result as shown in Figure 2.4.

Figure 2.3Figure 2.3 Launching the test suite.

Figure 2.4Figure 2.4 A passing test suite—the JUnit view.

Figure 2.5 shows an overview of the classes in the Simple Insurers Inc. domain model (the insurance.model package).

Figure 2.5Figure 2.5 Simple Insurers Inc. domain model.

Simple Insurers Inc. will be offering three kinds of insurance policies when they first launch: life insurance, car insurance, and home insurance. Policies are taken out by customers, who pay for their insurance in accordance with some payment schedule. The model also has a facility to record claims made against policies, although worryingly there is no implementation yet to actually pay out on claims.

Simple Insurers Inc. will initially use telesales to market their insurance products, and agents at their company headquarters will have available to them a simple desktop application with which they can create, view, and update information on customers and policies. The user interface for this application is implemented in the insurance.ui package. You can launch the application from the Eclipse workbench by selecting the SimpleInsuranceApp.java file in the Package Explorer view, and then choosing Run > Java Application from the context menu. Figure 2.6shows how the application looks when running.

Figure 2.6Figure 2.6 The Simple Insurers Inc.

  • + Share This
  • 🔖 Save To Your Account