我有以下代碼:春天@Aspect無法正常自動裝配
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: [email protected]700511f
* ------------------------------------------------------------------------------------------------
*/
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);
}
}
所以問題是,當我啓動應用程序一切都被正確地自動連接,但是當我做出了註解的方法,其中refernces請求該方面,自動裝配的領域神奇地被設置爲空。我不確定春天是使用這個豆還是試圖創建另一個...
注意:這段代碼存在於另外兩個項目中,它的絕對相同(複製/粘貼),並且它的功能完美,但在這裏它打破了...我使用春天版本4.1.5和aspectjrt 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: [email protected]700511f
* ------------------------------------------------------------------------------------------------
*/
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 AOP的AspectJ Maven插件,只需要Spring配置。只有當你想通過加載時或編譯時編織來使用AspectJ的全部功能(有或沒有Spring)時,你需要插件。 – kriegaex