【问题标题】:Spring @configurable NullPointerException, @autowired service is nullSpring @configurable NullPointerException,@autowired 服务为空
【发布时间】:2022-11-08 00:53:58
【问题描述】:

我试图在春天使用@configurable 在我创建的非bean 类中使用@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 类

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 类

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

具有@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();
    }
}

@服务类

package com.example.demo2;

import org.springframework.stereotype.Service;

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

另外这里是我之前关于该主题的帖子的链接。我确实收到了对我的问题有效的答案。但无论出于何种原因,它对我来说不再起作用了。
Spring @configurable NullPointerException

【问题讨论】:

    标签: java spring spring-boot maven spring-aop


    【解决方案1】:

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

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

    也许你可以试试Spring @Autowired on a class new instance 也许它有效

    【讨论】:

    • 您好,谢谢您的回答。这实际上是@configurable 的目的。我不明白为什么方面的事情不起作用。好像被停用了一样。
    • 阅读它可能是“aspectjweaver”以前的答案有一些问题,但当前的解决方案有“spring-boot-starter-aop”Afyer 阅读此stackoverflow.com/questions/4703206/… 看起来可能有很多原因它可能不起作用:/
    • 是的,我尝试了很多不同的东西,包括上一个答案的代码,没有改变任何东西。并且还升级所有东西(spring、java、eclispe 等)。或在两台不同的计算机上进行测试。或者使用不同的库(这次我使用了 spring-boot-starter-aop 以确保在此示例中使用官方的)。
    【解决方案2】:

    @Configurable 应该与本机 AspectJ 一起使用,而不是与 Spring AOP 一起使用,请参阅 here。因为注解是用于 POJO 而不是 Spring bean,所以这是有道理的。

    在my answer to your first question 中,我们使用 AspectJ Maven 插件进行编译时编织 (CTW)。您需要在此处执行相同操作,或者改为配置 load-time weaving (LTW)。

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

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

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

    我知道这不是微不足道的,这就是为什么我认为 CTW 方法更容易设置的原因。但是,如果您想通过 LTW 动态应用方面,则需要使用上述方法之一。这不是火箭科学,只是第一次做的时候很复杂。但就像宫本武藏所说:“一开始可能看起来很困难,但所有事情一开始都是困难的。”

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      相关资源
      最近更新 更多