猿问

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

我正在尝试@Service在 @Configuration 类中自动装配一个标记为 a 的属性 (myService),但我得到了一个 NullPointer。

相反,如果我myService在非配置类中自动装配,我没有问题。


这是我在自动装配时遇到问题的@Service:


package com.myapp.resources;


@Service

class 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;


@Configuration

public 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;


@SpringBootApplication

public 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),我可以在其中自动装配服务

暮色呼如
浏览 114回答 2
2回答

慕的地6264312

Spring 只会在实例化 bean 之后或实例化时注入依赖项(取决于是否使用构造函数注入)。MyService但是,您现在在初始化 bean 之前发生的字段初始化期间访问依赖项。因此,它无法MyService在字段初始化期间访问,因为它尚未注入。您可以通过更改为routingKeys同时在构造函数中使用构造函数注入和初始化来简单地修复它:@Configurationpublic class RabbitConfiguration {&nbsp; &nbsp; private List<String> routingKeys ;&nbsp; &nbsp; private MyService myService;&nbsp; &nbsp; @Autowired&nbsp; &nbsp; public RabbitConfiguration(MyService myService){&nbsp; &nbsp; &nbsp; &nbsp; this.myService = myService&nbsp; &nbsp; &nbsp; &nbsp; this.routingKeys = writeRoutingKeys();&nbsp; &nbsp; }&nbsp; &nbsp; private List<String> writeRoutingKeys() {&nbsp; &nbsp; &nbsp; &nbsp; return myService.getRoutingKeys();&nbsp;&nbsp; &nbsp; }&nbsp;}或者简单地说:@Autowiredpublic RabbitConfiguration(MyService myService){&nbsp; &nbsp; this.myService = myService&nbsp; &nbsp; this.routingKeys = myService.getRoutingKeys();}

梵蒂冈之花

@Bean我建议通过任何需要它的创建方法来注入服务:@Bean public&nbsp;MyBean&nbsp;create(MyService&nbsp;myService)然后将服务传递给writeRoutingKeys(MyService myService)方法进行相应的处理。根据文档:@Configuration 类在上下文初始化期间很早就被处理,强制以这种方式注入依赖项可能会导致意外的提前初始化。只要有可能,就如上例那样使用基于参数的注入。
随时随地看视频慕课网APP

相关分类

Java
我要回答