Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

JdbcTemplate Example


Spring 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.

  1. aop:before It is applied before calling the actual business logic method.
  2. aop:after It is applied after calling the actual business logic method.
  3. aop:after-returning it is applied after calling the actual business logic method. It can be used to intercept the return value in advice.
  4. aop:around It is applied before and after calling the actual business logic method.
  5. aop:after-throwing It is applied if actual business logic method throws exception.

To understand the aop concepts, its advantage etc. visit here AOP Concepts Tutorial


download all examples (developed using MyEclipse IDE)


1) aop:before Example

The 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.java

  1. package com.Kreationnext;
  2. public  class Operation{
  3.     public void msg(){System.out.println(“msg method invoked”);}
  4.     public int m(){System.out.println(“m method invoked”);return 2;}
  5.     public int k(){System.out.println(“k method invoked”);return 3;}
  6. }

Now, create the aspect class that contains before advice.

File: TrackOperation.java

  1. package com.Kreationnext;
  2. import org.aspectj.lang.JoinPoint;
  3. public class TrackOperation{
  4.     public void myadvice(JoinPoint jp)//it is advice
  5.     {
  6.         System.out.println(“additional concern”);
  7.         //System.out.println(“Method Signature: ”  + jp.getSignature());
  8.     }
  9. }

Now create the applicationContext.xml file that defines beans.

File: applicationContext.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:aop=“http://www.springframework.org/schema/aop”
  5.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  7.     http://www.springframework.org/schema/aop 
  8.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “>
  9. <aop:aspectj-autoproxy />
  10. <bean id=“opBean” class=“com.Kreationnext.Operation”>   </bean>
  11. <bean id=“trackAspect” class=“com.Kreationnext.TrackOperation”></bean>
  12. <aop:config>
  13.   <aop:aspect id=“myaspect” ref=“trackAspect” >
  14.      <!– @Before –>
  15.      <aop:pointcut id=“pointCutBefore”   expression=“execution(* com.Kreationnext.Operation.*(..))” />
  16.      <aop:before method=“myadvice” pointcut-ref=“pointCutBefore” />
  17.   </aop:aspect>
  18. </aop:config>
  19. </beans>

Now, let’s call the actual method.

File: Test.java

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test{
  5.     public static void main(String[] args){
  6.         ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
  7.         Operation e = (Operation) context.getBean(“opBean”);
  8.         System.out.println(“calling msg…”);
  9.         e.msg();
  10.         System.out.println(“calling m…”);
  11.         e.m();
  12.         System.out.println(“calling k…”);
  13.         e.k();
  14.     }
  15. }

Output

  1. calling msg…
  2. additional concern
  3. msg() method invoked
  4. calling m…
  5. additional concern
  6. m() method invoked
  7. calling k…
  8. additional concern
  9. k() method invoked

As you can see, additional concern is printed before msg(), m() and k() method is invoked.


2) aop:after example

The 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.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:aop=“http://www.springframework.org/schema/aop”
  5.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  7.     http://www.springframework.org/schema/aop 
  8.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “>
  9. <aop:aspectj-autoproxy />
  10. <bean id=“opBean” class=“com.Kreationnext.Operation”>   </bean>
  11. <bean id=“trackAspect” class=“com.Kreationnext.TrackOperation”></bean>
  12. <aop:config>
  13.   <aop:aspect id=“myaspect” ref=“trackAspect” >
  14.      <!– @After –>
  15.      <aop:pointcut id=“pointCutAfter”    expression=“execution(* com.Kreationnext.Operation.*(..))” />
  16.      <aop:after method=“myadvice” pointcut-ref=“pointCutAfter” />
  17.   </aop:aspect>
  18. </aop:config>
  19. </beans>

Output

  1. calling msg…
  2. msg() method invoked
  3. additional concern
  4. calling m…
  5. m() method invoked
  6. additional concern
  7. calling k…
  8. k() method invoked
  9. additional concern

You can see that additional concern is printed after calling msg(), m() and k() methods.


3) aop:after-returning example

By using after returning advice, we can get the result in the advice.

Create the class that contains business logic.

File: Operation.java

  1. package com.Kreationnext;
  2. public  class Operation{
  3.     public int m(){System.out.println(“m() method invoked”);return 2;}
  4.     public int k(){System.out.println(“k() method invoked”);return 3;}
  5. }

Create the aspect class that contains after returning advice.

File: TrackOperation.java

  1. package com.Kreationnext;
  2. import org.aspectj.lang.JoinPoint;
  3. public class TrackOperation{
  4.     public void myadvice(JoinPoint jp,Object result)//it is advice (after advice)
  5.     {
  6.         System.out.println(“additional concern”);
  7.         System.out.println(“Method Signature: “  + jp.getSignature());
  8.         System.out.println(“Result in advice: “+result);
  9.         System.out.println(“end of after returning advice…”);
  10.     }
  11. }

File: applicationContext.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:aop=“http://www.springframework.org/schema/aop”
  5.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  7.     http://www.springframework.org/schema/aop 
  8.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “>
  9. <aop:aspectj-autoproxy />
  10. <bean id=“opBean” class=“com.Kreationnext.Operation”>   </bean>
  11. <bean id=“trackAspect” class=“com.Kreationnext.TrackOperation”></bean>
  12. <aop:config>
  13.   <aop:aspect id=“myaspect” ref=“trackAspect” >
  14.      <!– @AfterReturning –>
  15.      <aop:pointcut id=“pointCutAfterReturning”   expression=“execution(* com.Kreationnext.Operation.*(..))” />
  16.      <aop:after-returning method=“myadvice” returning=“result” pointcut-ref=“pointCutAfterReturning” />
  17.   </aop:aspect>
  18. </aop:config>
  19. </beans>

File: Test.java

Now create the Test class that calls the actual methods.

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test{
  5.     public static void main(String[] args){
  6.         ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
  7.         Operation e = (Operation) context.getBean(“opBean”);
  8.         System.out.println(“calling m…”);
  9.         System.out.println(e.m());
  10.         System.out.println(“calling k…”);
  11.         System.out.println(e.k());
  12.     }
  13. }

Output

  1. calling m…
  2. m() method invoked
  3. additional concern
  4. Method Signature: int com.Kreationnext.Operation.m()
  5. Result in advice: 2
  6. end of after returning advice…
  7. 2
  8. calling k…
  9. k() method invoked
  10. additional concern
  11. Method Signature: int com.Kreationnext.Operation.k()
  12. Result in advice: 3
  13. end of after returning advice…
  14. 3

You can see that return value is printed two times, one is printed by TrackOperation class and second by Test class.


4) aop:around example

The AspectJ around advice is applied before and after calling the actual business logic methods.

Create a class that contains actual business logic.

File: Operation.java

  1. package com.Kreationnext;
  2. public  class Operation{
  3.     public void msg(){System.out.println(“msg() is invoked”);}
  4.     public void display(){System.out.println(“display() is invoked”);}
  5. }

Create 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

  1. package com.Kreationnext;
  2. import org.aspectj.lang.ProceedingJoinPoint;
  3. public class TrackOperation
  4. {
  5.     public Object myadvice(ProceedingJoinPoint pjp) throws Throwable
  6.     {
  7.         System.out.println(“Additional Concern Before calling actual method”);
  8.         Object obj=pjp.proceed();
  9.         System.out.println(“Additional Concern After calling actual method”);
  10.         return obj;
  11.     }
  12. }

File: applicationContext.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:aop=“http://www.springframework.org/schema/aop”
  5.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  7.     http://www.springframework.org/schema/aop 
  8.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “>
  9. <aop:aspectj-autoproxy />
  10. <bean id=“opBean” class=“com.Kreationnext.Operation”>   </bean>
  11. <bean id=“trackAspect” class=“com.Kreationnext.TrackOperation”></bean>
  12. <aop:config>
  13.   <aop:aspect id=“myaspect” ref=“trackAspect” >
  14.      <!– @Around –>
  15.      <aop:pointcut id=“pointCutAround”   expression=“execution(* com.Kreationnext.Operation.*(..))” />
  16.      <aop:around method=“myadvice” pointcut-ref=“pointCutAround” />
  17.   </aop:aspect>
  18. </aop:config>
  19. </beans>

