25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 8 ■ Give Advice to Your <strong>Spring</strong> Application<br />

AOP Concepts<br />

Let’s quickly review some of the AOP concepts over your SearchEngineService.java class (as shown in Listing 8-1).<br />

Let’s also start thinking about the logging concerns (see Figure 8-2 and Table 8-1). Start by writing your concerns.<br />

Figure 8-2. AOP Definitions<br />

Table 8-1. AOP Concepts<br />

ASPECT<br />

ADVICE<br />

POINTCUT<br />

JOIN POINT<br />

A set of pointcuts and related advices.<br />

An action that will be executed at the join points in a particular pointcut.<br />

A pattern (expression language) that bounds an advice with a single or many joint points.<br />

A method exception where the advice will be executed.<br />

Figure 8-2 explains the Table 8-1 concepts that you will find within your Log module. But I’ll explain them more<br />

in detail here. In your case, you need to create a Log module that will log every method call you have. Figure 8-2 shows<br />

the Log module that will define an advice.<br />

Figure 8-2 shows how the aspect will be applied. So in this case, the Log module defines an advice, this advice<br />

will be executed based on the defined pointcut. Figure 8-2 shows the (* findBy*(*)) pointcut expression. This<br />

expression means the following:<br />

• Select a method that has any return type (* findBy*(*)), that is what the first asterisk means.<br />

• Select a method that contains a prefix findBy. (* findBy*(*)).<br />

• With any number of parameters (* findBy*(*)), that is what the last asterisk means.<br />

So based on the preceding explanation, then joint point will be the findByType(Type Document)method that<br />

will execute the advice. In other words, the method findByType will be intercepted and the Log module logic will be<br />

executed. All of this working together is an aspect.<br />

The <strong>Spring</strong> <strong>Framework</strong> provides an AOP extension and supports four types of advices:<br />

• Before advice: Runs before every method call.<br />

• After returning advice: Runs after a method call and returns a result.<br />

• Around advice: Runs before and after a method call, and combines advices.<br />

• After throwing advice: Runs after throwing an exception.<br />

92

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!