• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Spring AOP aspect not working.

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have same problem.
thank you very much !!!
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the simple solution.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic