一.
Bean 元素说明
attributes:
id
name
class
parent
abstract
scope
lazy-init
autowire
dependency-check
depends-on
init-method
destroy-method
factory-method
factory-bean
子元素:
description
constructor-arg
property
lookup-method
replace-method
二.
Bean property:设置类属性值
字面值:
<property name=“maxSpeed”>
<value>200</value>
</property>
<property name=“brand”>
<value><![CDATA[红旗&CA72]]</value>
</property>
引用其他Bean:
public class Boss{
private Car car;
public void setCar(Car car) {
this.car = car;
}
}
<bean id=“car” class=“***”/>
<bean id=“boss” class=“***”>
<property name=“car”>
<ref bean=“car”></ref>
</property>
</bean>
内部Bean:
<bean id=“boss” class=“***”/>
<property name=“car”>
<bean class=“***” >
<property name=“price” value=“200”>
</bean>
</property>
</bean>
null值:<null/>
级联:public class Boss{
private Car car = nea Car();
public void setCar(Car car) {
this.car = car;
}
}
<bean id=“boss” class=“***”>
<property name=“car.brand”>
<value>奔驰E级</value>
</property>
</bean>
属性(List):
public class Boss{
private List favorites = new ArrayList();
public List getFavorites() {
return favorites;
}
public void setFavorites(List favorites) {
this.favorites = favorites;
}
}
<bean id=“boss” class=“***”>
<property name=“favorites”>
<list>
<value>唱歌</value>
<value>运动</value>
<value>读书</value>
</list>
</property>
</bean>
属性(Set):
和List差别不大
属性(Map):
public class Boss{
private Map favorites;
public Map getFavorites() {
return favorites;
}
public void setFavorites(Map favorites){
this.favorites = favorites;
}
}
<bean id=“boss” class=“***”>
<property name=“favorites”>
<map>
<entry>
<key><value>key01</value></key>
<value>唱歌</value>
</entry>
<entry>
<key><value>key02</value></key>
<value>唱歌</value>
</entry>
</map>
</property>
</bean>
集合合并:
<bean id=“parentBoss”abstract=“true”class=“***”>
<property name=“favorites”>
<set>
<value>唱歌</value>
<value>运动</value>
<value>看书</value>
</set>
</property>
</bean>
<bean id=“childtBoss”parent=“parentBoss”>
<property name=“favorites”>
<set merge=“true”>
<value>旅游</value>
<value>睡觉</value>
</set>
</property>
</bean>
自动装配:
autowire=“byName”
autowire=“ byType ”
autowire=“ constructor ”
autowire=“ autodetect ”
属性注入方法:
Public class Car {
private String brand;
public void setBrand(String brand) {
this.brand = brand;
}
public String getBrand() {
return this.brand;
}
}
<bean id=“car” class=“com.jike.***.Car” >
<property name=“brand”>
<value>奔驰</value>
</property>
</bean>
构造函数注入:
工厂方法注入:
三.
--
Bean 的依赖关系:继承、依赖、引用
继承:<bean id=“abstractCar” class=“com.jike.***.Car”/
p:brand=“奔驰E300” p:price=“2000” p:color=“黑色” abstract=“true”> ①
<bean id=“car1” p:color=“红色” parent=“abstractCar” />②
<bean id=“car1” p:color=“白色” parent=“abstractCar” />③
依赖:保证Bean依赖的Bean已初始化
<bean id=“manager” class=“com.jike.***.CacheManager” depends-on=“sysInit”; /> ①
<bean id=“sysInit” class=“com.jike.***.SysInit” /> ②
引用:idref