第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何使自動(dòng)裝配在配置類中工作?

如何使自動(dòng)裝配在配置類中工作?

暮色呼如 2023-06-14 11:15:01
我正在嘗試@Service在 @Configuration 類中自動(dòng)裝配一個(gè)標(biāo)記為 a 的屬性 (myService),但我得到了一個(gè) NullPointer。相反,如果我myService在非配置類中自動(dòng)裝配,我沒有問題。這是我在自動(dòng)裝配時(shí)遇到問題的@Service:package com.myapp.resources;@Serviceclass MyService {    public List<String> getRoutingKeys() {        List<String> routingKeys;        //Do stuff        return routingKeys;    }    public String aMethod() {        return "hello";    }}這是我無法自動(dòng)裝配服務(wù)的 @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    }}如果有幫助,這里有一個(gè)不同的類(@RestController),我可以在其中自動(dòng)裝配服務(wù)
查看完整描述

2 回答

?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊

Spring 只會(huì)在實(shí)例化 bean 之后或?qū)嵗瘯r(shí)注入依賴項(xiàng)(取決于是否使用構(gòu)造函數(shù)注入)。MyService但是,您現(xiàn)在在初始化 bean 之前發(fā)生的字段初始化期間訪問依賴項(xiàng)。因此,它無法MyService在字段初始化期間訪問,因?yàn)樗形醋⑷搿?/p>


您可以通過更改為routingKeys同時(shí)在構(gòu)造函數(shù)中使用構(gòu)造函數(shù)注入和初始化來簡(jiǎn)單地修復(fù)它:


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

    }

 }

或者簡(jiǎn)單地說:


@Autowired

public RabbitConfiguration(MyService myService){

    this.myService = myService

    this.routingKeys = myService.getRoutingKeys();

}


查看完整回答
反對(duì) 回復(fù) 2023-06-14
?
梵蒂岡之花

TA貢獻(xiàn)1900條經(jīng)驗(yàn) 獲得超5個(gè)贊

@Bean我建議通過任何需要它的創(chuàng)建方法來注入服務(wù):

@Bean
public MyBean create(MyService myService)

然后將服務(wù)傳遞給writeRoutingKeys(MyService myService)方法進(jìn)行相應(yīng)的處理。

根據(jù)文檔:

@Configuration 類在上下文初始化期間很早就被處理,強(qiáng)制以這種方式注入依賴項(xiàng)可能會(huì)導(dǎo)致意外的提前初始化。只要有可能,就如上例那樣使用基于參數(shù)的注入。


查看完整回答
反對(duì) 回復(fù) 2023-06-14
  • 2 回答
  • 0 關(guān)注
  • 137 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)