您的位置:首页 > 编程语言 > Java开发

(三)Spring XML 配置 之Bean 属性

2016-02-26 15:05 495 查看
Bean 的属性注入方式,分为属性注入 和 构造器注入。 属性注入包含如下几种

1. java 基本类型

2. List

3. Set

4. Map

5. Property

6. Java Bean

【1. 构造器注入】

【构造器注入一】



<!-- 1.1 构造器注入:  constructor-arg 没有name属性, spring 会自动进行类型转换-->
<bean id="computerBean" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<constructor-arg value="lenovo-Y470" />
<constructor-arg value="5000.0" />
</bean>


ComputerBean 必须有以下构造方法

public ComputerBean(String name, double price) {
this.name = name;
this.price = price;
}


【构造器注入二】

<!-- 1.2构造器注入:  constructor-arg 可以通过index 属性指定参数位置, 从0 开始  -->
<bean id="computerBean2" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<constructor-arg index="0" value="Lenovo-Y470"/>
<constructor-arg index="1" value="5000.0"/>
</bean>
ComputerBean 必须有以下构造方法

public ComputerBean(String name, double price) {
this.name = name;
this.price = price;
}


【构造器三】

<bean id="computerBean" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<constructor-arg value="lenovo-Y470" />
<constructor-arg value="5000.0" />
</bean>

<!-- 1.3 构造器注入: 注入其他Bean  -->
<bean id="computerBean3" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<constructor-arg ref="computerBean"/>
</bean>
ComputerBean 必须有以下构造方法

public ComputerBean(String name, double price) {
this.name = name;
this.price = price;
}


【2.1 属性注入-- 基本类型注入】

<!-- 2.1属性注入:name 为属性名称. 注意:此种情况必须有无参构造函数, 和属性相应的set 方法 -->
<bean id="computerBean4" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<property name="name" value="lenovo-Y470"/>
<property name="price" value="5000.0"/>
</bean>
ComputerBean 必须有以下set方法和无参构造函数,因为spring 是根据反射来注入属性值的

public void setName(String name) {
this.name = name;
}

public void setPrice(double price) {
this.price = price;
}


【2.2 属性注入-- 数组注入】

<!-- 2.2属性注入:name 为属性名称. 注意:此种情况必须有无参构造函数, 和属性相应的set 方法 -->
<bean id="computerBean22" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<property name="listKeys">
<array>
<value>F1</value>
<value>F2</value>
<value>F3</value>
</array>
</property>
</bean>
ComputerBean 必须有以下set方法和无参构造函数

public void setArrayKeys(String[] arrayKeys) {
this.arrayKeys = arrayKeys;
}


【2.3 属性注入--List类型注入】

<!-- 2.1属性注入:List -->
<bean id="computerBean5" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<property name="listKeys">
<list>
<value>F1</value>
<value>F2</value>
<value>F3</value>
<value>F..</value>
</list>
</property>
</bean>
ComputerBean 必须有以下set方法和无参构造函数

public void setListKeys(List<String> listKeys) {
this.listKeys = listKeys;
}


【2.4 属性注入-- Set类型注入】

<!-- 2.2属性注入:Set 需要有需要有无参构造方法-->
<bean id="computerBean6" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<property name="setKeys">
<set>
<value>F1</value>
<value>F2</value>
<value>F3</value>
<value>F..</value>
</set>
</property>
</bean>
ComputerBean 必须有以下set方法和无参构造函数

public void setSetKeys(Set<String> setKeys) {
this.setKeys = setKeys;
}


【2.5 属性注入--Map 类型注入】

entry 还有属性 key-ref, value-ref , 对应于key 和 value 为非java 基本类型的情况。

<!-- 2.3属性注入:Map 需要有需要有无参构造方法-->
<bean id="computerBean7" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<property name="mapKey">
<map>
<entry key="A" value="aaa"/>
<entry key="B" value="bbb"/>
<entry key="C" value="ccc"/>
</map>
</property>
</bean>
ComputerBean 必须有以下set方法和无参构造函数

public void setMapKey(Map<String, String> mapKey) {
this.mapKey = mapKey;
}


【2.6 属性注入--Property注入】

<!-- 2.5属性注入:Properties 需要有需要有无参构造方法-->
<bean id="computerBean8" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<property name="properties">
<props>
<prop key="A">aaaa</prop>
<prop key="B">bbbb</prop>
<prop key="C">cccc</prop>
</props>
</property>
</bean>
ComputerBean 必须有以下set方法和无参构造函数

public void setProperties(Properties properties) {
this.properties = properties;
}


【2.7 属性注入 -- JavaBean属性 注入】

2.7.1 引入已定义的Bean

<!-- ref 引用已定义Bean   -->
<bean id="computerBean27" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<property name="monitor" ref="monitorBean"/>
</bean>


【2.7.2 内部Bean】

<!-- 2.8 内部Bean , 内部bean 无id ,所以不能被获取到或者被其它bean 引用-->
<bean id="computerBean28" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<property name="monitor" >
<bean class="org.zgf.spring.ioc.beanpro.MonitorBean">
<property name="name" value="Lenovo"/>
<property name="price" value="1000"/>
</bean>
</property>
</bean>


【2.7.3 自动装配】

<!-- 2.9 自动注入: autowire: byName 根据变量名称注入 , byType:根据变量类型注入
缺点:如果有多个JavaBean属性, 那么只能指定一种方式,不能为不同的属性指定不同的注入方式
-->
<bean id="computerBean29" class="org.zgf.spring.ioc.beanpro.ComputerBean" autowire="byType"/>


【2.8 属性注入 -- 外部属性文件】

<!-- 3.  Spring 提供了一个PropertyPlaceholderConfigurer的BeanFactory 后置处理器, 这个后置处理器允许用户将Bean 配置的内容外移到属性文件中
location: 指定外部配置文件位置, 使用方式 ${var}  -->
<context:property-placeholder location="classpath:org/zgf/spring/ioc/beanpro/computer.properties"/>
<bean id="computerBean3" class="org.zgf.spring.ioc.beanpro.ComputerBean">
<property name="name" value="${computer.name}"/>
<property name="price" value="${computer.price}"/>
</bean>


属性文件:computer.properties
computer.properties#computer.properties
computer.name=lenovo-Y470
computer.price=1000


【2.9 属性注入--使用P命名空间】

<!-- 4.  P命名空间的使用 :Spring 提供了P命名空间,目的是简化Bean 的属性配置 , 注意需要引入P的命名空间-->
<bean id="computerBean4" class="org.zgf.spring.ioc.beanpro.ComputerBean"  p:name="lenovo-Y470" p:price="1000.0"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: