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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

Spring入門(mén)(三):通過(guò)JavaConfig裝配bean

標(biāo)簽:
Spring

上一篇博客中,我们讲解了使用组件扫描和自动装配实现自动化装配bean,这也是最好的使用方式。

但是某些场景下,我们可能无法使用自动装配的功能,此时就不得不显式的配置bean。

比如我们引用了一个第三方类库,需要将类库中的某个类装配到项目中,我们不可能在该类上添加@Component注解,因此无法使用自动装配的功能。

Spring中有以下两种方式显式配置bean:

  1. 通过JavaConfig配置bean

  2. 通过xml配置bean

本篇博客主要讲解下通过JavaConfig配置bean的实现方法,通过xml配置bean的实现方法后续再单独写一篇博客。

我们还使用上一篇博客中的例子,不过代码会做适当修改。

package soundsystem.javaconfig;public interface CompactDisc {    void play();
}
package soundsystem.javaconfig;public class SgtPeppers implements CompactDisc {    @Override
    public void play() {
        String title = "Sgt.Pepper's Lonely Hearts Club Band";
        String artists = "The Beatles";
        System.out.println("Playing " + title + " By " + artists);
    }
}
package soundsystem.javaconfig;public class CDPlayer {    private CompactDisc compactDisc;    public CDPlayer(CompactDisc compactDisc) {        this.compactDisc = compactDisc;
    }    public void play() {
        compactDisc.play();
    }
}

注意:和上一篇博客相比,我们去掉了SgtPeppers类和CDPlayer类上的@Component注解。**

1.创建配置类

package soundsystem.javaconfig;import org.springframework.context.annotation.Configuration;@Configurationpublic class CDPlayerConfig {
}

2.声明bean

在JavaConfig中,我们使用@Bean注解来声明bean,如下所示:

package soundsystem.javaconfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class CDPlayerConfig {    @Bean
    public CompactDisc sgtPeppers() {        return new SgtPeppers();
    }
}

默认生成的bean ID和方法名一致,即sgtPeppers,不过我们可以自定义:

@Bean(name = "lonelyHeartsClub")public CompactDisc sgtPeppers() {     return new SgtPeppers();
}

上面声明的bean比较简单,没有任何其它依赖,但是有些复杂的bean,比如CDPlayer,它依赖于CompactDisc,那我们该如何声明呢?

简单的一种方式是,直接使用刚刚定义的sgtPeppers()方法作为CDPlayer构造器的参数依赖:

@Beanpublic CDPlayer cdPlayer() {    return new CDPlayer(sgtPeppers());
}

不过更建议的是以下方式,将依赖项作为bean方法的参数,Spring会自动匹配到参数依赖项:

@Beanpublic CDPlayer cdPlayer(CompactDisc compactDisc) {    return new CDPlayer(compactDisc);
}

此时配置类的代码为:

package soundsystem.javaconfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class CDPlayerConfig {    @Bean
    //@Bean(name = "lonelyHeartsClub")
    public CompactDisc sgtPeppers() {        return new SgtPeppers();
    }    /*@Bean
    public CDPlayer cdPlayer() {
        return new CDPlayer(sgtPeppers());
    }*/

    @Bean
    public CDPlayer cdPlayer(CompactDisc compactDisc) {        return new CDPlayer(compactDisc);
    }
}

3.验证bean是否装配成功

新建测试类CDPlayerTest:

package soundsystem.javaconfig;import org.springframework.context.ApplicationContext;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class CDPlayerTest {    public static void main(String[] args) {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(CDPlayerConfig.class);
        CompactDisc compactDisc = (SgtPeppers) applicationContext.getBean("sgtPeppers");
        compactDisc.play();

        CDPlayer cdPlayer = applicationContext.getBean(CDPlayer.class);
        cdPlayer.play();
    }
}

运行结果:

5c7fcfed0001914114150182.jpg

从运行结果可以看出,bean装配成功。

4.源码地址

https://github.com/zwwhnly/SpringStudyDemo.git,欢迎大家下载,有问题可以多多交流。


               作者:周伟伟的技术博客                
               出处:https://www.cnblogs.com/zwwhnly/                
               本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。


點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶(hù)
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專(zhuān)欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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

舉報(bào)

0/150
提交
取消