我有以下代码:
package com.cooldomain.coolapp.application.rest.session;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SessionAspect
{
@Autowired
private HttpServletRequest request;
@Autowired
private SessionService sessionService;
@PostConstruct
public void init()
{
/*
* XXX Note: On application startup this piece of code prints the right values and I can see that they have been autowired correctly:
* ------------------------------------------------------------------------------------------------
* request: Current HttpServletRequest
* sessionService: com.cooldomain.coolapp.application.rest.session.SessionService@3700511f
* ------------------------------------------------------------------------------------------------
*/
System.out.println("request: " + request);
System.out.println("sessionService: " + sessionService);
}
@Around("@annotation(com.cooldomain.coolapp.application.rest.session.SessionRequired) && within(com.cooldomain.coolapp.application..*)")
public Object proceedWithValidSession(ProceedingJoinPoint joinPoint) throws Throwable
{
/*
* XXX Note: But when I make a request to the annotated with @SessionRequired method i get the following output and hence a nullpointer exception afterwards:
* ------------------------------------------------------------------------------------------------
* request: null
* sessionService: null
* ------------------------------------------------------------------------------------------------
*/
System.out.println("request: " + request);
System.out.println("sessionService: " + sessionService);
return setSessionAsFirstArgument(joinPoint);
}
private Object setSessionAsFirstArgument(ProceedingJoinPoint joinPoint) throws Throwable
{
Object[] args = joinPoint.getArgs();
args[0] = sessionService.getSession(request);
return joinPoint.proceed(args);
}
}
所以问题是,当我启动应用程序时,所有内容都会正确自动连接,但是当我向引用方面的带注释方法发出请求时,自动连接字段会神奇地设置为 null。我不确定 spring 是否使用这个 bean 或者正在尝试创建另一个 bean...
注意:这段代码存在于其他两个项目中,并且绝对相同(复制/粘贴),并且在那里它工作得很好,但在这里它中断了......我使用 spring 版本 4.1.5 和方面jrt 1.8.5,就像在其他两个项目中一样。
我已经完成了以下解决方法,该方法有效,但它很难看,我想将其删除:
package com.cooldomain.coolapp.application.rest.session;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SessionAspect
{
@Autowired
private HttpServletRequest request;
@Autowired
private SessionService sessionService;
// WORKAROUND
private static SessionAspect sessionAspect;
@PostConstruct
public void init()
{
/*
* Note: On application startup this piece of code prints the right values and I can see that they have been autowired correctly:
* ------------------------------------------------------------------------------------------------
* request: Current HttpServletRequest
* sessionService: com.cooldomain.coolapp.application.rest.session.SessionService@3700511f
* ------------------------------------------------------------------------------------------------
*/
System.out.println("request: " + request);
System.out.println("sessionService: " + sessionService);
// WORKAROUND
sessionAspect = this;
}
@Around("@annotation(com.cooldomain.coolapp.application.rest.session.SessionRequired) && within(com.cooldomain.coolapp.application..*)")
public Object proceedWithValidSession(ProceedingJoinPoint joinPoint) throws Throwable
{
/*
* XXX Note: But when I make a request to the annotated with @SessionRequired method i get the following output and hence a nullpointer exception afterwards:
* ------------------------------------------------------------------------------------------------
* request: null
* sessionService: null
* ------------------------------------------------------------------------------------------------
*/
System.out.println("request: " + request);
System.out.println("sessionService: " + sessionService);
// WORKAROUND
return sessionAspect.setSessionAsFirstArgument(joinPoint);
}
private Object setSessionAsFirstArgument(ProceedingJoinPoint joinPoint) throws Throwable
{
Object[] args = joinPoint.getArgs();
args[0] = sessionService.getSession(request);
return joinPoint.proceed(args);
}
}
有人知道如何解决这个问题吗?
最佳答案
.好吧,事实证明 Spring 不应该受到指责,它只创建了一个 bean。 我看到了项目 xml,有一个插件 aspectj-maven-plugin (aspectj-maven-plugin),它在 spring 上下文之外创建该类的另一个实例,并且 @Aspect 使用此实例 - 而不是 spring 实例 如果可以只制作 spring bean,而不删除插件,将在网上寻找解决方案...也许配置中有一个排除选项
关于java - Spring @Aspect 未正确 Autowiring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38144222/