AOP的原理和实例

AOP的原理
对哪些对象在什么位置拦截做什么 <=> <aop:before method="before" pointcut-ref="txPointcut" />
哪些对象
<!-- 定义切面,所有的service的所有方法 --> 
        <aop:pointcut id="txPointcut" expression="execution(* com.masterslave.service.*.*(..))" /> 
dataSourceAspect 是切面要拦截什么。aop:before就是在拦截对象的前面位置。method="before"就是使用切面中的方法处理拦截 
85 <aop:aspect ref="dataSourceAspect" order="-9999"> 
86 <aop:before method="before" pointcut-ref="txPointcut" /> 
87 </aop:aspect> 
如上是用配置文件配置的
还有方法是直接用注解方式
注解中也说明了,拦截范围@Pointcut,在什么位置@Before,做什么@Aspect,具体拦截对象是谁JoinPoint
@Aspect
public class LoggingAspect {


// @Pointcut("execution(* com.samsung.sdsc.legal..*Impl.*(..)) || execution(* com.samsung.sdsc.legal..*Action.*(..))")
@Pointcut("execution(* com.samsung.sdsc.legal..*Impl.*(..))")
public void serviceMethod() {
}


@Before("serviceMethod()")
public void beforeLogging(JoinPoint thisJoinPoint) {
Class<? extends Object> clazz = thisJoinPoint.getTarget().getClass();
Logger logger = Logger.getLogger(clazz);
String methodName = thisJoinPoint.getSignature().getName();
Object[] arguments = thisJoinPoint.getArgs();


StringBuffer argBuf = new StringBuffer();
StringBuffer argValueBuf = new StringBuffer();
int i = 0;
for (Object argument : arguments) {
String argClassName = "";
if (null == argument) {
argClassName = "Null";
argument = "";
} else {
argClassName = argument.getClass().getSimpleName();
}


if (i > 0) {
argBuf.append(", ");
}
argBuf.append(argClassName + " arg" + ++i);
argValueBuf.append(".arg" + i + " : " + argument.toString() + "\n");


}


if (i == 0) {
argValueBuf.append("No arguments\n");
}


StringBuffer messageBuf = new StringBuffer();
messageBuf.append("before executing " + methodName + "("
+ argBuf.toString() + ") method");
messageBuf
.append("\n-------------------------------------------------------------------------------\n");
messageBuf.append(argValueBuf.toString());
messageBuf
.append("-------------------------------------------------------------------------------");
logger.info(messageBuf);


}
}


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/750077/viewspace-2143165/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/750077/viewspace-2143165/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值