File: Test.java

Now create the Test class that calls the actual methods.

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test{
  5.     public static void main(String[] args){
  6.         ApplicationContext context = new classPathXmlApplicationContext(“applicationContext.xml”);
  7.         Operation op = (Operation) context.getBean(“opBean”);
  8.         op.msg();
  9.         op.display();
  10.     }
  11. }

Output

  1. Additional Concern Before calling actual method
  2. msg() is invoked
  3. Additional Concern After calling actual method
  4. Additional Concern Before calling actual method
  5. display() is invoked
  6. Additional Concern After calling actual method

You can see that additional concern is printed before and after calling msg() and display methods.


5) aop:after-throwing example

By 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.java

  1. package com.Kreationnext;
  2. public  class Operation{
  3.     public void validate(int age)throws Exception{
  4.     if(age<18){
  5.         throw new ArithmeticException(“Not valid age”);
  6.     }
  7.     else{
  8.         System.out.println(“Thanks for vote”);
  9.     }
  10.     }
  11. }

Create 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

  1. package com.Kreationnext;
  2. import org.aspectj.lang.JoinPoint;
  3. public class TrackOperation{
  4.     public void myadvice(JoinPoint jp,Throwable error)//it is advice
  5.     {
  6.         System.out.println(“additional concern”);
  7.         System.out.println(“Method Signature: “  + jp.getSignature());
  8.         System.out.println(“Exception is: “+error);
  9.         System.out.println(“end of after throwing advice…”);
  10.     }
  11. }

File: applicationContext.xml

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <beans xmlns=“http://www.springframework.org/schema/beans”
  3.     xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
  4.     xmlns:aop=“http://www.springframework.org/schema/aop”
  5.     xsi:schemaLocation=”http://www.springframework.org/schema/beans
  6.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
  7.     http://www.springframework.org/schema/aop 
  8.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “>
  9. <aop:aspectj-autoproxy />
  10. <bean id=“opBean” class=“com.Kreationnext.Operation”>   </bean>
  11. <bean id=“trackAspect” class=“com.Kreationnext.TrackOperation”></bean>
  12. <aop:config>
  13.   <aop:aspect id=“myaspect” ref=“trackAspect” >
  14.      <!– @AfterThrowing –>
  15.      <aop:pointcut id=“pointCutAfterThrowing”    expression=“execution(* com.Kreationnext.Operation.*(..))” />
  16.      <aop:after-throwing method=“myadvice” throwing=“error” pointcut-ref=“pointCutAfterThrowing” />
  17.   </aop:aspect>
  18. </aop:config>
  19. </beans>

File: Test.java

Now create the Test class that calls the actual methods.

  1. package com.Kreationnext;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test{
  5.     public static void main(String[] args){
  6.         ApplicationContext context = new ClassPathXmlApplicationContext(“applicationContext.xml”);
  7.         Operation op = (Operation) context.getBean(“opBean”);
  8.         System.out.println(“calling validate…”);
  9.         try{
  10.             op.validate(19);
  11.         }catch(Exception e){System.out.println(e);}
  12.         System.out.println(“calling validate again…”);
  13.         try{
  14.             op.validate(11);
  15.         }catch(Exception e){System.out.println(e);}
  16.     }
  17. }

Output

  1. calling validate…
  2. Thanks for vote
  3. calling validate again…
  4. additional concern
  5. Method Signature: void com.Kreationnext.Operation.validate(int)
  6. Exception is: java.lang.ArithmeticException: Not valid age
  7. end of after throwing advice…
  8. java.lang.ArithmeticException: Not valid age

Comments

comments