をオートワイヤリングではない私は、次のコードを持っている:春@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);
}
}
をだから、問題は、私は起動時にアプリケーションのすべてが正しくautowiredますが、私はアノテーション付きメソッドへの要求を行う際に、どのreferncesということです側面では、autowiredフィールドは魔法のようにnullに設定されます。私は春がこの豆を使用しているのか、別のものを作成しようとしているのか分かりません...
注:このコードは他の2つのプロジェクトに存在していて、絶対に同じです(コピー/貼り付け)しかしここでそれは壊れて...私は春のバージョン4.1.5とaspectjrt 1.8.5を2つの他のプロジェクトのように使用しています。
Iワークス次の回避策を、やったが、それは醜いだと私はそれを削除したい:
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