Hi,
I am trying to write a simple standalone program using the @before annotation of Spring AOP, but it is not working as expected. I am posting below the classes and Beans.xml file that i have generated for the same. THis is a very basic implementation but can't understand why is not working. Any help is much appreciated
Aspect Class:
Interface which needs to be advisedCode:package com.royz.test.aop; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; @Aspect public class LoggerAspect { @Before("execution(* com.royz.test.Performer.playTune(..))") public void logplaytune(){ System.out.println("Is about to play tune"); } }
package com.royz.test;
Implementation of the InterfaceCode:public interface Performer { public void playTune(); public void bowToApplause(); public void playtuneThrowsExcepttion() throws Exception; }
The class which is launching the Spring containerCode:package com.royz.test; public class PerformerImpl implements Performer { public void playTune() { System.out.println("Playing tune"); } public void bowToApplause() { // TODO Auto-generated method stub } public void playtuneThrowsExcepttion() throws Exception { // TODO Auto-generated method stub } }
The Beans.xml fileCode:package com.royz.test; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.FileSystemResource; public class SprintTestLaunch { public static void main(String[] args) { BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("E:\\xyz\\Spring STS\\workspace\\testaop\\src\\main\\java\\com\\royz\\test\\beans.xml")); Performer prfmr = (Performer)beanFactory.getBean("performer"); prfmr.playTune(); } }
When i am running this code the output i am getting is 'playing tune' but i am not getting the message that should be printed as part of the @Before advice.Code:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <aop:aspectj-autoproxy /> <bean id="performer" class="com.royz.test.PerformerImpl" /> <!-- Aspect --> <bean id="logAspect" class="com.royz.test.aop.LoggerAspect" /> </beans>
Regards,
Royz


Reply With Quote