代码之家  ›  专栏  ›  技术社区  ›  onigunn

Spring AOP注入PortletRequest

  •  2
  • onigunn  · 技术社区  · 15 年前

    @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 -我错过什么了吗?

    2 回复  |  直到 15 年前
        1
  •  0
  •   onigunn    15 年前

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

        2
  •  0
  •   Arlo    12 年前

    我选择使用 resolveReference解决方案

    至少:键的HttpServletRequest/PortletRequest引用 “request”,以及键的HttpSession/PortletSession引用 “会话”。

    示例代码:

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