Spring攻略筆記-1

Spring提供兩種IOC容器實現類型。基本的一種是Bean工廠(Bean Factory)。更高級的是稱爲應用程序上下文(Application context),是對Bean工廠的一種兼容的擴展。應用程序上下文提供比Bean工廠更高級的特性,同時保持基本特性的兼容。除非是資源有限的應用程序(例如運行一個小腳本或移動設備),否則最好使用Application context。

Bean工廠和Application context 的接口分別是BeanFactory和ApplicationContext。ApplicationContext是BeanFactory的子接口。

ApplicationContext只是個接口,必須實例化一個接口的實現可以使用以下的一些實現類:

1、ClassPathXmlApplicationContext:實現裝載一個xml配置文件,構建一個應用程序上下文。

ApplicationContext ac=new ClassPathXmlApplicationContext("beans.xml");

2、FileSystemXmlApplicationContext:用於從文件系統或URL裝載xml配置文件。

3、XmlWebApplicationContext和XmlPortletApplicationContext:僅能用於Web和入口應用程序。

爲了從ApplicationContext中得到已聲明的bean,只需要調用getBean()方法,並且傳入唯一的bean名稱。返回的是Object類型,所有要進行類型轉換

User user=(User)ac.getBean("user");

到這裏就可以獲取到Bean實例了

package com.lkt.entity;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	public static void main(String[] args) {
		ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
		
		Student student=(Student)ac.getBean("studentBean");
		
		System.out.println(student.toString());
	}
}



Spring可以使用xml文件,屬性文件,註解來配置Spring Ioc容器中的Bean。Spring允許在一個或在多個bean配置文件中配置bean。

首先創建Bean類,即實體類

package com.lkt.entity;


public class Student {
	private String userName;
	private String password;
	private String realName;
	
	private Clazz clazz;
	
	public Clazz getClazz() {
		return clazz;
	}
	public void setClazz(Clazz clazz) {
		this.clazz = clazz;
	}
	public Student() {
		
	}
	public Student(String userName,String password,String realName) {
		this.userName=userName;
		this.password=password;
		this.realName=realName;
	}
	@Override
	public String toString() {
		
		return "userName:"+userName+"  password:"+password+"  realName:"+realName;
	}
	
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
}

創建Bean配置文件

使用xml方式,名爲applicationContext.xml在classpath根路徑下

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
						">
						
	<!-- 配置一個Bean -->				
	<bean name="studentBean" class="com.lkt.entity.Student" scope="prototype">
		<!-- 配置屬性 -->
		<property name="userName" value="zhangsan"></property>
		<property name="password" value="zhangsan123"></property>
		<property name="realName" value="張三"></property>
		<!-- 配置構造函數 -->
		<constructor-arg value="zhangsans"/>
		<constructor-arg value="zhangsan123"/>
		<constructor-arg value="張三"/>
	</bean>
</beans>

同時,還有個更簡寫的方法,即p schema

需要添加xmlns:p="http://www.springframework.org/schema/p"的聲明

<?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-2.5.xsd
						">
						
	<!-- 配置一個Bean -->				
	<bean name="studentBean" class="com.lkt.entity.Student"
		 p:password="lisi" 
		 p:userName="lisi" 
		 p:realName="lisi" scope="prototype"/>
		
</beans>

類中引用別的類


<?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-2.5.xsd
						">
						
	<!-- 配置一個Bean -->				
	<bean name="studentBean" class="com.lkt.entity.Student"
		 p:password="lisi" 
		 p:userName="lisi" 
		 p:realName="lisi"
		 p:clazz-ref="classBean"
		 scope="prototype"/>
	
	<bean name="classBean" class="com.lkt.entity.Clazz" scope="prototype">
		<property name="className" value="一年三班"></property>
	</bean>
</beans>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章