2010-07-26 79 views
2

我想在我的切面類的Spring AOP注入的PortletRequest

@Autowired(required = true) 
private PortletRequest request; 

@Before("execution(* de.ac.mis.dao.*.getSessionFactory())") 
public void setUsername() { 
    System.out.println("Now I'm setting the username " + this.request); 
} 

中注入的PortletRequest只給了我一個

​​

例外

但我可以自動裝配HttpServletRequest - 我是遺漏了什麼?

回答

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或小服務程序)必須在春天上下文中運行其他沒有requestattributes可用在此刻。

0

我選擇使用resolveReference看的JavaDoc,其要求後:

在最低限度:對於鍵 「請求」 HttpServletRequest的/的PortletRequest參考,並且在HttpSession/PortletSession中用於密鑰參考 「會話」。

例如代碼:

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