Java Spring AOP with XML doesn't work -
i'm testing spring's aop xml features, can't make work.
edit: problem appears when method called constructor of class.
my applicationcontext.xml:
<beans xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <aop:aspectj-autoproxy/> <bean id="aopclass" class="hu.viper.aoptest.aopclass"/> <bean id="mainclass" class="hu.viper.aoptest.mainclass"/> <aop:config> <aop:aspect ref="aopclass"> <aop:pointcut id="pointcutbefore" expression="execution(* hu.viper.aoptest.mainclass.hello(..))"/> <aop:before method="writeaop" pointcut-ref="pointcutbefore"/> </aop:aspect> </aop:config> </beans>
my mainclass.java:
package hu.viper.aoptest; public class mainclass { public mainclass() { this.hello(); } public void hello() { system.out.println("hello world"); } }
my aopclass.java:
package hu.viper.aoptest; import org.aspectj.lang.joinpoint; public class aopclass { public void writeaop(joinpoint joinpoint) { system.out.println("this aop message!"); } }
it builds perfectly, , when run it, prints "hello world" twice on glassfish output in netbeans (i don't know why twice), no aop message. problem code?
use proxy-target-class
in <aop:config>
force use of cglib proxies.
<aop:config proxy-target-class="true"> ....... </aop:config>
in case using proxies mainclass
in client, need refer proxy call hello()
instead of this
. refer this nicely written doc aop proxies understand aiming at.
Comments
Post a Comment