在面向切面编程方面我自己比较喜欢spring的标注型切点的做法,主要这种切点类类型比较容易控制,并且是方法级别配制.前一段时间使用这种类型的切点出现了一个问题,现象是有些标注型切点可以切入,而另外一些不行,问题现在是找到了具体内在的原因还有待分析.那些无法切入的方方法特征如下:
假设有三个类A-->AB-->ABC,AB继承A,ABC继承AB,在这三个类中都有一个update方法,是覆盖关系
A中的方法是:
public Integer update(B baseModel) {
Assert.notNull(baseModel, "必须指定参数对象");
Integer count = getMyBatisDaoManager().update(baseModel);
baseModel.setResult(baseModel);
return count;
}
AB中的方法是:
public Integer update(B treeBaseModel) {
Assert.notNull(treeBaseModel, "参数对象不能为空");
B fromDb = this.getMyBatisDaoManager().get(treeBaseModel, false);
//规范记录的唯一标识
if(fromDb==null){
throw new CoreBusinessException("指定的唯一标识存在问题");
}
ABC中的方法是:
@NeedUE
public Integer update(CoreOrganization coreOrganization) {
.... return count;
}
在切面中定义的切点如下:
@Around("@annotation(needUE)")
public Object service(ProceedingJoinPoint joinPoint, NeedUE needUE) throws Throwable {
BaseModel<?> baseModel = null;
Object firstArg = joinPoint.getArgs()[0];
此时如果调用ABC对象的update方法是切点是是失效的,服务代码切入失败,但是如果在AB的update方法上打上@NeedUE标注则在ABC对象上调用update服务代码可以切入,通过多次测试发现标注型切点在覆盖类型的方法添加标注,如果要让切点生效,它的超类方法也需要添加此标注,处于继承体系的最顶级那个方法可以除外.