java - Spring @Aspect 未正确 Autowiring

标签 java spring dependency-injection aspectj

我有以下代码:

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/

相关文章:

java - 删除在 lucene 索引中不起作用(3.5.0)

java - 在线程实例上使用 sleep(long) 的副作用

带 Tiles : static webresources not found when deploying as jar 的 Spring Boot

java - 为什么Spring MVC会以404响应并报告“在DispatcherServlet中未找到带有URI […]的HTTP请求的映射”?

c# - 如何打破存储库之间的循环依赖

dependency-injection - 实用单例和依赖注入(inject)问题

java - Servlet 不应该有可变的实例字段误报 Spring Autowiring (squid :S2226)

java - 计算矩形周长和面积

java - 获取 GITHUB 存储库下的所有项目

java - URI 映射不可访问