使用Ajc编译器与Spring问题AspectJ
当我尝试使用ajc编译器弹簧aspectj时,我得到跟随errror.when我正在删除aspectj然后代码工作正常是编辑时间编织有什么导致问题
caused by: java.lang.ExceptionInInitializerError at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) at java.lang.reflect.Constructor.newInstance(Constructor.java:513) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100) at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877) ... 31 more Caused by: java.lang.NullPointerException at com.cdf.dfdxc.aspect.logging.LoggingAspect.logEntry(LoggingAspect.java:76) at com.cdfc.fdged.uow.UdfdFactory.(UOWfdy.java:1)
Spring bean文件
类路径:/资源/hibernate的映射
com.ibm.db2.jcc.DB2Driver jdbc:db2://20.15.29.108:50001/XC128086 db2dut$ db2dut org.hibernate.dialect.DB2Dialect update org.hibernate.context.ThreadLocalSessionContext org.hibernate.connection.C3P0ConnectionProvider 3 30 3 0 0 0 thread org.hibernate.cache.HashtableCacheProvider true false false false false
记录方面代码
@Pointcut("within(com.csc.exceed.uow.*)") public void loggingAspect() { } // invoked before the method execution @Before("loggingAspect()") public void logEntry(JoinPoint joinPoint) { Class clazz = joinPoint.getTarget().getClass(); String name = joinPoint.getSignature().getName(); if (ArrayUtils.isEmpty(joinPoint.getArgs())) { if (!(name.startsWith("get")) || (name.startsWith("set"))) logger.log(LogLevel.INFO, clazz, null, BEFORE_STRING, name, constructArgumentsString(clazz, joinPoint.getArgs())); } else { logger.log(LogLevel.INFO, clazz, null, BEFORE_WITH_PARAMS_STRING, name, constructArgumentsString(clazz, joinPoint.getArgs())); } }
很难知道哪条76线是:
at com.cdf.dfdxc.aspect.logging.LoggingAspect.logEntry(LoggingAspect.java:76)
但是你正在使用非常激进的切入点
@Pointcut("within(com.csc.exceed.uow.*)") public void loggingAspect() {}
这匹配所有类型的事件,不仅包括方法执行,还包括静态和实例初始化,字段访问等(请参阅AspectJ快速参考中的原始切入点概述)。
如果其中任何一个:
-
为
joinPoint.getTarget()
返回null
,这一行将抛出一个NPE:Class extends Object> clazz = joinPoint.getTarget().getClass();
-
对于
joinPoint.getSignature()
返回null
,这一行将抛出一个NPE:String name = joinPoint.getSignature().getName();
-
此外,您在这里检查null或空args:
if (ArrayUtils.isEmpty(joinPoint.getArgs())) { if (!(name.startsWith("get")) || (name.startsWith("set"))) logger.log(LogLevel.INFO, clazz, null, BEFORE_STRING, name,
但你仍在使用args:
constructArgumentsString(clazz, joinPoint.getArgs()));
这也可能会抛出NPE,具体取决于
constructArgumentsString()
的代码
检查第76行中发生了哪些,并且您的候选人失败了。 但我的第一个提示是用execution
或call
切入点替换你积极的catch-all切入点。
我会使用以模块化方式定义的组合切入点:
// you can easily reuse this @Pointcut("within(com.csc.exceed.uow.*)") public void myApp() {} // and this @Pointcut("execution(* *.*(..))") public void methodExecution(){} // but this is the pointcut you are actually matching @Pointcut("myApp() && methodExecution()") public void methodExecutionInMyApp(){} @Before("methodExecutionInMyApp()") public void logMethodExecutions(JoinPoint jp){ // your code here }
以下是如何使该类具有NPE安全性:
Class> clazz = joinPoint.getTarget() !=null ? joinPoint.getTarget().getClass() : null;