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

首頁 > 學院 > 開發(fā)設計 > 正文

《Spring實戰(zhàn)》學習筆記(一)裝配Bean

2019-11-14 11:17:31
字體:
來源:轉載
供稿:網(wǎng)友

自動轉配Bean

創(chuàng)建可被發(fā)現(xiàn)的Bean

代碼結構:

這里寫圖片描述

程序清單:

package soundsystem;public interface CompactDisc { void play();}

PS:@Component表明組件

package soundsystem;import org.sPRingframework.stereotype.Component;@Componentpublic class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}

PS:@ComponentScan注解啟用了組件掃描,如果沒有其他配置的話,@ComponentScan默認會掃描與配置類相同的包以及它的子包。這里是基于java的配置啟用組件掃描

package soundsystem;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScanpublic class CDPlayerConfig {}

測試

PS:@Autowired注解表明自動裝配

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 soundsystem.CDPlayerConfig;import soundsystem.CompactDisc;import static org.junit.Assert.assertNotNull;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes=CDPlayerConfig.class)public class CDPlayerTest { @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { assertNotNull(cd); }}

這里寫圖片描述

通過xml啟用組件掃描

代碼結構

這里寫圖片描述

程序清單

PS:applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 啟用組件掃描 --> <context:component-scan base-package="soundsystem"/></beans>

PS:一樣

package soundsystem;public interface CompactDisc { void play();}

PS:一樣

package soundsystem;import org.springframework.stereotype.Component;@Componentpublic class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}

測試

PS:變?yōu)?@ContextConfiguration(locations = { “classpath*:/applicationContext.xml”})

import static org.junit.Assert.assertNotNull;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath*:/applicationContext.xml"})public class CDPlayerTest { @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { assertNotNull(cd); }}

這里寫圖片描述

為組件掃描的bean命名

(1)默認bean的ID為sgtPeppers

@Componentpublic class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}

(2)bean的ID為Leo

@Component("Leo")public class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}

(3)bean的ID為Leo

@Named("Leo")public class SgtPeppers implements CompactDisc{ private String title = "Sgt.Pepper's Lonely Hearts Club Bands"; private String artist = "The Beatles"; public void play() { System.out.println("Playing "+title+" by "+artist); }}

設置組件掃描的基礎包

(1)掃描soundsystem

@Configuration@ComponentScan("soundsystem")public class CDPlayerConfig {}

(2)掃描soundsystem,video

@Configuration@ComponentScan(basePackages={"soundsystem","video"})public class CDPlayerConfig {}

(3)掃描CDPlayer類,DVDPlayer類所在的包

@Configuration@ComponentScan(basePackageClasses = {CDPlayer.class,DVDPlayer.class})public class CDPlayerConfig {}

通過為bean添加注解實現(xiàn)自動裝配

代碼結構

這里寫圖片描述

程序清單

PS:任何方法都可以實現(xiàn)自動裝配

package soundsystem;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;@Componentpublic class CDPlayer implements MediaPlayer { private CompactDisc cd; @Autowired public CDPlayer(CompactDisc cd) { this.cd = cd; } @Autowired public void setCompactDisc(CompactDisc cd) { this.cd = cd; } @Autowired public void insertDisc(CompactDisc cd) { this.cd = cd; } public void play() { cd.play(); }}

PS:Java配置組件掃描

package soundsystem;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration@ComponentScanpublic class CDPlayerConfig {}package soundsystem;public interface CompactDisc { void play();}package soundsystem;public interface MediaPlayer { public void play();}package soundsystem;import org.springframework.stereotype.Component;/** * Created by adc333 on 2017/2/3. */@Componentpublic class SgtPeppers implements CompactDisc{ private String title = "hello"; private String artist = " world"; public void play() { System.out.print(title+artist); }}

測試

PS:控制臺輸出比較,這里簡單輸出hello world

import org.junit.Rule;import org.junit.Test;import org.junit.contrib.java.lang.system.StandardOutputStreamLog;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 soundsystem.CDPlayerConfig;import soundsystem.CompactDisc;import soundsystem.MediaPlayer;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertNotNull;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = CDPlayerConfig.class)public class CDPlayerTest { @Rule public final StandardOutputStreamLog log = new StandardOutputStreamLog(); @Autowired private MediaPlayer player; @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { assertNotNull(cd); } @Test public void play() { player.play(); assertEquals("hello world",log.getLog()); }}

