Spring AOP注入PortletRequest

发布于 2024-09-11 10:01:34 字数 734 浏览 10 评论 0原文

我试图在我的方面类中注入一个 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 - 我错过了什么吗?

I'm trying to inject a portletrequest in my aspect class

@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);
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨轻弹 2024-09-18 10:01:34

好的,经过一些实验后解决了这个问题,也许对其他人有用。

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

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.

‖放下 2024-09-18 10:01:34

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

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

示例代码:

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

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文