为了账号安全,请及时绑定邮箱和手机立即绑定

如何使自动装配在配置类中工作?

如何使自动装配在配置类中工作?

暮色呼如 2023-06-14 11:15:01
我正在尝试@Service在 @Configuration 类中自动装配一个标记为 a 的属性 (myService),但我得到了一个 NullPointer。相反,如果我myService在非配置类中自动装配,我没有问题。这是我在自动装配时遇到问题的@Service:package com.myapp.resources;@Serviceclass MyService {    public List<String> getRoutingKeys() {        List<String> routingKeys;        //Do stuff        return routingKeys;    }    public String aMethod() {        return "hello";    }}这是我无法自动装配服务的 @Configuration 类package com.myapp.messaging;import com.myapp.resources;import org.springframework.amqp.core.Binding;import org.springframework.amqp.core.BindingBuilder;import org.springframework.amqp.core.Queue;import org.springframework.amqp.core.TopicExchange;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.util.List;@Configurationpublic class RabbitConfiguration {    private List<String> routingKeys = writeRoutingKeys();    @Autowired    private MyService myService;    private List<String> writeRoutingKeys() {        boolean test = myService == null;        System.out.println("is the service null? " + test); //output: true!!!        return myService.getRoutingKeys(); //here I get a NullPointer    }    //Methods with bean declarations for RabbitMQ }如果有帮助,这是我的主类:package com.myapp;import com.myapp.resources;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import java.util.List;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        ConfigurableApplicationContext appContext = SpringApplication.run(Application.class, args);        MyService myService = (MyService) appContext.getBean(MyService.class);        boolean test = myService == null;        System.out.println("is the service null? " + test); //output: false        //Do stuff    }}如果有帮助,这里有一个不同的类(@RestController),我可以在其中自动装配服务
查看完整描述

2 回答

?
慕的地6264312

TA贡献1817条经验 获得超6个赞

Spring 只会在实例化 bean 之后或实例化时注入依赖项(取决于是否使用构造函数注入)。MyService但是,您现在在初始化 bean 之前发生的字段初始化期间访问依赖项。因此,它无法MyService在字段初始化期间访问,因为它尚未注入。


您可以通过更改为routingKeys同时在构造函数中使用构造函数注入和初始化来简单地修复它:


@Configuration

public class RabbitConfiguration {


    private List<String> routingKeys ;

    private MyService myService;


    @Autowired

    public RabbitConfiguration(MyService myService){

        this.myService = myService

        this.routingKeys = writeRoutingKeys();

    }


    private List<String> writeRoutingKeys() {

        return myService.getRoutingKeys(); 

    }

 }

或者简单地说:


@Autowired

public RabbitConfiguration(MyService myService){

    this.myService = myService

    this.routingKeys = myService.getRoutingKeys();

}


查看完整回答
反对 回复 2023-06-14
?
梵蒂冈之花

TA贡献1900条经验 获得超5个赞

@Bean我建议通过任何需要它的创建方法来注入服务:

@Bean
public MyBean create(MyService myService)

然后将服务传递给writeRoutingKeys(MyService myService)方法进行相应的处理。

根据文档:

@Configuration 类在上下文初始化期间很早就被处理,强制以这种方式注入依赖项可能会导致意外的提前初始化。只要有可能,就如上例那样使用基于参数的注入。


查看完整回答
反对 回复 2023-06-14
  • 2 回答
  • 0 关注
  • 89 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信