91-9990449935 0120-4256464 |
Spring AOP ExampleThere are given examples of Spring1.2 old style AOP (dtd based) implementation. Though it is supported in spring 3, but it is recommended to use spring aop with aspectJ that we are going to learn in next page. There are 4 types of advices supported in spring1.2 old style aop implementation.
To understand the basic concepts of Spring AOP, visit the previous page.Understanding the hierarchy of advice interfacesLet's understand the advice hierarchy by the diagram given below: All are interfaces in aop. MethodBeforeAdvice interface extends the BeforeAdvice interface. AfterReturningAdvice interface extends the AfterAdvice interface. ThrowsAdvice interface extends the AfterAdvice interface. MethodInterceptor interface extends the Interceptor interface. It is used in around advice. 1) MethodBeforeAdvice ExampleCreate a class that contains actual business logic. File: A.javaNow, create the advisor class that implements MethodBeforeAdvice interface. File: BeforeAdvisor.javaIn xml file, create 3 beans, one for A class, second for Advisor class and third for ProxyFactoryBean class. File: applicationContext.xmlUnderstanding ProxyFactoryBean class: The ProxyFactoryBean class is provided by Spring Famework. It contains 2 properties target and interceptorNames. The instance of A class will be considered as target object and the instance of advisor class as interceptor. You need to pass the advisor object as the list object as in the xml file given above. The ProxyFactoryBean class is written something like this: Now, let's call the actual method. File: Test.javaOutputPrinting additional information in MethodBeforeAdviceWe can print additional information like method name, method argument, target object, target object class name, proxy class etc. You need to change only two classes BeforeAdvisor.java and Test.java. File: BeforeAdvisor.java File: Test.javaOutput2) AfterReturningAdvice ExampleCreate a class that contains actual business logic. File: A.javaSame as in the previous example. Now, create the advisor class that implements AfterReturningAdvice interface. File: AfterAdvisor.javaCreate the xml file as in the previous example, you need to change only the advisor class here. File: applicationContext.xml File: Test.javaSame as in the previous example. Output3) MethodInterceptor (AroundAdvice) ExampleCreate a class that contains actual business logic. File: A.javaSame as in the previous example. Now, create the advisor class that implements MethodInterceptor interface. File: AroundAdvisor.javaCreate the xml file as in the previous example, you need to change only the advisor class here. File: applicationContext.xml File: Test.javaSame as in the previous example. Output4) ThrowsAdvice ExampleCreate a class that contains actual business logic. File: Validator.javaNow, create the advisor class that implements ThrowsAdvice interface. File: ThrowsAdvisor.javaCreate the xml file as in the previous example, you need to change only the Validator class and advisor class. File: applicationContext.xml File: Test.javaOutput
Next TopicSpring AOP AspectJ Annotation Example
|