spring學習筆記(3)——bean配置細節註意
阿新 • • 發佈:2017-08-07
collect 1.5 之前 ice ble person name return 引用
1. 一個bean引用另外一個bean
當Person類中有一個屬性是Car,那麽該如何配置呢
person:
package com.zj.spring;
public class Person {
private String name;
private int age;
private Car car;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age +
", car=" + car + "]";
}
}
car:
package com.zj.spring;
public class Car {
private String brand;
private double price;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
使用ref來引用其他bean
<bean id="car" class="com.zj.spring.Car">
<property name="brand" value="bmw"></property>
<property name="price" value="1111111"></property>
</bean>
<bean id="person" class="com.zj.spring.Person">
<property name="name" value="tom"></property>
<!-- 使用ref來引用其他的bean -->
<property name="car" ref="car"></property>
</bean>
2. 當value中有特殊符號
如果value值有 <
這樣子的特殊符號,將會出錯,因為該符號在xml文件中是節點的起始
我們可以使用<![CDATA[value]]>
來解決 <bean id="car" class="com.zj.spring.Car">
<property name="brand">
<value><![CDATA[<bmw>]]></value>
</property>
<property name="price" value="1111111"></property>
</bean>
3. 使用內部bean
之前person引入car類,我們也可以使用內部類
<bean id="person" class="com.zj.spring.Person">
<property name="name" value="tom"></property>
<!-- 使用ref來引用其他的bean -->
<!-- <property name="car" ref="car"></property> -->
<property name="car"><!-- 內部bean -->
<bean id="car" class="com.zj.spring.Car">
<property name="brand" value="bmw"></property>
<property name="price" value="1111111"></property>
</bean>
</property>
</bean>
兩者有什麽區別
內部bean可以不寫id,內部bean不能被其他bean使用,有點像匿名類,內部類的感覺
4. null值
如果你想給屬性賦null值,應該這樣寫
<property name="brand"><null/></property>
5. 級聯屬性配置
<bean id="person" class="com.zj.spring.Person">
<property name="name"><null/></property>
<!-- 使用ref來引用其他的bean -->
<!-- 如果沒有引用,那麽使用級聯屬性時候將有異常 -->
<property name="car" ref="car"></property>
<property name="car.brand" value="bwm"></property>
<property name="car.price" value="4444444"></property>
</bean>
6. 集合屬性
假設一個person有好幾輛Car
我們改造一下person
package com.zj.collection;
import java.util.List;
public class Person {
private String name;
private int age;
private List<Car> cars;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<Car> getCars() {
return cars;
}
public void setCars(List<Car> cars) {
this.cars = cars;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", cars=" + cars + "]";
}
}
car還是和原來一樣。那麽該怎麽配置呢
<!-- 集合屬性 -->
<bean id="car1" class="com.zj.collection.Car">
<property name="brand" value="bwm"></property>
<property name="price" value="111111"></property>
</bean>
<bean id="car2" class="com.zj.collection.Car">
<property name="brand" value="auti"></property>
<property name="price" value="222222"></property>
</bean>
<bean id="car3" class="com.zj.collection.Car">
<property name="brand" value="KIA"></property>
<property name="price" value="333333"></property>
</bean>
<bean id="person1" class="com.zj.collection.Person">
<property name="name" value="tom"></property>
<property name="age" value="22"></property>
<property name="cars">
<list> <!-- 使用list節點配置集合屬性 -->
<ref bean="car1"/>
<ref bean="car2"/>
<ref bean="car3"/>
</list>
</property>
</bean>
- 同樣的,除了使用list節點,也可以使用set節點和map節點
- map比較特殊一些,因為他是鍵值對的形式
map可以這樣配置
<map>
<entry key="bwm" value-ref="car1"></entry>
<entry key="auti" value-ref="car2"></entry>
<entry key="KIA" value-ref="car3"></entry>
</map>
Java.util.Properties類型
在配置數據庫的時候,會需要properties,那麽該如何配置這種類型的bean呢
先寫一個DataSource類模擬一下
package com.zj.collection;
import java.util.Properties;
public class DataSource {
private Properties properties;
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "DataSource [properties=" + properties + "]";
}
}
配置
<bean id="dataSource" class="com.zj.collection.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">123</prop>
<prop key="jdbcUrl">jdbc:mysql://xxxx</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
mian方法
DataSource dataSource = (DataSource) ctx.getBean("dataSource");
System.out.println(dataSource);
打印結果
7. 配置獨立的集合bean,供多個bean引用
上面的集合屬性,都是在bean的內部,不能被其他的bean引用,我們可以使用util:list來配置集合,就可以被多個bean引用
<!-- 需要 引入util命名空間-->
<!--
<util:list id="cars">
<ref bean="car1"/>
<ref bean="car2"/>
</util:list>
-->
<util:map id="cars">
<entry key="bwm" value-ref="car1"></entry>
<entry key="audi" value-ref="car2"></entry>
</util:map>
<bean id="person2" class="com.zj.collection.Person">
<property name="name" value="tom"></property>
<property name="age" value="22"></property>
<property name="cars" ref="cars"></property>
</bean>
還有util:properties等,用法都差不多
- 註意,需要引入util命名空間
xmlns:util="http://www.springframework.org/schema/util"
8. 使用p命名空間
p命名空間可以簡化我們配置bean的過程,減少工作量
先導入p命名空間
xmlns:p="http://www.springframework.org/schema/p"
配置
<bean id="person3" class="com.zj.collection.Person" p:name="tom" p:age="33" p:cars-ref="cars"></bean
spring學習筆記(3)——bean配置細節註意