File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Spring AOP aspect not working." Watch "Spring AOP aspect not working." New topic

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. - Client class who calls the buySquishee method on target class.
2. - Target class which implements the KwikEMart interface.
3. - The advice class.
4. - 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.


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

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



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


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.


public interface KwikEMart {
Squishee buySquishee();

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


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.


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=""

<bean id="kwikMartTarget"

<bean id="welcomeAdvice"

<bean id="welcomeAdvisor"
class=" thodPointcutAdvisor">
<property name="mappedName">
<property name="advice">
<ref bean="welcomeAdvice"/>
<bean id="kwikEMart"
class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="proxyInterfaces">
<property name="interceptorNames">
<property name="target">
<ref bean="kwikMartTarget"/>

I get the output but the advive is not applied.

Thanks and regards,

SCJP 1.6
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

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) ...

Rahul Vs

Joined: Oct 07, 2011
Posts: 1
Thank you for the simple solution.
I agree. Here's the link:
subject: Spring AOP aspect not working.
It's not a secret anymore!