This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Spring and the fly likes Spring AOP aspect not working. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring AOP aspect not working." Watch "Spring AOP aspect not working." New topic
Author

Spring AOP aspect not working.

Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
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


SCJP 1.6
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ]

[My Blog]
All roads lead to JavaRanch
Pradeep Kumar
Ranch Hand

Joined: Sep 11, 2007
Posts: 77
Thanks a lot.
Cainiao Zou
Ranch Hand

Joined: Mar 03, 2009
Posts: 36
I have same problem.
thank you very much !!!
Trilochan Bharadwaj
Ranch Hand

Joined: Feb 02, 2009
Posts: 100
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

Joined: Oct 07, 2011
Posts: 1
Thank you for the simple solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Spring AOP aspect not working.