久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Spring如何基于注解顯式實(shí)現(xiàn)自動裝配

瀏覽:8日期:2023-08-23 18:34:17

構(gòu)建bean文件:

public class People { private String name = '小明';}

編寫配置類:

@Configuration@Import(ApplicationConfig2.class)public class ApplicationConfig { @Bean public People getPeople(){ return new People(); }}

@configuration:說明這是一個配置類,功能幾乎等同于<beans>標(biāo)簽

@Bean:說明這是一個bean,方法的返回值也就是<bean>中的class屬性,方法的名稱就是<bean>中的id

@Import:用于導(dǎo)入其它的配置類,相當(dāng)于<beans>下的<import>標(biāo)簽

編寫測試類:

public class MyTest { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext('com.guan.config'); People people = context.getBean('getPeople',People.class); System.out.println(people.getName()); }}

注意:這里使用AnnotationConfigApplicationContext類獲得上下文

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 茂名市| 马龙县| 荔浦县| 屏东市| 同仁县| 乌海市| 闸北区| 两当县| 新闻| 平顺县| 大同市| 兴化市| 阜阳市| 积石山| 万源市| 桐城市| 邢台市| 三原县| 汝阳县| 西乌珠穆沁旗| 建宁县| 文安县| 新安县| 自治县| 遂平县| 吉隆县| 浦江县| 沅陵县| 山阳县| 元江| 塘沽区| 邳州市| 锦州市| 泗阳县| 湖州市| 临夏市| 封丘县| 合肥市| 英德市| 田东县| 长沙县|