【初識Spring】物件(Bean)例項化及屬性注入(xml方式)
阿新 • • 發佈:2019-02-20
初識Spring之Bean例項化及屬性注入
1.通過xml配置的方式進行例項化。
- 配置檔案中bean標籤的常用屬性
- 通過無參構造(常用)
- 使用靜態工廠進行建立
- 使用例項工廠進行建立
2.屬性注入。
- 使用有引數構造方法注入屬性
- 使用set方法注入屬性(常用)
- 注入物件型別屬性
- 注入複雜型別屬性
xml配置的方式進行例項化
- 配置檔案中bean標籤的屬性
(1)id屬性:起名稱,id屬性值名稱任意命名
- id屬性值,不能包含特殊符號
- 根據id值得到配置物件
(2)class屬性:建立物件所在類的全路徑
(3)name屬性:功能和id屬性一樣的,id屬性值不能包含特殊符號,但是在name屬性值裡面可以包含特殊符號
(4)scope屬性
- singleton:預設值,單例
- prototype:多例
- 無參構造例項化物件
//phone類:
package com.test.vo;
public class Phone {
public void printTest() {
System.out.print("Phone.......");
}
}
<!--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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="phone" class="com.test.vo.Phone"></bean>
</beans>
//測試類
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//載入配置檔案,建立物件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//得到配置建立的物件
Phone Phone = (Phone) context.getBean("phone");
//呼叫物件方法
Phone.printTest();
}
}
注:java類中預設有無參構造方法,若類中已聲明瞭有參構造,則需手動宣告無參構造方法。
- 使用靜態工廠進行建立
//靜態工廠類
package com.test.utils;
import com.test.vo.Phone;
public class BeanFactory {
//靜態方法,返回Phone物件
public static Phone getPhone() {
return new Phone();
}
}
//建立的物件為Phone類物件不變
//配置檔案改為:
<!--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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--class為靜態工廠的路徑,factory-method為工廠的方法-->
<bean id="phoneFa" class="com.test.utils.BeanFactory" factory-method="getPhone"></bean>
</beans>
//測試類
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Phone Phone = (Phone) context.getBean("phoneFa");
Phone.printTest();
}
}
- 使用例項工廠進行建立
//實列工廠類:
import com.test.vo.Phone;
public class BeanUFactory {
//普通方法,返回Phone物件
public Phone getPhone() {
return new Phone();
}
}
配置檔案修改:
<!-- 1.先建立工廠物件 -->
<!-- 2.再建立Phone物件 -->
<bean id="BeanUFactory" class="com.test.utils.BeanUFactory"></bean>
<bean id="phoneUFa" factory-bean="BeanUFactory" factory-method="getPhone"></bean>
//測試類:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Phone Phone = (Phone) context.getBean("phoneUFa");
Phone.printTest();
}
}
屬性注入
- 使用有引數構造方法注入屬性:
Phone類改寫為:
public class Phone {
private String name;
//顯示宣告無參構造
public Phone() {}
//有參構造
public Phone(String name) {
this.name=name;
}
public void printTest() {
System.out.print(name+"Phone.......");
}
}
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--class為靜態工廠的路徑,factory-method為工廠的方法-->
<bean id="phoneFa" class="com.test.utils.BeanFactory" factory-method="getPhone">
<!--name為構造方法的引數名,value為要將其設定的值-->
<constructor-arg name="name" value="諾基亞"></constructor-arg>
</bean>
</beans>
測試類:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Phone Phone = (Phone) context.getBean("phoneFa");
Phone.printTest();
}
}
結果:
諾基亞Phone.......
- 使用set方法注入屬性:
Phone類改寫為:
public class Phone {
private String name;
//set方法
public void setName(String name) {
this.name = name;
}
public void printTest() {
System.out.print(name+"Phone.......");
}
}
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--class為靜態工廠的路徑,factory-method為工廠的方法-->
<bean id="phoneFa" class="com.test.utils.BeanFactory" factory-method="getPhone">
<!--name為要注入的屬性的名稱,value為要將其設定的值-->
<property name="name" value="三星"></property>
</bean>
</beans>
結果:
三星Phone.......
- 注入物件型別屬性
新建Person類:
public class Person {
private String name;
private String sex;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
Phone類修改為:
package com.test.vo;
public class Phone {
private String name;
private Person person;
//set方法
public void setName(String name) {
this.name = name;
}
public void setPerson(Person person) {
this.person = person;
}
public void printTest() {
System.out.print(person.getName()+"::"+person.getAge()+"::"+person.getSex());
}
}
配置檔案作如下修改:
<?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.xsd">
<bean id="person" class="com.test.vo.Person" scope="prototype">
<property name="name" value="小王"></property>
<property name="sex" value="man"></property>
<property name="age" value="11"></property>
</bean>
<bean id="phone" class="com.test.vo.Phone">
<!-- 因注入的是物件寫ref屬性 -->
<property name="person" ref="person"></property>
</bean>
</beans>
測試方法不變,結果為:
小王::11::man
- 注入複雜型別屬性
Phone類修改為:
package com.test.vo;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public class Phone {
private String arr[];
private List<Integer> list;
private Map<String,String> map;
//set方法
public void setArr(String[] arr) {
this.arr = arr;
}
public void setList(List<Integer> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void printTest() {
System.out.println("arr:"+Arrays.toString(arr));
System.out.println("list:"+list);
System.out.println("map:"+map);
}
}
配置檔案作如下修改:
<?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.xsd">
<bean id="phone" class="com.test.vo.Phone">
<!-- 陣列 -->
<property name="arr">
<list>
<value>小米</value>
<value>中興</value>
<value>華為</value>
</list>
</property>
<!-- list集合 -->
<property name="list">
<list>
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<!-- map集合 -->
<property name="map">
<map>
<entry key="aa" value="lucy"></entry>
<entry key="bb" value="bob"></entry>
<entry key="cc" value="jerry"></entry>
</map>
</property>
</bean>
</beans>
結果如下:
arr:[小米, 中興, 華為]
list:[1, 2, 3]
map:{aa=lucy, bb=bob, cc=jerry}