Tuesday, August 14, 2012

AOP Implementation using multiple advices


Audience.java:
package com.lnn.advice;

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 CLAP CLAP");
      }

      public void demandRefund() {
            System.out.println("Boo! We want our money back!");
      }
}
Instrument.java:
package com.lnn.advice;

public interface Instrument {
      public void play();
}
Violin.java:
package com.lnn.advice;

public class Violin implements Instrument {
      @Override
      public void play() {
            System.out.println("violin is playing now...");
      }
}
PerformanceException:
package com.lnn.advice;

public class PerformanceException extends Exception {

      private static final long serialVersionUID = -6772609716850440380L;
     
}
Performer.java:
package com.lnn.advice;

public interface Performer {
      public void perform() throws PerformanceException;
}
Instrumentalist.java:
package com.lnn.advice;

import com.lnn.advice.Instrument;
import com.lnn.advice.PerformanceException;
import com.lnn.advice.Performer;

public class Instrumentalist implements Performer {
      public Instrumentalist() {
      }

      public void perform() throws PerformanceException {
            System.out.print("Playing " + song + " : ");
            if (instrument != null) {
                  instrument.play();
            } else {
                  throw new PerformanceException();
            }
      }

      private String song;

      public void setSong(String song) {
            this.song = song;
      }

      private Instrument instrument;

      public void setInstrument(Instrument instrument) {
            this.instrument = instrument;
      }
}
AfterAdvice.java:
package com.lnn.advice.after;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

import com.lnn.advice.Audience;

public class AfterAdvice implements AfterReturningAdvice {
      private Audience audience;

      public void setAudience(Audience audience) {
            this.audience = audience;
      }

      @Override
      public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
            audience.applaud();
      }
}
BeforeAdvice.java:
package com.lnn.advice.before;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

import com.lnn.advice.Audience;

public class BeforeAdvice implements MethodBeforeAdvice {
      private Audience audience;

      public void setAudience(Audience audience) {
            this.audience = audience;
      }

      @Override
      public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
            audience.takeSeats();
            audience.turnOffCellPhones();
      }
}
AroundAdvice.java:
package com.lnn.advice.around;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

import com.lnn.advice.Audience;
import com.lnn.advice.PerformanceException;

public class AroundAdvice implements MethodInterceptor {
      private Audience audience;

      public void setAudience(Audience audience) {
            this.audience = audience;
      }

      @Override
      public Object invoke(MethodInvocation invocation) throws Throwable {
            try {
                  audience.takeSeats();
                  audience.turnOffCellPhones();
                  Object returnValue = invocation.proceed();
                  audience.applaud();
                  return returnValue;
            } catch (PerformanceException throwable) {
                  audience.demandRefund();
                  throw throwable;
            }
      }
}


multiple.xml:
<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">
      <bean id="instrumentalist" class="com.lnn.advice.Instrumentalist">
            <property name="song" value="It's My Life" />
            <property name="instrument" ref="violin" />
      </bean>
      <bean id="violin" class="com.lnn.advice.Violin" />
      <bean id="audience" class="com.lnn.advice.Audience" />
      <bean id="afterAdvice" class="com.lnn.advice.after.AfterAdvice">
            <property name="audience" ref="audience" />
      </bean>
      <bean id="beforeAdvice" class="com.lnn.advice.before.BeforeAdvice">
            <property name="audience" ref="audience" />
      </bean>
      <bean id="aroundAdvice" class="com.lnn.advice.around.AroundAdvice">
            <property name="audience" ref="audience" />
      </bean>
      <bean id="instrumentalistproxy" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="target" ref="instrumentalist" />
            <property name="interceptorNames">
                  <list>
                        <value>afterAdvice</value>
                        <value>beforeAdvice</value>
                        <value>aroundAdvice</value>
                  </list>
            </property>
            <property name="proxyInterfaces" value="com.lnn.advice.Performer" />
      </bean>
</beans>
Test.java:
package com.lnn.advice.multiple;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lnn.advice.PerformanceException;
import com.lnn.advice.Performer;

public class Test {
      public static void main(String[] args) throws PerformanceException {
            ApplicationContext context = new ClassPathXmlApplicationContext("aop/multiple.xml");
            Performer performer = (Performer) context.getBean("instrumentalistproxy");
            performer.perform();
      }
}

No comments:

Post a Comment