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

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

【Spring 核心】裝配bean(三)XML配置

2019-11-08 01:58:08
字體:
供稿:網(wǎng)友

項(xiàng)目包結(jié)構(gòu):

src/main/java

com.bonc.pojo--|-CompactDisc.java (接口)

                            |-SgtPeppers.java     (實(shí)現(xiàn)類 實(shí)現(xiàn) CompactDisc)

                            |-BlankDisc.java        (實(shí)現(xiàn)類 實(shí)現(xiàn) CompactDisc)

                            |-MediaPlayer.java     (接口)

                            |-CDPlayer.java          (實(shí)現(xiàn)類 實(shí)現(xiàn) MediaPlayer)

src/main/resources

sPRing.xml (Spring應(yīng)用上下文配置信息)

接口 CompactDisc.java

package com.bonc.pojo;public interface CompactDisc {	void  play();}實(shí)現(xiàn)類 SgtPeppers.java

package com.bonc.pojo;public 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);	}	public SgtPeppers() {		super();	}	//自定義帶參構(gòu)造器	public SgtPeppers(String title, String artist) {		super();		this.title = title;		this.artist = artist;	}}實(shí)現(xiàn)類 BlankDisc.java

package com.bonc.pojo;import java.util.List;public class BlankDisc implements CompactDisc {	private String title;	private String artist;	private List<String> tracks;	public void play() {		System.out.println("Playing "+title+" by "+artist);		for(String track:tracks){			System.out.println("-Track: "+track);		}	}	//自定義帶參構(gòu)造器	public BlankDisc(String title, String artist, List<String> tracks) {		super();		this.title = title;		this.artist = artist;		this.tracks = tracks;	}	public String getTitle() {		return title;	}	public void setTitle(String title) {		this.title = title;	}	public String getArtist() {		return artist;	}	public void setArtist(String artist) {		this.artist = artist;	}	public List<String> getTracks() {		return tracks;	}	public void setTracks(List<String> tracks) {		this.tracks = tracks;	}	}接口 MediaPlayer.java

package com.bonc.pojo;public interface MediaPlayer {	void play();}實(shí)現(xiàn)類CDPlayer.java

package com.bonc.pojo;public class CDPlayer implements MediaPlayer{	private CompactDisc cd;		public CDPlayer(){		super();	}		public CDPlayer(CompactDisc cd){		this.cd = cd;	}	public void play() {		cd.play();	}	public void setCd(CompactDisc cd) {		this.cd = cd;	}	}Spring.xml配置信息

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"	xmlns:c="http://www.springframework.org/schema/c"	xmlns:p="http://www.springframework.org/schema/p"	xmlns:util="http://www.springframework.org/schema/util"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:aop="http://www.springframework.org/schema/aop"    xmlns:tx="http://www.springframework.org/schema/tx"     xmlns:context="http://www.springframework.org/schema/context"    xsi:schemaLocation="     http://www.springframework.org/schema/beans      http://www.springframework.org/schema/beans/spring-beans-4.0.xsd     http://www.springframework.org/schema/tx      http://www.springframework.org/schema/tx/spring-tx-4.0.xsd     http://www.springframework.org/schema/aop      http://www.springframework.org/schema/aop/spring-aop-4.0.xsd     http://www.springframework.org/schema/context     http://www.springframework.org/schema/context/spring-context-4.0.xsd     http://www.springframework.org/schema/util     http://www.springframework.org/schema/util/spring-util.xsd">	<!--	 一、XML配置說明:	在Spring剛剛出現(xiàn)的時(shí)候,XML是描述配置的主要方式。	盡管Spring長(zhǎng)期以來確實(shí)與XML有著關(guān)聯(lián),但需要說明的是,XML不再是配置Spring的唯一方案。	鑒于已經(jīng)存在那么多基于XML的Spring配置,所以理解如何在Spring中配置XML還是很重要的。	本篇文章在于幫助你維護(hù)已有的XML配置,在完成新的Spring工作時(shí),希望你使用自動(dòng)化配置和JavaConfig     			如果不給出ID屬性,這個(gè)bean會(huì)根據(jù)全限定類名來進(jìn)行命名	本例中為 com.bonc.pojo.SgtPeppers#0 #0是一個(gè)計(jì)數(shù)的形式,用來區(qū)分其他相同類型的bean -->	<bean id="compactDisc" class="com.bonc.pojo.SgtPeppers"/>	<!--	二、借助構(gòu)造器初始化bean有兩種方案:	1. <constructor-arg>元素	2. 使用Spring3.0所引入的c-命名空間-->	 <bean id="cdPlayer" class="com.bonc.pojo.CDPlayer">	 	<constructor-arg  ref="compactDisc"/>	 </bean><!-- 	c:cd-ref 	c(命名空間的前綴)-構(gòu)造器的參數(shù)名-ref(告訴Spring 正在裝配的是一個(gè)bean的引用)		也可以使用參數(shù)在參數(shù)列表中的位置信息	c:_0-ref="compactDisc"-->	 <bean id="cdPlayer2" class="com.bonc.pojo.CDPlayer" c:cd-ref="compactDisc"/>	 <!--	  裝配字面量 -->	 <bean id="compactDisc" class="com.bonc.pojo.SgtPeppers">	 	<constructor-arg value="Sgt. Pepper's Lonely Hearts Club Band"/>	 	<constructor-arg value="The Beatles"/>	 </bean>	 <bean id="compactDisc" class="com.bonc.pojo.SgtPeppers"	 	c:_title="gt. Pepper's Lonely Hearts Club Band"	 	c:_artist="The Beatles"/>	 <bean id="compactDisc" class="com.bonc.pojo.SgtPeppers"	 	c:_0="gt. Pepper's Lonely Hearts Club Band"	 	c:_1="The Beatles"/><!-- 	裝配list  -->	 <bean id="compactDisc" class="com.bonc.pojo.BlankDisc">	 	<constructor-arg  value="Sgt. Pepper's Lonely Hearts Club Band"/>	 	<constructor-arg  value="The Beatles"/>	 	<constructor-arg>	 		<list>	 			<value>Sgt.Pepper's warm heart</value>	 			<value>With a little help from My Friends</value>	 			<value>in the Sky with Diamonds</value>	 			<value>Getting Better</value>	 			<value>Fixing A Hole</value>	 		</list>	 	</constructor-arg>	 </bean><!-- 裝配set -->	<bean id="compactDisc" class="com.bonc.pojo.BlankDisc">	 	<constructor-arg  value="Sgt. Pepper's Lonely Hearts Club Band"/>	 	<constructor-arg  value="The Beatles"/>	 	<constructor-arg>	 		<set>	 			<value>Sgt.Pepper's warm heart</value>	 			<value>With a little help from My Friends</value>	 			<value>in the Sky with Diamonds</value>	 			<value>Getting Better</value>	 			<value>Fixing A Hole</value>	 		</set>	 	</constructor-arg>	 </bean>	 <!-- 	三、屬性初始化bean -->	<bean id="compactDisc" class="com.bonc.pojo.BlankDisc">		<property name="title" value="Sgt. Pepper's Lonely Hearts Club Band"/>		<property name="artist"value="The Beatles"/>		<property name="tracks">			<list>				<value>Sgt.Pepper's warm heart</value>	 			<value>With a little help from My Friends</value>	 			<value>in the Sky with Diamonds</value>	 			<value>Getting Better</value>	 			<value>Fixing A Hole</value>			</list>		</property>	</bean><!-- 	四、使用Spring util-命名空間簡(jiǎn)化bean配置		首先需要在XML中聲明util-命名空間及其模式		util:list只是util-命名空間中的多個(gè)元素之一 --> 	<util:list id="trackList">		<value>Sgt.Pepper's warm heart</value>		<value>With a little help from My Friends</value>		<value>in the Sky with Diamonds</value>		<value>Getting Better</value>		<value>Fixing A Hole</value> 		 	</util:list> 	<bean id="compactDisc" class="com.bonc.pojo.BlankDisc" 		p:title="Sgt. Pepper's Lonely Hearts Club Band" 		p:artist="The Beatles" 		p:track-ref="trackList"/> 	</beans>


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 伊宁县| 平乡县| 邵武市| 中山市| 大竹县| 永城市| 固原市| 临桂县| 胶州市| 婺源县| 桃园市| 景宁| 城步| 沁阳市| 杭州市| 宁晋县| 华亭县| 东兰县| 浙江省| 疏勒县| 井冈山市| 安泽县| 崇州市| 页游| 老河口市| 岳普湖县| 汾西县| 彰化县| 鹤岗市| 北安市| 兴海县| 康保县| 乌鲁木齐市| 塘沽区| 上思县| 连云港市| 大荔县| 大港区| 于田县| 临武县| 平顶山市|