Bean中屬性註入(4)
阿新 • • 發佈:2018-04-25
Bean中屬性註入Spring支持構造方法註入和setter方法註入:
構造器註入:
car.java
package cn.spring.demo5; public class Car { private String name; private Double prices; public Car() { super(); } public Car(String name, Double prices) { super(); this.name = name; this.prices = prices; } @Override public String toString() { return "Car [name=" + name + ", prices=" + prices + "]"; } }
有兩種方式(兩者是一樣的結果): <bean id="car" class="cn.spring.demo5.Car" > <!-- 構造方法的註入 (第1種)--> <constructor-arg name="name" value="寶馬"/> <constructor-arg name="prices" value="100000.00"/> <!-- 下標索引的方式 (第2種)--> <constructor-arg index="0" type="java.lang.String" value="奔馳"/> <constructor-arg index="1" type="java.lang.Double" value="200000"/> </bean>
setter方法註入:
car2.java
package cn.spring.demo5; public class Car2 { private String name; private Double prices; public void setName(String name) { this.name = name; } public void setPrices(Double prices) { this.prices = prices; } @Override public String toString() { return "Car2 [name=" + name + ", prices=" + prices + "]"; } }
- setter方法註入
<bean id="car2" class="cn.spring.demo5.Car2" > <!-- property name是屬性名稱,value是普通的屬性的值,ref:引用的是對象 --> <property name="name" value="保時捷"></property> <property name="prices" value="300000"></property> </bean>
- setter方法註入對象屬性
<property name="car2" ref="car"/>
名稱 空間註入方式
步驟1:現在xml先引入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.xsd">
car3.java
package cn.spring.demo5;
public class Car3 {
private String name;
private Double prices;
public void setName(String name) {
this.name = name;
}
public void setPrices(Double prices) {
this.prices = prices;
}
@Override
public String toString() {
return "Car3 [name=" + name + ", prices=" + prices + "]";
}
}
<!-- 名稱 空間註入方式,先引入xmlns:p="http://www.springframework.org/schema/p"-->
<bean id="car3" class="cn.spring.demo5.Car3" p:name="奧迪" p:prices="400000"/>
SpEL:屬性的註入
Spring3.0提供註入屬性方式:
語法:#{表達式}
<bean id="" value="#{表達式}">
car4.java
package cn.spring.demo5;
public class Car4 {
private String name;
private Double prices;
public void setName(String name) {
this.name = name;
}
public void setPrices(Double prices) {
this.prices = prices;
}
@Override
public String toString() {
return "Car4 [name=" + name + ", prices=" + prices + "]";
}
}
<!--SpEL:屬性的註入: -->
<bean id="car4" class="cn.spring.demo5.Car4">
<property name="name" value="#{‘大眾‘}"></property>
<property name="prices" value="#{‘500000‘}"></property>
</bean>
ref引用的是其他id或name對象例子
person.java
package cn.spring.demo5;
public class Person {
private String name;
private Car2 car;
public void setName(String name) {
this.name = name;
}
public void setCar(Car2 car) {
this.car = car;
}
@Override
public String toString() {
return "Person [car=" + car + ", name=" + name + "]";
}
}
<bean id="person1" class="cn.spring.demo5.Person1" p:name="李四" p:car-ref="car2">
</bean>
ref引用的是其他類對象或方法的演示
personinfo.java
package cn.spring.demo5;
public class PersonInfo {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public String showname(){
return name+"七";
}
}
引用其他類(personinfo.java)的屬性
person2.java
package cn.spring.demo5;
public class Person2 {
private String name;
private Car2 car;
public void setName(String name) {
this.name = name;
}
public void setCar(Car2 car) {
this.car = car;
}
@Override
public String toString() {
return "Person2 [car=" + car + ", name=" + name + "]";
}
}
<!--SpEL:屬性的註入:調用屬性 -->
<bean id="person2" class="cn.spring.demo5.Person2" >
<!-- property name是屬性名稱,value是普通的屬性的值,ref:引用的是其他id或name對象 -->
<property name="name" value="#{personinfo.name}"></property>
<property name="car" value="#{car2}"></property>
</bean>
<!-- 註意要有get方法 -->
<bean id="personinfo" class="cn.spring.demo5.PersonInfo">
<property name="name" value="#{‘王五六‘}"></property>
引用其他類(personinfo.java)的方法
package cn.spring.demo5;
public class Person3 {
private String name;
private Car2 car;
public void setName(String name) {
this.name = name;
}
public void setCar(Car2 car) {
this.car = car;
}
@Override
public String toString() {
return "Person3 [car=" + car + ", name=" + name + "]";
}
}
<!--SpEL:屬性的註入: 可以調用方法-->
<bean id="person3" class="cn.spring.demo5.Person3" >
<!-- property name是屬性名稱,value是普通的屬性的值,ref:引用的是其他id或name對象 -->
<property name="name" value="#{personinfo.showname()}"></property>
<property name="car" value="#{car2}"></property>
</bean>
測試類編寫:
package cn.spring.demo5;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class springtest5 {
@Test
public void demo1() {
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Car car=(Car) applicationcontext.getBean("car");
System.out.println(car);
}
@Test
public void demo2() {
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Car2 car=(Car2) applicationcontext.getBean("car2");
System.out.println(car);
}
@Test
public void demo2_1() {
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Car3 car=(Car3) applicationcontext.getBean("car3");
System.out.println(car);
}
@Test
public void demo2_2() {
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Car4 car=(Car4) applicationcontext.getBean("car4");
System.out.println(car);
}
@Test
public void demo3() {
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Person person=(Person) applicationcontext.getBean("person");
System.out.println(person);
}
@Test
public void demo3_1() {
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Person1 person=(Person1) applicationcontext.getBean("person1");
System.out.println(person);
}
@Test
public void demo3_2() {
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Person2 person=(Person2) applicationcontext.getBean("person2");
System.out.println(person);
}
@Test
public void demo3_3() {
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
Person3 person=(Person3) applicationcontext.getBean("person3");
System.out.println(person);
}
}
集合屬性的註入
CollectionBean.java
package cn.spring.demo6;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionBean {
private List<String> list;//list集合(1)
private Set<String> set;//set集合(2)
private Map<String, Integer> map;//map集合(3)
private Properties properties;//屬性(4)
private String[] arrary;//數組(5)
public void setList(List<String> list) {
this.list = list;
}
public void setSet(Set<String> set) {
this.set = set;
}
public void setMap(Map<String, Integer> map) {
this.map = map;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
public void setArrary(String[] arrary) {
this.arrary = arrary;
}
@Override
public String toString() {
return "CollectionBean [arrary=" + Arrays.toString(arrary) + ", list="
+ list + ", map=" + map + ", properties=" + properties
+ ", set=" + set + "]";
}
}
對應applicationContext.xml配置入下:
<!-- bean 集合的屬性註入-->
<bean id="collectionBean" class="cn.spring.demo6.CollectionBean">
<!-- 註入list集合(1) -->
<property name="list">
<list>
<value>統統list</value>
<value>小風list</value>
<!-- 如果是對象用ref -->
</list>
</property>
<!-- 註入set集合(2) -->
<property name="set">
<set>
<value>統統set</value>
<value>小風set</value>
</set>
</property>
<!-- 註入map集合(3)-->
<property name="map">
<map>
<entry key="統統map" value="1111"/>
<entry key="統統map1" value="2222"/>
</map>
</property>
<!-- 註入properties集合(4) -->
<property name="properties">
<props>
<prop key="統統prop" >3333</prop>
<prop key="統統prop" >4444</prop>
</props>
</property>
<!-- 註入數組(5) -->
<property name="arrary">
<list>
<value>統統arrary</value>
<value>小風arrary</value>
<!-- 如果是對象用ref -->
</list>
</property>
</bean>
<!-- bean集合的屬性註入-->
測試類編寫如下:
package cn.spring.demo6;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest6 {
@Test
public void demo1() {
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml");
//如果想分開引pring的配置文件有兩種方式
/*
* 第一種:ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
* "applicationContext.xml","applicationContext2.xml");
*
* 第二種:applicationContext.xml文件中加入<import resource="applicationContext2.xml"/>
*/
CollectionBean collectionBean=(CollectionBean) applicationcontext.getBean("collectionBean");
System.out.println(collectionBean);
}
}
加載其他或者多個配置文件
- 一種寫法(java代碼裏):
ApplicationContext applicationcontext = new ClassPathXmlApplicationContext(
"applicationContext.xml","applicationContext2.xml"); - 二種方法(applicationContext.xml文件中):
applicationContext.xml文件中加入<import resource="applicationContext2.xml"/>
Bean中屬性註入(4)