Announcement Announcement Module
Collapse
No announcement yet.
Around advice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Around advice

    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
    Code:
    <?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>
    Performer.java
    Code:
    package SampleAOP2;
    
    public interface Performer {
    void perform();
    }
    Juggler.java
    Code:
    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");
    	}
    
    }
    Singer.java
    Code:
    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");
    	}
    }
    Audience.java
    Code:
    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");
    	}
    
    }
    AudienceAroundAdvice.java
    Code:
    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;
    	}
    
    }
    MainClass.java
    Code:
    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();
    	}
    
    }
    i am using MainClass.java to debug/run the application

  • #2
    some body please check my way to use Around Advice and Correct me

    Comment


    • #3
      You defined in your example the behavior you want applied (around advice), where to apply it (.*perform), but there is no entity that creates those advised elements.
      Please, check the reference documentation (http://static.springframework.org/sp...e/aop-api.html).

      In your case you can add, for example:
      Code:
      <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

      Comment

      Working...
      X