gpt4 book ai didi

java - Spring AOP注入(inject)PortletRequest

转载 作者:行者123 更新时间:2023-11-29 09:24:55 24 4
gpt4 key购买 nike

我试图在我的方面类中注入(inject)一个 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)}

异常

但我可以 Autowiring HttpServletRequest - 我是否遗漏了什么?

最佳答案

好的,经过一些实验解决了它,也许它对其他人有用

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

关于java - Spring AOP注入(inject)PortletRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3335439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com