2016-07-01 39 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); 
    } 
} 

所以問題是,當我啓動應用程序一切都被正確地自動連接,但是當我做出了註解的方法,其中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); 
    } 
} 

是否有人有任何想法如何解決這一問題?

回答

0

。所以事實證明,春天不是怪,它只創建一個bean。 我看到了項目xml,並且有一個插件aspectj-maven-plugin(aspectj-maven-plugin)在spring上下文中創建另一個類的實例,@Aspect使用這個實例 - 不是彈簧的一個 將尋找soltion在網上如果只能製作spring bean,不用刪除插件...也許在配置中有一個排除選項

+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(); 
    } 

請試試這個,並說出發生了什麼。