Spring 配置文件中 Bean 的 property 属性使用示例

2022-07-01 14:06:27

原创文章,转载请注明出处。

在 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;
	}
}
<bean id="car" class="***" />
<bean id="boss" class="***">
  <property name="car">
    <ref bean="car"></ref>
  </property>
</bean

(3)内部 Bean:当 Spring IoC 容器中的 bean1 只会被 bean2 引用,而不会被容器中任何其他 Bean 引用的时候,则可以将这个 bean1 以内部 Bean 的方式注入到 bean2 中。跟 Java 中的内部类是相似的。
<bean id="boss" class="***">
  <property name="car">
    <bean class="***">
      <property name="price" value="200">
    </bean>
  </property>
</bean>


(4)null 值:有些时候,需要为某个 bean 的属性注入一个 null 值,在这里我们需要使用专用的 <null/> 元素标签,通过它可以为其他对象的属性注入 null 值。
<bean id="car" class="***">
  <property name="brand">
    <null/>
  </property>
</bean>


(5)级联属性:跟 Struts、Hibernate 等框架一样,Spring 支持级联属性的配置,例如当我们希望在定义 bean1 时直接为 bean2 的属性提供注入值,则可以采取以下的配置方式:(boss.getCar().setBrand())
public class Boss {
	private Car car;
	public void setCar(Car car) {
		this.car = car;
	}
}
<bean id="boss" class="***">
  <property name="car.brand">
    <value>奔驰E级</value>
  </property>
</bean>


(6)集合类型属性:java.util 包中的集合类是最常用的数据结构类型,主要包括 List、Set、Map 以及 Properties,Spring 为这些集合类型属性提供了专门的配置元素标签:
①  当属性为 java.util.List 的时候,
public class Boss {
	private List favorites = new ArrayList();
	public List getFavorites() {
		return favrites;
	}
	public void setFavorites(List favrites) {
		this.favrites = favorites;
	}
}
<bean id="boss" class="***">
  <property name="favorites">
    <list>
      <value>唱歌</value>
      <value>运动</value>
      <value>读书</value>
    </list>
  </property>
</bean>
② 当属性为 java.util.Set 的时候,
public class Boss {
	private Set favorites = new ArrayList();
	public Set getFavorites() {
		return favrites;
	}
	public void setFavorites(Set favrites) {
		this.favrites = favorites;
	}
}
<bean id="boss" class="***">
  <property name="favorites">
    <set>
      <value>唱歌</value>
      <value>运动</value>
      <value>读书</value>
    </set>
  </property>
</bean>
③ 当属性为 java.util.Map 的时候,
public class Boss {
	private Map favorites;
	public Map getFavorites() {
		return favrites;
	}
	public void setFavorites(Map favrites) {
		this.favrites = 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>
      <entry>
        <key><ref bean="keyBean" /></key>
        <ref bean="valueBean" />
      </entry>
    </map>
  </property>
</bean>
④ 当属性为 java.util.Properties 的时候,可以看做是属性为 Map 的一个特例,Properties 属性的键值只能是字符串,
public class Boss {
	private Properties favorites;
	public Properties getFavorites() {
		return favrites;
	}
	public void setFavorites(Properties favrites) {
		this.favrites = favorites;
	}
}
<bean id="boss" class="***">
  <property name="favorites">
      <props>
        <prop key="p01">唱歌</prop>
        <prop key="p02">运动</prop>
        <prop key="p03">读书</prop>
      </props>
    </properties>
  </property>
</bean>
⑤ 强类型结合:根据 JDK5.0 提供的强类型集合功能,在配置文件中,允许为集合元素指定类型:
public class Boss {
	private Map<Integer, String> favorites;
	public Map getFavorites() {
		return favrites;
	}
	public void setFavorites(Map favrites) {
		this.favrites = favorites;
	}
}
<bean id="boss" class="***">
  <property name="favorites">
      <map>
      <entry>
        <key><value>101</value></key>
        <value>唱歌</value>
      </entry>
    </map>
  </property>
</bean>

⑥集合合并:配置文件中的集合合并的功能,允许子 <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>


(7)简化配置方式:Spring 为字面值、引用 Bean 和集合都提供了相对简化的配置方式。

(8)自动装配:就是不再使用 ref 进行手工装配 Bean,这种方式可以减少配置文件的代码量,但是在大型项目中,不推荐使用,容易混乱。

①autowire="byName"

②autowire="byType"

③autowire="constructor"

④autowire="autodetect"

<beans> 元素标签中的 default-autowire 属性可以配置全局自动装配,其属性的默认值为 no,标志不启用自动装配;在 <beans> 中定义的自动装配策略可以被 <bean> 的自动装配策略覆盖。




  • 作者:_Hin
  • 原文链接:https://blog.csdn.net/qq_21396469/article/details/63684769
    更新时间:2022-07-01 14:06:27