..
Now let's see how to create an aspect.
First of all we create an interface on which the example we will write our pointcuts:
public interface MyInterface {
public void f1 ();
public int f2 ();
public int f3 () throws Exception;
public void f4 () throws Exception;
public int f5 ();
}
To write a pointcut must know the AspectJ pointcut designators , in our examples we will use only execution that matches the join point execution methods.
execution (modifiers-pattern? Declaring ret-type-pattern-type-pattern? name-pattern (param-pattern) throws-pattern?)where:
First we must create our own aspect:
@ Aspect
{public class MyAspect
.............
}
As we see the class is annotated with @ Aspect.
This is not enough because you have to enable support for AOP nell'applicationContext. Xml:
<! - ENBLING AspectJ -> <aop:aspectj-autoproxy /> <! - MYASPECT -> <bean id="myAspect" class="it.mrwebmaster.aop.MyAspect" />
Once you have made these two operations are ready to create an advice, for example, an action performed before the execution of the method f1 (before advice):
@ Before ("execution (* it.mrwebmaster.aop.MyInterface.f1 (..))")
public void beforeF1 () {
System.out.println ("BEFORE F1");
}
As can be seen from the code we used the @ Before annotation which accepts an expression as a value that identifies a pointcut.
In our example, the expression matches all methods that are called interface it.mrwebmaster.aop.MyInterface f1 indipendetemente by their modifiers, return type and input parameters.
Similarly we can use an advice that executes when a method completes its execution (after returning advice) correctly using the annotation @ AfterReturning:
@ AfterReturning (pointcut = "execution (* it.mrwebmaster.aop.MyInterface.f2 (..))", returning =" retVal ")
public void afterReturningF2 (Object retVal) {
System.out.println ("RETURN F2" + retVal);
}
This record takes the following parameters, in addition to pointcuts, the name given to the object returned by the method that can be given as an input parameter of Advaita.
In this case the expression of the pointcut is inviarata except for the name of the method, which in this case, f2.
@ AfterThrowing (pointcut = "execution (* it.mrwebmaster.aop.MyInterface.f3 (..))", throwing =" throwable ")
public void afterThrowingF3 (Throwable throwable) {
System.out.println ("F3 throws" + throwable);
}
The difference is that the method does not return an object but an exception.
Another type of advice is always executed after a method, it is time normally or throws an exception (after advice). This advice is implemented through the use of @ After:
@ After ("execution (* it.mrwebmaster.aop.MyInterface.f4 (..))")
public void afterF4 () {
System.out.println ("AFTER F4");
}
Finally we see how to make the 'around advice:
@ Around ("execution (* it.mrwebmaster.aop.MyInterface.f5 (..))")
public void aroundF5 (ProceedingJoinPoint PJP) {
System.out.println ("BEFORE F5");
try {
Object retVal = pjp.proceed ();
System.out.println ("RETURN F5" + retVal);
} Catch (Throwable e) {
System.out.println ("F5 throws" + e);
}
}
As we can see from the code of the pointcut expression is not different from other advices.
What changes is the same advice that must explicitly invoke the execution of the method through the method of the class ProceedingJoinPoint proceed, one whose application is passed as input.
This interface provides also uses other methods proceed to retrieve information about method parameters, return type and the object on which method is executed.
It is left to the reader any deeper.
To test our advices we can do is write a trivial implementation of the interface MyInterface, and create a main test:
MyInterfaceImpl {public class implements MyInterface
@ Override
public void f1 () {
System.out.println ("F1");
}
@ Override
public int f2 () {
System.out.println ("F2");
return 0;
}
@ Override
public int f3 () throws Exception {
System.out.println ("F3");
throw new Exception ("Exception F3");
}
@ Override
public void f4 () throws Exception {
System.out.println ("F4");
}
@ Override
public int f5 () {
System.out.println ("F5");
return 0;
}
}
nell'applicationContext. xml:
<! - Target: Object -> <bean id="myInterfaceImpl" class="it.mrwebmaster.aop.MyInterfaceImpl" />The main test:
public class Main {
public static void main (String [] args) {
/ **
* Instanz the IoC container
* /
ApplicationContext ApplicationContext ClassPathXmlApplicationContext = new ("applicationContext.xml");
MyInterface MyInterface = (MyInterface) applicationContext.getBean ("myInterfaceImpl");
myInterface.f1 ();
System.out.println ("########## \ n ");
myInterface.f2 ();
System.out.println ("########## \ n ");
try {
myInterface.f3 ();
} Catch (Exception e) {}
System.out.println ("########## \ n ");
try {
myInterface.f4 ();
} Catch (Exception e) {}
System.out.println ("########## \ n ");
myInterface.f5 ();
System.out.println ("########## \ n ");
}
}
| |
Linux (Course)
Complete guide to open-source system. From 49 €. |
| |
PHP (Course)
Full course for creating dynamic Web sites. From 49 €. |
| |
Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €. |