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