hi ,
i am trying to implement Around advice. i am not getting my Aspects getting invoked. i am not using <aop:config> tag as i am only using Around advice type. is that correct?. the code follows
SampleAOP2.xml
Performer.javaCode:<?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-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"> <bean id="Singer" class="SampleAOP2.Singer"> <constructor-arg value="3" /> </bean> <bean id="Juggler" class="SampleAOP2.Juggler"> <constructor-arg value="5" /> </bean> <bean id="Audience" class="SampleAOP2.Audience"></bean> <bean id="AudienceAroundAdvice" class="SampleAOP2.AudienceAroundAdvice"> <property name="audience" ref="Audience" /> </bean> <bean id="AudienceAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="AudienceAroundAdvice" /> <property name="pattern" value=".*perform" /> </bean> </beans>
Juggler.javaCode:package SampleAOP2; public interface Performer { void perform(); }
Singer.javaCode:package SampleAOP2; public class Juggler implements Performer { private int beanBags; public Juggler() { } public Juggler(int beanBags) { this.beanBags = beanBags; } public void perform() { System.out.println("Juggler is Performing"); System.out.println("Juggler is JUGGLING with "+beanBags+"CREDITS"); } }
Audience.javaCode:package SampleAOP2; public class Singer implements Performer { private int beanBags; public Singer() { } public Singer(int beanBags) { this.beanBags = beanBags; } public void perform() { System.out.println("Singer is Performing"); System.out.println("Singer is SINGING with "+beanBags+"CREDITS"); } }
AudienceAroundAdvice.javaCode:package SampleAOP2; public class Audience { public Audience(){} public void takeSeats(){ System.out.println("The Audience is taking their seats."); } public void turnOffCellPhones(){ System.out.println("The Audience is turning off their cellphones "); } public void applaud(){ System.out.println("CLAP CLAP CLAP"); } public void demandRefund(){ System.out.println("Boo !. We want our money back"); } }
MainClass.javaCode:package SampleAOP2; import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class AudienceAroundAdvice implements MethodInterceptor { private Audience audience; public Object invoke(MethodInvocation invocation) throws Throwable { try { audience.takeSeats(); audience.turnOffCellPhones(); Object returnValue = invocation.proceed(); audience.applaud(); return returnValue; } catch (Exception ex) { audience.demandRefund(); throw ex; } } public void setAudience(Audience audience) { this.audience = audience; } }
i am using MainClass.java to debug/run the applicationCode:package SampleAOP2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import SampleAOP2.Juggler; import SampleAOP2.Singer; public class MainClass { public static void main(String[] args) { ApplicationContext AC = new ClassPathXmlApplicationContext( "SampleAOP2.xml"); Performer performerOBJ = (Performer) AC.getBean("Singer"); performerOBJ.perform(); performerOBJ = (Performer) AC.getBean("Juggler"); performerOBJ.perform(); } }


Reply With Quote