首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring @configurable NullPointerException,@autowired服务为null

Spring @configurable NullPointerException,@autowired服务为null
EN

Stack Overflow用户
提问于 2022-10-24 16:27:52
回答 2查看 118关注 0票数 1

我正在尝试在我创建的一个非bean类中使用@configurable在spring中使用一个@autowired服务。

不管我怎么试都不想再起作用了。

有人能告诉我我做错了什么吗?(我做了一些研究,但我现在完全一无所知)

下面是我做的一个非常基本的代码示例:

pom.xml

代码语言:javascript
复制
<?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类

代码语言:javascript
复制
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类

代码语言:javascript
复制
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();
    }
    
}

使用“可配置”和“自动配置”服务初始化

代码语言:javascript
复制
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();
    }
}

@服务类

代码语言:javascript
复制
package com.example.demo2;

import org.springframework.stereotype.Service;

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

这里还有一个链接到我之前关于这个主题的文章中。我确实收到了对我的问题的一个有效的答复。但不管出于什么原因,这对我来说已经不起作用了。

Spring @可配置NullPointerException

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-07 16:53:58

@Configurable应该用于与本机AspectJ的关联,而不是Spring,参见这里。因为注释是用于POJO而不是Spring,所以这是有意义的。

我对你第一个问题的回答中,我们使用AspectJ Maven插件进行编译时编织(CTW)。要么您需要在这里执行同样的操作,要么配置负载时间编织(LTW)

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

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

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

我知道这并不是小事一桩,所以我认为CTW的方法更容易建立。但是,如果要通过LTW动态应用方面,则需要使用上述方法之一。这不是火箭科学,只是第一次做的时候有点复杂。但就像宫本武藏说的:“一开始看起来很困难,但一开始一切都很困难。”

更新:这里为您提供了一个MCVE,即一个完整的、最小的示例:

74184130

如果您想使用invesdwin-instrument和Spring的方法,可以随意使用它并删除使用-javaagent和相应的依赖项的代码。我真的建议你这么做。

票数 1
EN

Stack Overflow用户

发布于 2022-10-25 13:06:32

仅当new HelloWorldClient() / service在春季创建时,您才使用@Autowired手动创建服务。

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

也许你可以试试Spring @Autowired在一个类新实例上,也许它能用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74184130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档