Spring如何基于注解顯式實(shí)現(xiàn)自動(dòng)裝配
構(gòu)建bean文件:
public class People { private String name = '小明';}
編寫配置類:
@Configuration@Import(ApplicationConfig2.class)public class ApplicationConfig { @Bean public People getPeople(){ return new People(); }}
@configuration:說(shuō)明這是一個(gè)配置類,功能幾乎等同于<beans>標(biāo)簽
@Bean:說(shuō)明這是一個(gè)bean,方法的返回值也就是<bean>中的class屬性,方法的名稱就是<bean>中的id
@Import:用于導(dǎo)入其它的配置類,相當(dāng)于<beans>下的<import>標(biāo)簽
編寫測(cè)試類:
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類獲得上下文
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車2. ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)3. Docker 容器健康檢查機(jī)制4. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效5. Python xml、字典、json、類四種數(shù)據(jù)類型如何實(shí)現(xiàn)互相轉(zhuǎn)換6. python中asyncio異步編程學(xué)習(xí)7. python os.listdir()亂碼解決方案8. Java struts2 package元素配置及實(shí)例解析9. ASP實(shí)現(xiàn)文件上傳的方法10. python使用openpyxl庫(kù)讀寫Excel表格的方法(增刪改查操作)