這里寫圖片描述


通過Java代碼裝配bean

創(chuàng)建配置類

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

聲明簡單的bean

(1)默認bean的ID為sgtPeppers

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

(2)bean的ID為Leo

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

借助JavaConfig實現(xiàn)注入

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

測試

代碼結構

程序清單

package soundsystem;import org.springframework.beans.factory.annotation.Autowired;public class CDPlayer implements MediaPlayer { private CompactDisc cd; @Autowired public CDPlayer(CompactDisc cd) { this.cd = cd; } @Autowired public void setCompactDisc(CompactDisc cd) { this.cd = cd; } @Autowired public void insertDisc(CompactDisc cd) { this.cd = cd; } public void play() { cd.play(); }}package soundsystem;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class CDPlayerConfig { @Bean public CompactDisc sgtPeppers() { return new SgtPeppers(); } @Bean public CDPlayer cdPlayer(CompactDisc compactDisc) { return new CDPlayer(compactDisc); }}package soundsystem;public interface CompactDisc { void play();}package soundsystem;public interface MediaPlayer { public void play();}package soundsystem;public class SgtPeppers implements CompactDisc{ private String title = "hello"; private String artist = " world"; public void play() { System.out.print(title+artist); }}

測試

import org.junit.Rule;import org.junit.Test;import org.junit.contrib.java.lang.system.StandardOutputStreamLog;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 soundsystem.CDPlayerConfig;import soundsystem.CompactDisc;import soundsystem.MediaPlayer;import static org.junit.Assert.assertEquals;import static org.junit.Assert.assertNotNull;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(classes = CDPlayerConfig.class)public class CDPlayerTest { @Rule public final StandardOutputStreamLog log = new StandardOutputStreamLog(); @Autowired private MediaPlayer player; @Autowired private CompactDisc cd; @Test public void cdShouldNotBeNull() { assertNotNull(cd); } @Test public void play() { player.play(); assertEquals("hello world",log.getLog()); }}

這里寫圖片描述


通過XML裝配bean

聲明一個簡單的bean

(1)bean的ID為soundsystem.SgtPeppers#0,如果聲明了另外一個SgtPeppers,同樣沒有明確標識,bean的ID為soundsystem.SgtPeppers#1

<bean class="soundsystem.SgtPeppers"/>

(2)bean的ID為compactDisc

<bean id="compactDisc" class="soundsystem.SgtPeppers"/>

借助構造器注入初始化bean

構造器注入bean引用

(1)

<bean id="cdPlayer" class="soundsystem.CDPlayer"> <constructor-arg ref="compactDisc"/></bean>

(2)

<bean id="cdPlayer" class="soundsystem.CDPlayer" c:cd-ref="compactDisc" />

將字面量注入到構造器中

package soundsystem;public class BlankDisc implements CompactDisc { private String title; private String artist; public BlankDisc(String title, String artist) { this.title = title; this.artist = artist; } public void play() { System.out.println("Playing "+title+" by "+artist); }}

(1)

<bean id="compactDisc" class="soundsystem.BlankDisc"> <constructor-arg value="hello"/> <constructor-arg value="world"/></bean>

(2)

<bean id="compactDisc" class="soundsystem.BlankDisc" c:_title="hello" c:_artist="world" />

裝配集合

package soundsystem;import java.util.List;/** * Created by adc333 on 2017/2/3. */public class BlankDisc implements CompactDisc { private String title; private String artist; private List<String> tracks; public BlankDisc(String title, String artist, List<String> tracks) { this.title = title; this.artist = artist; this.tracks = tracks; } public void play() { System.out.println("Playing "+title+" by "+artist); for(String track : tracks) { System.out.println("-Track: "+track); } }}<bean id="compactDisc" class="soundsystem.BlankDisc"> <constructor-arg value="hello"/> <constructor-arg value="world"/> <constructor-arg> <list> <value>11111</value> <value>22222</value> <value>33333</value> <value>44444</value> <value>55555</value> <list> </constructor-arg></bean>

