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

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

具有AnnotationConfigApplicationContext的Spring應(yīng)用程序:

具有AnnotationConfigApplicationContext的Spring應(yīng)用程序:

森欄 2022-12-28 11:03:15
這是我的一個(gè) SO 問(wèn)題的延續(xù)。我擴(kuò)展了同一個(gè)例子,我希望它能工作,但是我得到了 NullPointerException。這是完整的源代碼:FirstBean.javapackage com.example;import org.springframework.stereotype.Component;@Componentpublic class FirstBean {    public FirstBean() {    }    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public String toString() {        return "FirstBean [name=" + name + "]";    }}SomeBean.javapackage com.example;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Component;@Component@Configuration@ComponentScan(basePackages = { "com.example" })public class SomeBean {    @Autowired    private FirstBean fb;    @Bean    FirstBean instantiateFirstBean() {        return new FirstBean();    }    public SomeBean() {        // this.fb.setName("Some Name"); -> this was causing problem as         // bean isn't still created fully    }    public void print() {        this.fb.toString();     }        @PostConstruct    void post() {        fb.setName("Some name");    }}
查看完整描述

1 回答

?
森林海

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

我認(rèn)為這是因?yàn)?Spring 將首先必須實(shí)例化您的SomeBean類,然后它將嘗試FirstBean通過(guò)反射自動(dòng)裝配,因?yàn)槟鶣Autowired在字段上放置了注釋。


您正在嘗試訪問(wèn)構(gòu)造函數(shù)中的FirstBean依賴項(xiàng),但是當(dāng)調(diào)用SomeBean構(gòu)造函數(shù)時(shí),在創(chuàng)建上下文時(shí),依賴項(xiàng)為空,因?yàn)?Spring 尚未自動(dòng)裝配依賴項(xiàng) - 它會(huì)在對(duì)象創(chuàng)建后嘗試通過(guò)反射自動(dòng)裝配它。這就是你到達(dá)那里的原因。你的班級(jí)也會(huì)發(fā)生同樣的情況——決定一種方法。SomeBeanFirstBeanNullPointerExceptionSomeBean


此外,奇怪的是,您創(chuàng)建的類都用@Configuration和進(jìn)行了注釋@Component。請(qǐng)參閱此 SO 問(wèn)題以查看潛在錯(cuò)誤。


此外,您的配置中存在一些問(wèn)題。您使用注釋FirstBean進(jìn)行@Component注釋,并且您仍然創(chuàng)建了一個(gè)@Bean返回此類實(shí)例的帶注釋的方法。您應(yīng)該決定一種方法 - 您使用注釋為應(yīng)用程序上下文實(shí)例化它,@Bean或者使用注釋此類,@Component然后讓組件掃描自動(dòng)為您創(chuàng)建它。


編輯:


您應(yīng)該決定是要使用@ComponentScan還是要通過(guò)@Bean注釋為上下文創(chuàng)建 bean - 現(xiàn)在您正在混合不同的類型,并且在嘗試按類型自動(dòng)裝配時(shí)會(huì)因重復(fù)的 bean 定義而出錯(cuò),因?yàn)樵谀?dāng)前的實(shí)現(xiàn)中會(huì)有在您的上下文中是 2 個(gè) beanSomeBean和 2 個(gè) bean FirstBean- 它們只會(huì)有不同的 ID。請(qǐng)注意,我用 NPE 期望指出的錯(cuò)誤與它無(wú)關(guān)。我將提出一個(gè)使用自動(dòng)包掃描的解決方案:


FirstBean類


@Component

public class FirstBean {


    private String name;


    public String getName() {

        return name;

    }


    public void setName(String name) {

        this.name = name;

    }


    @Override

    public String toString() {

        return "FirstBean [name=" + name + "]";

    }

}

SomeBean類


@Component

public class SomeBean {


    private FirstBean fb;


    @Autowired

    public SomeBean(FirstBean firstBean) {

        this.fb = firstBean;

        this.fb.setName("Some Name");

    }


    public void print() {

        this.fb.toString();

    }

}

我的配置類


@Configuration

@ComponentScan(basePackages = { "com.example" })

public class MyConfig {


}

主驅(qū)動(dòng)類


@Component

public class MainDriver {


    @Autowired

    private Environment env;


    @Autowired

    protected ConfigurableEnvironment cenv;


    @Autowired

    ApplicationContext ctx;


    @Autowired

    private SomeBean sb;



    public static void main(String[] args) {


        ApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class);


        MainDriver l = ctx.getBean(MainDriver.class);

        System.out.println("In main() the ctx is " + ctx);

        l.test();

    }


    public void test() {

        System.out.println("hello");

        sb.print();

    }

}


查看完整回答
反對(duì) 回復(fù) 2022-12-28
  • 1 回答
  • 0 關(guān)注
  • 115 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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