91-9990449935 0120-4256464 |
Spring AOP AspectJ Annotation ExampleThe Spring Framework recommends you to use Spring AspectJ AOP implementation over the Spring 1.2 old style dtd based AOP implementation because it provides you more control and it is easy to use. There are two ways to use Spring AOP AspectJ implementation:
To understand the aop concepts, its advantage etc. visit here AOP Concepts TutorialSpring AspectJ AOP implementation provides many annotations:
The annotations used to create advices are given below:
Understanding PointcutPointcut is an expression language of Spring AOP. The @Pointcut annotation is used to define the pointcut. We can refer the pointcut expression by name also. Let's see the simple example of pointcut expression. The name of the pointcut expression is doSomething(). It will be applied on all the methods of Operation class regardless of return type. Understanding Pointcut ExpressionsLet's try the understand the pointcut expressions by the examples given below: It will be applied on all the public methods. It will be applied on all the public methods of Operation class. It will be applied on all the methods of Operation class. It will be applied on all the public setter methods of Employee class. It will be applied on all the methods of Operation class that returns int value. 1) @Before ExampleThe AspectJ Before Advice is applied before the actual business logic method. You can perform any operation here such as conversion, authentication etc. Create a class that contains actual business logic. File: Operation.javaNow, create the aspect class that contains before advice. File: TrackOperation.javaNow create the applicationContext.xml file that defines beans. File: applicationContext.xmlNow, let's call the actual method. File: Test.javaOutputAs you can see, additional concern is printed before msg(), m() and k() method is invoked. Now if you change the pointcut expression as given below: Now additional concern will be applied for the methods starting with m in Operation class. Output will be as this: Now you can see additional concern is not printed before k() method invoked. 2) @After ExampleThe AspectJ after advice is applied after calling the actual business logic methods. It can be used to maintain log, security, notification etc. Here, We are assuming that Operation.java, applicationContext.xml and Test.java files are same as given in @Before example. Create the aspect class that contains after advice. File: TrackOperation.javaOutputYou can see that additional concern is printed after calling msg(), m() and k() methods. 3) @AfterReturning ExampleBy using after returning advice, we can get the result in the advice. Create the class that contains business logic. File: Operation.javaCreate the aspect class that contains after returning advice. File: TrackOperation.java File: applicationContext.xmlIt is same as given in @Before advice example File: Test.javaNow create the Test class that calls the actual methods. OutputYou can see that return value is printed two times, one is printed by TrackOperation class and second by Test class. 4) @Around ExampleThe AspectJ around advice is applied before and after calling the actual business logic methods. Here, we are assuming that applicationContext.xml file is same as given in @Before example. Create a class that contains actual business logic. File: Operation.javaCreate the aspect class that contains around advice. You need to pass the PreceedingJoinPoint reference in the advice method, so that we can proceed the request by calling the proceed() method. File: TrackOperation.java File: Test.javaNow create the Test class that calls the actual methods. OutputYou can see that additional concern is printed before and after calling msg() and display methods. 5) @AfterThrowing ExampleBy using after throwing advice, we can print the exception in the TrackOperation class. Let's see the example of AspectJ AfterThrowing advice. Create the class that contains business logic. File: Operation.javaCreate the aspect class that contains after throwing advice. Here, we need to pass the Throwable reference also, so that we can intercept the exception here. File: TrackOperation.java File: applicationContext.xmlIt is same as given in @Before advice example File: Test.javaNow create the Test class that calls the actual methods. Output |