設置屬性

package soundsystem;import org.springframework.beans.factory.annotation.Autowired;public class CDPlayer implements MediaPlayer { private CompactDisc cd; @Autowired public void setCompactDisc(CompactDisc cd) { this.cd = cd; } public void play() { cd.play(); }}

(1)

<bean id="cdPlayer" class="soundsystem.CDPlayer"> <property name="compactDisc" ref="compactDisc"/></bean>

(2)

<bean id="cdPlayer" class="soundsystem.CDPlayer" p:compactDisc-ref="compactDisc" />

將字面量注入到屬性中

package soundsystem;import java.util.List;public class BlankDisc implements CompactDisc { private String title; private String artist; private List<String> tracks; public void setTitle(String title) { this.title = title; } public void setArtist(String artist) { this.artist = artist; } public void setTracks(List<String> tracks) { this.tracks = tracks; } public void play() { System.out.println("Playing "+title+" by "+artist); for(String track : tracks) { System.out.println("-Track: "+track); } }}

(1)

<bean id="compactDisc" class="soundsystem.BlankDisc"> <property name="title" value="hello"/> <property name="artist" value="world"/> <property name="tracks"> <list> <value>11111</value> <value>22222</value> <value>33333</value> <value>44444</value> <value>55555</value> <list> </property></bean>

(2)

<bean id="compactDisc" class="soundsystem.BlankDisc" p:title="hello" p:artist="world"> <property name="tracks"> <list> <value>11111</value> <value>22222</value> <value>33333</value> <value>44444</value> <value>55555</value> <list> </property></bean>

測試

代碼結構

這里寫圖片描述

程序清單

package soundsystem;import java.util.List;public class BlankDisc implements CompactDisc { private String title; private String artist; private List<String> tracks; public void setTitle(String title) { this.title = title; } public void setArtist(String artist) { this.artist = artist; } public void setTracks(List<String> tracks) { this.tracks = tracks; } public void play() { System.out.print(title+artist); for(String track : tracks) { System.out.print(track); } }}package soundsystem;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;public class CDPlayer implements MediaPlayer { private CompactDisc cd; public CDPlayer(CompactDisc cd) { this.cd = cd; } public void play() { cd.play(); }}package soundsystem;public interface CompactDisc { void play();}package soundsystem;public interface MediaPlayer { public void play();}

applicationContext.xml:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 設置屬性注入bean --> <bean id="compactDisc" class="soundsystem.BlankDisc"> <property name="title" value="hello"/> <property name="artist" value="world"/> <property name="tracks"> <list> <value>11111</value> <value>22222</value> <value>33333</value> <value>44444</value> <value>55555</value> </list> </property> </bean> <!-- 構造器注入bean --> <bean id="cdPlayer" class="soundsystem.CDPlayer"> <constructor-arg ref="compactDisc"/> </bean></beans>

測試

import org.junit.Rule;import org.junit.Test;import org.junit.contrib.java.lang.system.StandardOutputStreamLog;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationContext;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import soundsystem.*;import static org.junit.Assert.assertEquals;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations = { "classpath*:/applicationContext.xml"})public class CDPlayerTest { @Autowired private ApplicationContext context; @Rule public final StandardOutputStreamLog log = new StandardOutputStreamLog(); @Test public void play() { CompactDisc compactDisc = (CompactDisc)context.getBean(BlankDisc.class); MediaPlayer cdPlayer = (MediaPlayer)context.getBean(CDPlayer.class); cdPlayer.play(); assertEquals("helloworld1111122222333334444455555",log.getLog()); }}

這里寫圖片描述


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 贺兰县| 宽城| 通化县| 安新县| 岑溪市| 青海省| 通许县| 荥阳市| 溧水县| 雷州市| 铁岭县| 罗田县| 韶山市| 若羌县| 肥乡县| 三都| 永登县| 留坝县| 揭西县| 尼勒克县| 渑池县| 武宣县| 濉溪县| 塔城市| 怀仁县| 高要市| 深圳市| 静宁县| 肥乡县| 皮山县| 喀什市| 泸州市| 西畴县| 岑巩县| 宜君县| 大埔县| 营山县| 长岛县| 错那县| 包头市| 建宁县|