Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Spring AOP aspect not working.

 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am new to spring framework. I was trying the AOP advice weaving and tried a sample advice to weave to a class. I am not getting the advice weaved. Here is the code snippet below. Can somebody tell me what is the error as i am not able to figure it exactly.

Following are the files whoise source code is given below. Kindly look into this as the advice is not been printed before the method call and revert back.

1. AdviceApp.java - Client class who calls the buySquishee method on target class.
2. ApuKwikEMart.java - Target class which implements the KwikEMart interface.
3. WelcomeAdvice.java - The advice class.
4. KwikEMart.java - The interface defining the contract.
5.hello.xml - The xml configuration file.

// Below is the client class which will invoke the method to which advice is applied.

package com.spring.training.advice;

import org.springframework.aop.framework.ProxyFactoryBean ;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlAp plicationContext;



public class AdviceApp {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("hello.xml");
ApuKwikEMart kiwiMart = (ApuKwikEMart)factory.getBean("kwikMartTarget");


kiwiMart.buySquishee();

}
}


//Below is the class which has the method buySquishee method i.e the target class which implements KwikEMartInterface.

package com.spring.training.advice;

public class ApuKwikEMart implements KwikEMart {

private boolean squisheeMachineEmpty;

public Squishee buySquishee() {
System.out.println("Came here for SURE");
return new Squishee();
}
}

//Below is the interface which defines the contract and will be implemented by the target class.

package com.spring.training.advice;

public interface KwikEMart {
Squishee buySquishee();
}


//Below is the Squishee class whose object will be returned by the target method.

package com.spring.training.advice;

public class Squishee {

}


//Below is the advice class which will step in to the call made to the buySquishee method of the target class and prints a message.

package com.spring.training.advice;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class WelcomeAdvice implements MethodBeforeAdvice {

public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
//Customer customer = (Customer)arg1[0];
System.out.println("Hello How are you doing");
}

}


//Also last is the xml file which weaves the beans together Name is hello.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schem...ontext-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<bean id="kwikMartTarget"
class="com.spring.training.advice.ApuKwikEMart"/>

<bean id="welcomeAdvice"
class="com.spring.training.advice.WelcomeAdvice"/>

<bean id="welcomeAdvisor"
class="org.springframework.aop.support.NameMatchMe thodPointcutAdvisor">
<property name="mappedName">
<value>buySquishee</value>
</property>
<property name="advice">
<ref bean="welcomeAdvice"/>
</property>
</bean>
<bean id="kwikEMart"
class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="proxyInterfaces">
<value>com.spring.training.advice.KwikEMart</value>
</property>
<property name="interceptorNames">
<list>
<value>welcomeAdvisor</value>
</list>
</property>
<property name="target">
<ref bean="kwikMartTarget"/>
</property>
</bean>
</beans>

I get the output but the advive is not applied.

Thanks and regards,
Pradeep
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are getting the bean here. If you don't fo through the proxy, AOP is not going to work. Replace your AdviceApp with:

Or use auto-proxying.

You can refer to this Journal article if you don't understand how to use a proxy.

(and don't forget to use to enclose your source. It makes it easier to read)
[ May 12, 2008: Message edited by: Christophe Verre ]
 
Pradeep Kumar
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
 
Cainiao Zou
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have same problem.
thank you very much !!!
 
Trilochan Bharadwaj
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris great write up! But you haven't talked about Introductions and Aspect instantiation models ... any one out there that you know (The documentation explains it very briefly ... cant really get a full view out of it) ...

Trilochan
 
Rahul Vs
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the simple solution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic