分類  >  Web前端 >

Spring Bean工場和上下文

tags:    時間:2013-12-09 23:36:49
Spring Bean工廠和上下文
package com.svse.entity;  import java.io.Serializable; import java.util.List;  public class AdminEntity implements Serializable { 	 	private String college; 	private String tel; 	private List ar; 	public String getCollege() { 		return college; 	} 	public void setCollege(String college) { 		this.college = college; 	} 	public String getTel() { 		return tel; 	} 	public void setTel(String tel) { 		this.tel = tel; 	} 	public List getAr() { 		return ar; 	} 	public void setAr(List ar) { 		this.ar = ar; 	} }


package com.svse.entity;  import java.io.Serializable;  public class UserEntity implements Serializable { 	private int id; 	private String name; 	private AdminEntity admin; 	 	public UserEntity(){} 	public UserEntity(int id ,String name,AdminEntity admin){ 		this.id = id; 		this.name = name; 		this.admin = admin; 	}  	public int getId() { 		return id; 	}  	public void setId(int id) { 		this.id = id; 	}  	public String getName() { 		return name; 	}  	public void setName(String name) { 		this.name = name; 	}  	public AdminEntity getAdmin() { 		return admin; 	}  	public void setAdmin(AdminEntity admin) { 		this.admin = admin; 	}  }


<?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:p="http://www.springframework.org/schema/p" 	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  	<!-- 1、解釋:AdminEntity adminEntity = new AdminEntity(); --> 	<bean id="adminEntity" class="com.svse.entity.AdminEntity">  		<!-- AdminEntity 類中有個屬性為:college 的,賦值為:value="" --> 		<property name="college" value="湖北國土資源職業學院"></property> 		<property name="tel" value="1533*******"></property> 		<!-- 為集合賦值List --> 		<property name="ar"> 			<list> 				<value>上網</value> 				<value>吃飯</value> 				<value>睡覺</value> 			</list> 		</property> 	</bean>  	<!-- 解釋:==UserEntity userEntity = new UserEntity(); --> 	<bean id="userEntity" class="com.svse.entity.UserEntity"> 		<property name="id" value="1"></property> 		<property name="name"> 			<value>小王八蛋</value> 		</property> 		<!-- 屬性名:admin,為其依賴注入 adminEntity== admin = new AdminEntity(); 並進行賦值 --> 		<property name="admin" ref="adminEntity"></property> 	</bean>  	<!-- 		2、bean 中的屬性介紹: abstract="true" 設置該bean對應的類為抽象類,不能直接進行使用 		parent="id標識,繼承的父類" 	-->  	<!-- 		===========================定義UserEntity 為抽象類========================== 	--> 	<bean id="userEntity1" class="com.svse.entity.UserEntity" 		abstract="true"> 		<property name="id" value="123"></property> 		<property name="name" value="謝遜"></property> 	</bean> 	<!-- 		通過parent這個屬性集成:抽象類(根據ID標識找到需要繼承的類),繼承了對應類中的屬性,也可以隨時的根據自己的需要更改相關的屬性 		不需要更改的可以直接的進行引用; 	--> 	<bean id="xx" parent="userEntity1"> 		<property name="id" value="456"></property> 	</bean> 	<!--============================================================== -->  	<!-- 3、bean中屬性:autowire的用法 ,以及null值的設置--> 	<!-- 		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 	--> 	<bean id="admin" class="com.svse.entity.AdminEntity">  		<!-- 將college屬性值設為:"" --> 		<property name="college"> 			<value></value> 		</property> 		<!-- 將tel的屬性值設為:null --> 		<property name="tel"> 			<null /> 		</property> 		<property name="ar"> 			<list> 				<value>八嘎</value> 				<value>九噶</value> 			</list> 		</property> 	</bean> 	<bean id="userEntity3" class="com.svse.entity.UserEntity" 		autowire="byName"> 		<property name="id" value="3"></property> 		<property name="name"> 			<value>小紅</value> 		</property> 		<!-- 			該實體下包含一個對象名稱為:admin的對象,設置了autowire 然後自動的尋找到id名為admin的bean,實現自動匹配 		--> 	</bean> 	<!-- 		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 	-->  	<!-- 4、bean的name屬性、depends-on="標識ID名" --> 	<!-- 		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 	--> 	<!-- 		bean中的ID 是唯一標識,而name 能多名稱標識,相當於給同一個bean起多個名稱,可以根據任意的一個別名獲取到當前 		的bean,命名方式如:name="/user,yy,xx,aa" , id="xx" 		代碼中可以根據任意你喜歡的名稱(user,yy,xx,aa)獲取,而 id只能根據xx獲取; depends-on: 通過ID 		指定依賴的對象,被依賴者執行之後才會執行本身;例如:處理數據之前必須先鏈接資料庫; 	--> 	<!-- 		+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 	-->  	<!-- 5、通過xml文件給實體中構造方法賦值 傳遞參數值,以及傳遞實體對象 --> 	<!-- 		============================================================================================= 	--> 	<bean id="adminEntity1" class="com.svse.entity.AdminEntity"> 		<property name="college" value="工業大學"></property> 		<property name="tel"> 			<value></value> 		</property> 		<property name="ar"> 			<null /> 		</property> 	</bean> 	<bean id="userEntity4" class="com.svse.entity.UserEntity"> 		<constructor-arg index="0" value="1"></constructor-arg> 		<constructor-arg index="1" value="大獎人"></constructor-arg> 		<constructor-arg index="2" ref="adminEntity1"></constructor-arg> 	</bean> 	<!-- 		============================================================================================= 	-->  	<!-- 6、BeanFactory 的作用域 ,scope屬性--> 	<!-- 		scope="singleton" : 單例模式 ,查詢一次后數據存入內存,然後常駐內存,再次查詢的時候仍然是內存中的數據, 		數據沒有和資料庫同步; scope="prototype" :代理模式 , 只負責封裝數據,能保證頁面數據和資料庫的同步 	--> 	<bean id="adminEntity6" class="com.svse.entity.AdminEntity"></bean> 	<bean id="userEntity6" class="com.svse.entity.UserEntity" scope="singleton"></bean> 	<!-- 單例模式下: 		UserEntity user1 = app.getBean("userEntity6",UserEntity.class); 		UserEntity user2= app.getBean("userEntity6",UserEntity.class); 		此時:user1==user2   : true 	 --> 	<bean id="userEntity7" class="com.svse.entity.UserEntity" scope="prototype"></bean> 	<!-- 代理模式下: 		UserEntity user1 = app.getBean("userEntity7",UserEntity.class); 		UserEntity user2= app.getBean("userEntity7",UserEntity.class); 		此時:user1!=user2   : false 	 --> 	 <!-- ==================================================================================== -->     </beans>

推薦閱讀文章

Bookmark the permalink ,來源:互聯網