Followers

springAOP : Around

Employee.java
=========

package com.mohan;
public  class Employee{
    public void msg(){System.out.println("msg() is invoked");}
    public void display(){System.out.println("display() is invoked");}
}
      

TrackEmployee.java
=============

package com.mohan;
import org.aspectj.lang.ProceedingJoinPoint;
public class TrackEmployee
{
          public Object myadvice(ProceedingJoinPoint pjp) throws Throwable
          {
            System.out.println("Additional Concern Before calling actual method");
            Object obj=pjp.proceed();
            System.out.println("Additional Concern After calling actual method");
            return obj;
          }
}


Beans.xml
=======

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
          http://www.springframework.org/schema/aop
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />

<bean id="opBean" class="com.mohan.Employee">       </bean>      
<bean id="trackAspect" class="com.mohan.TrackEmployee"></bean>          
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @Around -->
     <aop:pointcut id="pointCutAround"      expression="execution(* com.mohan.Employee.*(..))" />
     <aop:around method="myadvice" pointcut-ref="pointCutAround" />
  </aop:aspect>
</aop:config>
         
</beans>


Test.java
======
package com.mohan;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
          public static void main(String[] args){
                   ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");                 
                   Employee e = (Employee) context.getBean("opBean");
                   e.msg();
                   e.display();
          }

}

No comments:

Post a Comment