我想在建議之前獲取Spring AOP中的響應對象。如果會話無效,我想重定向到登錄頁面,但無法獲取Before advice方法中的HttpServletResponse對象。如何在Spring AOP中獲取HttpServletRequest和HttpServletResponse對象
試着用下面的方法。
@Autowired
private HttpServletResponse response;
public void setResponse(HttpServletResponse response) {
this.response = response;
}
堆棧跟蹤:
caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.servlet.http.HttpServletResponse com.****.****.aspect.LogProvider.response; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.servlet.http.HttpServletResponse] 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)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 33 more
任何幫助將不勝感激。
而是自動裝配的,你嘗試過嗎? 'HttpServletRequest request =((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())。getRequest(); ' –
感謝您的回覆。我會試着用這個 –
我需要HttpServletResponse對象。 –