繁体   English   中英

Spring @configurable NullPointerException,@autowired 服务是 null

[英]Spring @configurable NullPointerException, @autowired service is null

我正在尝试在 spring 中使用 @configurable 在我创建的非 bean class 中使用 @autowired 服务。
无论我尝试什么,它都不想再工作了。
有人可以告诉我我做错了什么吗? (我做了一些研究,但我现在完全无能为力)
这是我制作的一个非常基本的代码示例:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo2</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

配置ComponentScan class

package com.example.demo2;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.aspectj.EnableSpringConfigured;

@Configuration
@ComponentScan
@EnableSpringConfigured
public class AspectJConfig
{
    
}

@SpringBootApplication class

package com.example.demo2;

import javax.annotation.PostConstruct;

//import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class Demo2Application
{
    //@Autowired
    //private HelloWorldService helloWorldService;
    
    public static void main(String[] args)
    {
        SpringApplication.run(Demo2Application.class, args);
    }
    
    @PostConstruct
    public void doSomethingIProbablyShouldNotBeDoing()
    {
        //helloWorldService.sayHello();
        HelloWorldClient client = new HelloWorldClient();
        client.sayHello();
    }
    
}

class 带有@Configurable 和@Autowired 服务

package com.example.demo2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

@Configurable
public class HelloWorldClient
{
    @Autowired
    private HelloWorldService service;
    
    public void sayHello()
    {
        // Used injected instance of service
        service.sayHello();
    }
}

@服务 class

package com.example.demo2;

import org.springframework.stereotype.Service;

@Service
public class HelloWorldService
{
    public void sayHello()
    {
        System.out.println("Hello world!");
    }
}

这里还有一个链接,指向我之前关于该主题的帖子。 我确实收到了我的问题的有效答案。 但无论出于何种原因,它在我这边不再起作用。
Spring @configurable NullPointerException

@Configurable应该与本机 AspectJ 结合使用,而不是与 Spring AOP 结合使用,请参见此处 因为该注解旨在用于 POJO 而不是 Spring bean,所以这是有道理的。

我对您的第一个问题的回答中,我们使用了 AspectJ Maven 插件来进行编译时织入 (CTW)。 您需要在此处执行相同的操作,或者改为配置加载时织入 (LTW)

使用 LTW 时,为了让org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect工作,它负责实现 POJO 依赖注入,您需要

  • -javaagent:/path/to/aspectjweaver.jar在 JVM 命令行或
  • -javaagent:/path/to/spring-instrument.jar在 JVM 命令行和@EnableLoadTimeWeaving在你的 Spring 配置或
  • de.invesdwin:invesdwin-instrument在依赖列表中加上一个代码片段来初始化你的应用程序中的编织器。 在更新的 Java 版本中,您可能还需要--add-opens java.base/java.lang=ALL-UNNAMED在 JVM 命令行上启用invesdwin-instrument以使用反射正常工作。

您还需要spring-aspects才能找到AnnotationBeanConfigurerAspect 或者,您可能希望在资源目录中添加自己的META-INF/aop.xml (或org/aspectj/aop.xml )文件,如果您想要配置某些编织选项或停用spring-aspects中不需要的方面,如果控制台上相应的警告消息会让您心烦意乱。

我知道这不是微不足道的,这就是为什么我认为 CTW 方法更容易设置。 但是如果你想通过 LTW 动态应用方面,你需要使用上面提到的方法之一。 这不是火箭科学,第一次做的时候有点复杂。 但就像宫本武藏说的那样: “乍看之下似乎很难,但万事开头难。”


更新:这是给你的MCVE ,即一个完整的、最小的例子:

https://github.com/kriegaex/SO_AJ_SpringMavenConfigurableNPE_74184130

如果您想使用-javaagent和 Spring 机载方式,请随意使用它并使用invesdwin-instrument和相应的依赖项删除代码。 我实际上建议这样做。

您正在使用new HelloWorldClient()手动创建服务@Autowired仅在 bean/service 由 spring 创建时有效

尝试在Demo2Application中自动装配HelloWorldClient而不是手动创建它

也许您可以在 class 新实例上尝试 Spring @Autowired也许它可以工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM