I'm new to Spring and AOP and I have worked through examples and tutorials but I can't get my own to work. I would appreciate any advice. Everything compiles and runs but it only executes the code in the method and does not execute the code in the aspect. Here is my code and config:
Trans.java:
TransAspect.java:Code:package example; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.METHOD; @Target({METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface Trans { String name() default ""; String type() default ""; }
SampleApp.java:Code:package example; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; @Aspect public class TransAspect { @Around(value = "@annotation(trans)", argNames = "pjp, trans") public Object beginEndTransaction(ProceedingJoinPoint pjp, Trans trans) throws Throwable { System.out.println("*** BEGIN TRANS ***"); if (trans.name() != null) System.out.println("*** name: " + trans.name() + " ***"); if (trans.type() != null) System.out.println("*** type: " + trans.type() + " ***"); try { return pjp.proceed(); } finally { System.out.println("*** END TRANS ***"); } } }
springappctx.xml:Code:package example; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SampleApp { private static final String SPRING_APP_CTX = "springappctx.xml"; public static void main(String[] args) throws Exception { ApplicationContext ctx = new ClassPathXmlApplicationContext(SPRING_APP_CTX); SampleApp app = (SampleApp) ctx.getBean("sampleApp"); app.doSomething(); } @Trans private void doSomething() { System.out.println("*** DOING SOMETHING ***"); } }
This is the output:Code:<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy> <aop:include name="transAspect"/> </aop:aspectj-autoproxy> <bean id="transAspect" class="example.TransAspect"/> <bean id="sampleApp" class="example.SampleApp"/> </beans>
Code:Apr 15, 2011 11:15:06 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1027b4d: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1027b4d]; startup date [Fri Apr 15 11:15:06 CDT 2011]; root of context hierarchy Apr 15, 2011 11:15:06 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions from class path resource [springappctx.xml] Apr 15, 2011 11:15:07 AM org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1027b4d]: org.springframework.beans.factory.support.DefaultListableBeanFactory@e2dae9 Apr 15, 2011 11:15:07 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@e2dae9: defining beans [org.springframework.aop.config.internalAutoProxyCreator,transAspect,sampleApp]; root of factory hierarchy *** DOING SOMETHING ***


Reply With Quote
