国产探花免费观看_亚洲丰满少妇自慰呻吟_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();	}}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博湖县| 九台市| 靖安县| 荥经县| 博湖县| 耿马| 会同县| 武汉市| 浦城县| 普兰店市| 巴林右旗| 宣化县| 甘谷县| 青川县| 长乐市| 思茅市| 临汾市| 和平县| 麦盖提县| 金乡县| 福贡县| 买车| 伊金霍洛旗| 灵台县| 开原市| 盐津县| 深水埗区| 鞍山市| 南皮县| 集安市| 临沂市| 车险| 林芝县| 晋江市| 福海县| 定陶县| 昭觉县| 佛教| 海原县| 林西县| 阜城县|