I'm trying to inject a portletrequest in my aspect class
@Autowired(required = true)
private PortletRequest request;
@开发者_如何学GoBefore("execution(* de.ac.mis.dao.*.getSessionFactory())")
public void setUsername() {
    System.out.println("Now I'm setting the username " + this.request);
}
Only gives me an
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)}
exception
but I can autowire HttpServletRequest - am I missing something?
Okay solved it after some experimenting, maybe it could be useful for someone else
@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);
}
Important for these config is that the requesting resources (portlets or servlets) must run in spring context else no requestattributes are available at this point.
I opted to use resolveReference after looking at the JavaDocs, which claims:
At a minimum: the HttpServletRequest/PortletRequest reference for key "request", and the HttpSession/PortletSession reference for key "session".
Example Code:
RequestAttributes attrs = RequestContextHolder.getRequestAttributes();
// According to JavaDoc, PortletRequest should be available
PortletRequest request = (PortletRequest) attrs.resolveReference("request");
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论