2

我正在尝试在我的方面类中注入一个 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);
}

只给我一个

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No matching bean of type [javax.portlet.PortletRequest] found for dependency: 
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

例外

但我可以自动接线HttpServletRequest- 我错过了什么吗?

4

2 回答 2

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 上下文中运行,否则此时没有可用的请求属性。

于 2010-07-27T14:30:21.337 回答
0

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

至少:键“请求”的 HttpServletRequest/PortletRequest 参考,键“会话”的 HttpSession/PortletSession 参考。

示例代码:

RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
// According to JavaDoc, PortletRequest should be available
PortletRequest request = (PortletRequest) attrs.resolveReference("request");
于 2013-07-16T21:00:57.767 回答