91-9990449935 0120-4256464 |
Spring AOP AspectJ Xml Configuration ExampleSpring enables you to define the aspects, advices and pointcuts in xml file. In the previous page, we have seen the aop examples using annotations. Now we are going to see same examples by the xml configuration file. Let's see the xml elements that are used to define advice.
To understand the aop concepts, its advantage etc. visit here AOP Concepts Tutorial1) aop: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. 2) aop: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, TrackOperation.java and Test.java files are same as given in aop:before example. Now create the applicationContext.xml file that defines beans. File: applicationContext.xmlOutputYou can see that additional concern is printed after calling msg(), m() and k() methods. 3) aop:after-returning 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.xml 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) aop:around exampleThe AspectJ around advice is applied before and after calling the actual business logic methods. 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: applicationContext.xml 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) aop:after-throwing 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.xml File: Test.javaNow create the Test class that calls the actual methods. Output
Next TopicSpring Jdbc Template Tutorial
|