ui设计师mike个人网站/南京seo公司教程
1、概念
为bean中的属性注入值,成为数据的装配,可以装配不同类型的值,如int、Map、Boolean等。
2、简单类型(共19种)
八种基本类型及包装类byte short int long double float char booleanByte Short Integer Long Double Float Character BooleanString Class Resource
简单类型装配值 -- 使用value属性装配
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="springBean" class="ioc04.SpringBean"><property name="age" value="13"/><property name="bt" value="22"/><property name="price" value="12.2"/><property name="username" value="tom"/><property name="clazz" value="java.lang.String"/></bean>
</beans>
3、集合类型
数组 List Set Map Properties
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean name="otherBean" class="ioc05.OtherBean"><property name="name" value="tom"/></bean><bean name="springBean" class="ioc05.SpringBean"><!-- 引用IOC容器中另外一个bean 使用ref属性 --><property name="otherBean" ref="otherBean"/><property name="arrays"><!--Array使用array装配--><array><value>1</value><value>2</value><value>4</value></array><!--Array也可以使用List装配--><!--<List><value>1</value><value>2</value><value>4</value></List>--></property><property name="lists"><list><!-- 由于List中放的是OtherBean,所以这里不可以用value属性赋值 因改用ref --><!--使用IOC中已经存在的OtherBean--><ref bean="otherBean"/><!--List中元素可重复--><ref bean="otherBean"/><!-- 重新创建一个新的OtherBean--><bean class="ioc05.OtherBean"><property name="name" value="lokus"/></bean></list></property><property name="sets"><set><!--Set中元素不可重复 如下两个ref引用IOC容器的同一个实例,因此在Set中只会有一个值--><ref bean="otherBean"/><ref bean="otherBean"/><!-- 重新创建一个新的OtherBean--><bean class="ioc05.OtherBean"><property name="name" value="zhangsan"/></bean></set></property><property name="map" ><map><entry key-ref="otherBean" value="java.lang.Integer"/></map></property><property name="properties"><props><!--properties中都是字符串--><prop key="key1">value1</prop></props></property></bean>
</beans>
map集合在装配时有简单类型、引用类型两种方法
4、其他bean的引用
IOC容器要注入的值,是另外一个bean的值 ---->使用ref
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean name="otherBean" class="ioc05.OtherBean"><property name="name" value="tom"/></bean><bean name="springBean" class="ioc05.SpringBean"><!-- 引用IOC容器中另外一个bean 使用ref属性 --><property name="otherBean" ref="otherBean"/></bean>
</beans>
5、NULL类型
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="springBean" class="ioc06.SpringBean"><!--将name属性装配成null--><property name="name"><null/></property></bean>
</beans>