国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

【Spring 核心】裝配Bean(一) 自動化裝配

2019-11-08 02:20:40
字體:
來源:轉載
供稿:網友

SPRing從兩個角度實現自動化裝配:組件掃描 (Spring自動發現應用上下文中所創建的bean)自動裝配(autowiring)自動滿足bean之間的依賴

組件掃描:

package test.soundsystem;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import test.voice.Book;/* * 使用basePackageClasses,spring會自動掃描這些類所在的包, * 建議在包中寫空標記接口,用來被掃描,有利于項目重構。 *  * */@Configuration@ComponentScan(basePackageClasses={Book.class,CompactDisc.class})public class CDPlayerConfig {}接口:

package test.soundsystem;public interface CompactDisc {	void play();}實現類:

package test.soundsystem;import javax.inject.Named;//Named 和 Component相似@Namedpublic class SgtPeppers implements CompactDisc {	private String title="Sgt. Pepper's Lonely Hearts Club Band";	private String artist = "The Beatles";	public void play() {		System.out.println("Playing "+title+" by "+artist);	}}測試類:

package test.soundsystem;import javax.inject.Inject;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import test.voice.Book;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes=CDPlayerConfig.class)public class CDPlayerTest {	@Autowired	private CompactDisc cd;		@Inject	private Book book;		@Test	public void cdShouldNotBeNull(){		cd.play();		book.read();	}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蓬莱市| 阿拉尔市| 邳州市| 措美县| 涟源市| 鹤庆县| 彝良县| 甘泉县| 穆棱市| 呈贡县| 凭祥市| 施秉县| 雷波县| 锡林郭勒盟| 兴业县| 兴宁市| 松滋市| 修水县| 乌审旗| 石屏县| 灵石县| 梓潼县| 卓资县| 临泉县| 江川县| 康马县| 永安市| 湛江市| 九江县| 株洲市| 濮阳县| 陕西省| 霍林郭勒市| 米林县| 武宣县| 和林格尔县| 甘德县| 桐庐县| 胶南市| 沙洋县| 林州市|