在 Spring 配置文件中,beans 元素是 spring-beans 内容模型的根结点,bean 元素是 beans 元素的子节点,通常 bean 元素用于定义 JavaBean。而 bean 元素包含以下几种子元素,它们分别是:
- constructor-arg 元素
- property 元素
- lookup-method 元素
- replace-method 元素
在 Spring 配置文件中,用户可以通过 Bean 的属性 property 进行参数注入。使用 property 属性不但可以将 String、int 等字面值注入到 Bean 中,还可以将集合、Map 等类型注入到 Bean 中,此外还可以注入其他的 Bean。
(1)字面值:一般是指可用字符串表示的值,这些值可通过 <value> 元素标签进行注入。在默认情况下,基本数据类型及其封装类、String 等类型都可以采取字面值注入的方式。
(2)引用其他的 Bean:Spring IoC 容器中定义的 Bean 可以互相引用,IoC 容器则充当了介绍人的角色。<ref> 元素可以通过 bean、local、parent 三个属性引用其他 Bean 的属性,其中 bean 可以引用统一配置文件中或者父容器中的 Bean,local 只能引用同一配置文件中的 Bean,parent 只能引用父容器中的 Bean。
-
public
class Boss {
-
private Car car;
-
public void setCar(Car car) {
-
this.car = car;
-
}
-
}
- 1
-
<bean id=
"car"
class=
"***" />
-
<bean id="boss" class="***">
-
<property name="car">
-
<ref bean="car">
</ref>
-
</property>
-
</bean
- 1
-
<bean id=
"boss"
class=
"***">
-
<property name="car">
-
<bean class="***">
-
<property name="price" value="200">
-
</bean>
-
</property>
-
</bean>
- 1
-
<bean id=
"car"
class=
"***">
-
<property name="brand">
-
<null/>
-
</property>
-
<
/bean>
- 1
-
public
class Boss {
-
private Car car;
-
public void setCar(Car car) {
-
this.car = car;
-
}
-
}
- 1
-
<bean id=
"boss"
class=
"***">
-
<property name="car.brand">
-
<value>奔驰E级
</value>
-
</property>
-
<
/bean>
- 1
① 当属性为 java.util.List 的时候,
-
public
class Boss {
-
private List favorites =
new ArrayList();
-
public List getFavorites() {
-
return favrites;
-
}
-
public void setFavorites(List favrites) {
-
this.favrites = favorites;
-
}
-
}
- 1
-
<bean id=
"boss"
class=
"***">
-
<property name="favorites">
-
<list>
-
<value>唱歌
</value>
-
<value>运动
</value>
-
<value>读书
</value>
-
</list>
-
</property>
-
<
/bean>
- 1
-
public
class Boss {
-
private Set favorites =
new ArrayList();
-
public Set getFavorites() {
-
return favrites;
-
}
-
public void setFavorites(Set favrites) {
-
this.favrites = favorites;
-
}
-
}
- 1
-
<bean id=
"boss"
class=
"***">
-
<property name="favorites">
-
<set>
-
<value>唱歌
</value>
-
<value>运动
</value>
-
<value>读书
</value>
-
</set>
-
</property>
-
<
/bean>
- 1
-
public
class Boss {
-
private Map favorites;
-
public Map getFavorites() {
-
return favrites;
-
}
-
public void setFavorites(Map favrites) {
-
this.favrites = favorites;
-
}
-
}
- 1
-
<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>
-
<entry>
-
<key>
<ref bean="keyBean" />
</key>
-
<ref bean="valueBean" />
-
</entry>
-
</map>
-
</property>
-
</bean>
- 1
-
public
class Boss {
-
private Properties favorites;
-
public Properties getFavorites() {
-
return favrites;
-
}
-
public void setFavorites(Properties favrites) {
-
this.favrites = favorites;
-
}
-
}
- 1
-
<bean id="boss" class="***">
-
<property name="favorites">
-
<props>
-
<prop key="p01">唱歌
</prop>
-
<prop key="p02">运动
</prop>
-
<prop key="p03">读书
</prop>
-
</props>
-
</properties>
-
</property>
-
</bean>
- 1
-
public
class Boss {
-
private Map<Integer, String> favorites;
-
public Map getFavorites() {
-
return favrites;
-
}
-
public void setFavorites(Map favrites) {
-
this.favrites = favorites;
-
}
-
}
- 1
-
<bean id=
"boss"
class=
"***">
-
<property name="favorites">
-
<map>
-
<entry>
-
<key>
<value>101
</value>
</key>
-
<value>唱歌
</value>
-
</entry>
-
</map>
-
</property>
-
<
/bean>
- 1
⑥集合合并:配置文件中的集合合并的功能,允许子 <bean> 集成父 <bean> 的同名属性集合元素,并将子 <bean> 中配置的集合属性值和父 <bean> 中配置的同名属性值合并,作为最终 <bean> 的属性值,
-
<bean id="parentBoss" abstract="true" class="***">
-
<property name="favorites">
-
<set>
-
<value>唱歌
</value>
-
<value>运动
</value>
-
<value>读书
</value>
-
</set>
-
</property>
-
</bean>
-
-
<bean id="childBoss" parent="parentBoss">
-
<property name="favorites">
-
<set merge="true">
-
<value>旅游
</value>
-
<value>睡觉
</value>
-
</set>
-
</property>
-
</bean>
- 1
(8)自动装配:就是不再使用 ref 进行手工装配 Bean,这种方式可以减少配置文件的代码量,但是在大型项目中,不推荐使用,容易混乱。
①autowire="byName"
②autowire="byType"
③autowire="constructor"
④autowire="autodetect"
<beans> 元素标签中的 default-autowire 属性可以配置全局自动装配,其属性的默认值为 no,标志不启用自动装配;在 <beans> 中定义的自动装配策略可以被 <bean> 的自动装配策略覆盖。
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e44c3c0e64.css" rel="stylesheet">
</div>
</article>