NutzCN Logo
问答 请教autoloadCache如何能拦截父类方法?
发布于 2423天前 作者 qq_eb9f8a0a 1219 次浏览 复制 上一个帖子 下一个帖子
标签:

您好,
我现在使用的是1.r.59版本,最新的org-nutz-integration-autoloadcache
我有一个父类,写了大部分公用的dao操作

@IocBean
public class BaseService<T> extends IdNameEntityService<T> {

	@Inject
	protected SqlTemplate sqlTemplate;
	
	@Inject
	@Override
	public void setDao(Dao dao) {
		super.setDao(dao);
	}

	@Cache(expire=600, key="'fetchById:'+#args[0]")
	public T fetch(long id) {
		return dao().fetch(this.getEntityClass(), id);
	}
}

有一个子类继承上面的父类

@IocBean
public class SystemService extends BaseService<System> {

	@Cache(expire=600, key="'fetchById:'+#args[0]")
	public System fetchById(int id){
		Criteria cri = Cnd.cri();
		cri.where().andEquals("id", id);
		return fetch(cri);
	}
}

我在使用auloadCache 的时候发现子类的@Cache可以拦截到,但是直接调用父类的方法,@Cache就拦截不到,想请教一下这种情况有没有其他的做法,因为我有很多子类其实继承BaseService就能满足大部分操作,感谢

@IocBean
public class SystemServiceTest extends NutzTestCase {

	@Inject
	private SystemService systemService;

	@Test
	public void testFetchSystemById() {
		
		int testId = 3;
		
		for(int i=0; i<3; i++){
			System sys1 = systemService.fetch(testId);
			System sys2 = systemService.fetchById(testId);
		}
	}
}
2 回复

原因应该是 SimpleAopMaker只获取当前类的方法,没有获取继承得到的方法, 报个issue讨论一下吧

org.nutz.ioc.aop.SimpleAopMaker.getInterceptorPairList(Ioc, Class<?>)

对,我下断点跟到getInterceptorPairList也是发现了 for (Method method : klass.getDeclaredMethods()),只获取了自己的方法

@Override
    public List<InterceptorPair> getInterceptorPairList(Ioc ioc, Class<?> klass) {
        if (!checkClass(klass))
            return null;
        List<InterceptorPair> list = new ArrayList<InterceptorPair>();
        for (Method method : klass.getDeclaredMethods()) {
            if (!checkMethod(method))
                continue;
            T t = method.getAnnotation(_anno());
            if (t != null) {
                List<? extends MethodInterceptor> _list = makeIt(t, method, ioc);
                if (_list != null) {
                    for (MethodInterceptor mi : _list) {
                        list.add(new InterceptorPair(mi, new SimpleMethodMatcher(method)));
                    }
                }
            }
        }
        if (list.isEmpty())
            return null;
        return list;
    }
添加回复
请先登陆
回到顶部