时间:2010-07-26 13:54:12

标签: java spring aop portlet aspects

2 个答案:

答案 0 :(得分:0)

好的经过一些实验后解决了,也许对其他人来说可能有用

@Before("execution(* de.ac.mis.dao.acDynamicUserSessionFactory.getSessionFactory())")
public void setUsername(JoinPoint joinPoint) {
    acDynamicUserSessionFactory dao = (acDynamicUserSessionFactory) joinPoint.getTarget();
    RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
    String userName = "";
    if (requestAttributes instanceof PortletRequestAttributes) {
        PortletRequest request = ((PortletRequestAttributes) requestAttributes).getRequest();
        userName = request.getRemoteUser();
    } else if (requestAttributes instanceof ServletRequestAttributes) {
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        userName = request.getRemoteUser();
    }
    dao.setUserName(userName);
    this.log.debug("acUserSessionfactory was set for user: " + userName);
}

对于这些配置重要的是请求资源(portlet或servlet)必须在spring上下文中运行,否则此时没有可用的任何请求属性。

答案 1 :(得分:0)

在查看JavaDocs后,我选择使用 resolveReference ,声称:

  

至少:密钥的HttpServletRequest / PortletRequest引用   “请求”,以及密钥的HttpSession / PortletSession引用   “会话”。

示例代码:

RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
// According to JavaDoc, PortletRequest should be available
PortletRequest request = (PortletRequest) attrs.resolveReference("request");