2016-07-01 11 views
1

をオートワイヤリングではない私は、次のコードを持っている:春@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); 
    } 
} 

誰かがこの問題を解決するためにどのように任意のアイデアを持っていますか?

答えて

0

.OKだから、Springは責任を負うことはなく、ただ1つのBeanを作成するだけです。 私はプロジェクトxmlを見ましたが、プラグインaspectj-maven-plugin(aspectj-maven-plugin)があり、クラスの別のインスタンスを作成します。@Aspectはこのインスタンスを使用します。プラグインを削除せずに春の豆だけを作ることができるなら、おそらくコンフィグレーションに除外オプションがあります。

+0

Spring AOP用のAspectJ Mavenプラグインは必要ありません。Springの設定のみです。ロードタイムやコンパイル時にAspectJ(Springの有無にかかわらず)をフルに使いたい場合にのみ、プラグインが必要です。 – kriegaex

0

私はなぜあなたがjoinPoint.proceed()を使っていないのか分かりません。あなたの周りのアドバイスで。 働くために周囲のアドバイスをすることが不可欠です。

@Around("@annotation(com.cooldomain.coolapp.application.rest.session.SessionRequired) && within(com.cooldomain.coolapp.application..*)") 
    public Object proceedWithValidSession(ProceedingJoinPoint joinPoint) throws Throwable 
    { 
//your code 

//solution 
joinPoint.proceed(); 
    } 

これを試して、何が起こっているのかを教えてください。