Wednesday 15 September 2010

Pointcut expression issues in Logging, Spring AOP with log4j -



Pointcut expression issues in Logging, Spring AOP with log4j -

i have apply logging info methods belonging bundle , sub-packages com.mycomp.** .i have tried lots of pointcuts given in spring reference manual sadly no 1 working out me. using

@pointcut("execution(* com.mycomp..(..))") private void businessservice() {} // signature

what getting

caused by: java.lang.illegalargumentexception: pointcut not well-formed: expecting 'name pattern cannot finish ..' @ character position 27 execution(* com.mycomp..(..))

in documentation given that

@pointcut("execution( com.xyz.someapp..service..(..))")

i have tried using execution(com.mycomp..(..)) got similar exception.

on using pointcut @around(value="execution(* xyz.package.foo.bar..*(..))") suggested james getting new kind of exception when starting server.

caused by: java.lang.illegalstateexception: cannot convert value of type [$proxy76 implementing org.springframework.context.applicationcontextaware,org.springframework.aop.springproxy,org.springframework.aop.framework.advised] required type [com.mycompname.bancscontextaware] property 'bancscontextaware': no matching editors or conversion strategy found @ org.springframework.beans.typeconverterdelegate.convertifnecessary(typeconverterdelegate.java:264) @ org.springframework.beans.beanwrapperimpl.convertifnecessary(beanwrapperimpl.java:448) ... 57 more

later on debugging found out in 1 of classes using object.getclass().getannotation() , proxy object created spring aop not have annotation. because of getting null pointer exception. sorted using aopproxyutils.ultimatetargetclass(someobject) problem have final classes , enum within sub-packages of com.mycom.. because of getting

nested exception org.springframework.aop.framework.aopconfigexception: not generate cglib subclass of class [class com.mycom.util.bancsserviceprovider]: mutual causes of problem include using final class or non-visible class; nested exception java.lang.illegalargumentexception: cannot subclass final class class com.mycom.util.bancsserviceprovider @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:529)

so how exclude final classes , enum spring aop pointcut.

the pointcut , method looking this:

@around(value="execution(* xyz.package.foo.bar..*(..))") public object beforeadvice(proceedingjoinpoint jp) throws throwable { //get log4j logger target class. logger logger = logmanager.getlogger(jp.gettarget().getclass()); logger.info(jp.gettarget().getclass().getname() + " " + jp.getsignature().getname() + " " + "hit:"); object returnval = jp.proceed(jp.getargs()); logger.info(jp.gettarget().getclass().getname() + " " + jp.getsignature().getname() + " " + "finished:"); homecoming returnval; }

the above code log class , method name 'hit' before enters method , 'finished' 1 time done in next manner:

xyz.package.foo.bar.service.getperson: hit: xyz.package.foo.bar.dao.getperson: hit: xyz.package.foo.bar.dao.getperson: finished: xyz.package.foo.bar.service.getperson: finished: ...

if want seek follow multithreaded execution of methods, utilize following:

@around(value="execution(* xyz.package.foo.bar..*(..))") public object beforeadvice(proceedingjoinpoint jp) throws throwable { logger logger = logmanager.getlogger(jp.gettarget().getclass()); string executionid = uuid.randomuuid().tostring(); logger.info(jp.gettarget().getclass().getname() + " " + jp.getsignature().getname() + " " + "hit:" + executionid); object returnval = jp.proceed(jp.getargs()); logger.info(jp.gettarget().getclass().getname() + " " + jp.getsignature().getname() + " " + "finished:" + executionid); homecoming returnval; }

you uuid per threaded invocation , allow track how fast these methods executing across threads. utilize code when trying method times performance testing , works wonders.

make sure have class declared in aspect.

spring spring-aop

No comments:

Post a Comment