项目进入ST的时候,为了定位问题处于哪个模块。需要明确知道出入中的参数的值以追寻问题的根源。这经常需要编写大量的打印语句。可能有些人会写这些东西,但是有些人却忘记写了。也有人通过完整的Unit Testing确信自己的代码没有问题。可是当问题出现时,事情不会这么容易解决。因为人家可能也做了测试,只不过漏了测试某些分支而导致问题发生了。这个时候就需要证据了。一种方法是把出入口参数的值一一打印出来。可是这些语句要是散落在各个类里就太丑陋了,维护起来也费劲。
利用AOP我们可以一下这方面的事情。以下的主要的代码片段:
public Object cut(ProceedingJoinPoint pjp)
throws Throwable {
String instanceMethod =
pjp.getTarget().getClass() + "@" + pjp.getTarget().hashCode() + "]"+"." + pjp.getSignature().getName();
if(this.isPrintArguments())
{
this.printArguments(instanceMethod, pjp.getArgs());
}
Object rel = null;
long start = System.nanoTime();
try
{
rel = pjp.proceed();
}catch(Exception e)
{
if(this.isCatchException())
{
if(log.isErrorEnabled())
{
log.error("Exception occure while executing.", e);
}
}
throw e;
}
long end = System.nanoTime();
if(this.isPrintInvocationTime())
{
if(log.isDebugEnabled())
{
log.debug("["+(end - start)+"] nanoseconds elapsed executing [" + instanceMethod );
}
}
if(this.isPrintReturnValue())
{
printReturnVal(rel);
}
log.debug("");
return rel;
}
样本配置如下:
<aop:config>
<aop:aspect id="com.bee.fw.core.methodInvocationAspect" ref="com.bee.fw.core.MethodInvocationDetailsInspector">
<aop:pointcut id="com.bee.fw.core.cutExpresseion"
expression="execution(* com.bee.fw.debug.Aspect.*(..))" />
<aop:around pointcut-ref="com.bee.fw.core.cutExpresseion" method="cut" />
</aop:aspect>
</aop:config>
<bean id="com.bee.fw.core.MethodInvocationDetailsInspector" class="com.bee.fw.debug.MethodInvocationDetailsInspector">
<property name="printInvocationTime" value="true" />
<property name="order" value="100" />
</bean>
<bean id="com.bee.fw.core.Aspect" class="com.bee.fw.debug.Aspect">
</bean>
这个代码依赖的类库有:
Spring >= 2.0
Commons-io >= 1.3.1
Commons-logging >=1.1
Commons-beanUtils > 1.8.0
Log4j >= 1.2.13
有兴趣的可以查看附件的源码和程序执行的效果。
分享到:
相关推荐
Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
sisu-guice-3.1.3-no_aop.jar
开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具 aopalliance-1.0开发工具...
guice-2.0-no_aop.jar
源代码是spring-boot添加aop切面功能的demo,简单易懂.
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
spring-3.2-aop.jar
代理模式与动态代理--Spring AOP原理.doc
java运行依赖jar包
AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析-SpringAOP中定义的类图AOP流程源码分析...
spring AOP 开发所需的两个 jar 包:aspectjweaver-1.8.7和aopalliance1.0
java运行依赖jar包
Jet-AOP一个Android AOP框架,使用注释, AspectJ等技术实现
aspectj.jar-aspectjweaver.jar-aopalliance.jar
赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...
my-spring.rar-Spring-AOP源码Demo
aopalliance.jar、spring-aop-4.1.6.RELEASE.jar、spring-aspects-4.1.6.RELEASE.jar
springboot结合自定义注解整合动态数据源实现通过注解切换,事务回滚springboot结合自定义注解整合动态数据源实现通过注解切换,事务回滚springboot结合自定义注解整合动态数据源实现通过注解切换,事务回滚