论坛首页 Java企业应用论坛

Spring 可以迭代拦截么?

浏览 3389 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-09-17  
我希望通过 Spring 拦截 UserManagerImpl 的 saveUserProperty 和 addUserPropertyValue 两个方法。以下分别是 UserManagerImpl 和 Advice 的两个类的主要代码

    
    public class UserManagerImpl implements UserManager {

    private static Log log = LogFactory.getLog(UserManagerImpl.class);

   
    public void saveUserProperty(long userId, String key, String value) throws
            StatusCodeException {
        UserProperty prop = userPropertyDAO.getUserProperty(userId,
                key, UserProperty.STATUS_ACTIVE);
        if (prop == null) {
            prop = new UserProperty();
            prop.setUserId(userId);
            prop.setPropName(key);
            prop.setPropValue(value);
        } else {
            prop.setUpdateDate(Calendar.getInstance());
            prop.setPropValue(value);
        }
        userPropertyDAO.saveUserProperty(prop);
    }

    public long addUserPropertyValue(long userId, String key, long value) throws
            StatusCodeException {
        long sum = 0;
        String propVaule = getUserProperty(userId, key);
        if (propVaule != null) {
            sum = StringUtils.parseLong(propVaule, 0);
        }
        sum = sum + value;
        saveUserProperty(userId, key, String.valueOf(sum));
        return sum;
    }

}



注意:addUserPropertyValue 中调用了 saveUserProperty

  public UserStatisticAdvice() {
    }

    public void afterReturning(Object returnValue, Method method,
                               Object[] args, Object target) throws
            Throwable {

        if (target == null)return;
        String methodName = method.getName();

        log.debug("advice running...method="+methodName);

        if (target instanceof UserManager) { 

            if ("addUserPropertyValue".equals(methodName)) {
                long userId = ((Long)args[0]).longValue();
                String key = (String)args[1];
                if (userId > 0 && key != null){
                    ...
                }
            }
            
            if ("saveUserProperty".equals(methodName)) {
                long userId = ((Long)args[0]).longValue();
                String key = (String)args[1];
                String value = (String)args[2];
                if (userId > 0 && key != null && value != null){
                    ...
                }
            }
        }

    }


}


按照我的理解,
当 UserManger 的 addUserPropertyValue 被调用时,拦截器应该被拦截到,
由于被拦截后, UserManger 又调用了 saveUserProperty,故又会被拦截器拦截到。

但是跟踪结果,却发现 只有 addUserPropertyValue 被调用时,Spring 进行了拦截。而后续的 saveUserProperty 却没有被拦截到。

请问,我希望两个函数都被拦截到,有办法解决我的需求么?
   发表时间:2006-09-18  
这种解决方法很好?
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics