通过spring构造实例对象的时候,对象中的属性赋值的方式有如下几种:
一、通过setter方法注入:类中一定要提供相应的setXX方法,才能执行成功
属性注入即通过 setter 方法注入Bean 的属性值或依赖的对象
属性注入使用 <property> 元素, 使用 name 属性指定 Bean 的属性名称,value 属性或 <value> 子节点指定属性值
属性注入是实际应用中最常用的注入方式
<!-- 通过setter注入 -->
<beanid="userDao1"class="cn.itcast.h_di_simple.UserDaoImpl">
<propertyname="jdbcUrl"value="jdbc:mysql:///test"></property>
<propertyname="driverClass"value="com.mysql.jdbc.Driver"></property>
<propertyname="username"value="root"></property>
<propertyname="password"value="root"></property>
</bean>
二、通过构造器注入:类中要提供带参数的构造方法,并且下面参数是有顺序的
通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。
构造器注入在 <constructor-arg> 元素里声明属性, <constructor-arg> 中没有 name 属性
<!-- 通过构造方法注入,会自动按顺序与参数匹配 -->
<beanid="userDao2"class="cn.itcast.h_di_simple.UserDaoImpl">
<constructor-argvalue="jdbc:mysql:///test22"></constructor-arg>
<constructor-argvalue="com.mysql.jdbc.Driver22"></constructor-arg>
<constructor-argvalue="root_name22"></constructor-arg>
<constructor-argvalue="root_password22"></constructor-arg>
</bean>
或是:
<!-- 通过构造方法注入,会与指定的索引匹配,第1个索引为0 -->
<beanid="userDao3"class="cn.itcast.h_di_simple.UserDaoImpl">
<constructor-argindex="0"value="jdbc:mysql:///test22"></constructor-arg>
<constructor-argindex="1"value="com.mysql.jdbc.Driver22"></constructor-arg>
<constructor-argindex="2"value="root_name22"></constructor-arg>
<constructor-argindex="3"value="root_password22"></constructor-arg>
</bean>
或是:
<!-- 若一个 bean 有多个构造器, 如何通过构造器来为 bean 的属性赋值 -->
<!-- 可以根据 index 和 value 进行更加精确的定位. (了解) -->
<bean id="car" class="com.atguigu.spring.helloworld.Car">
<constructor-arg value="KUGA" index="1"></constructor-arg>
<constructor-arg value="ChangAnFord" index="0"></constructor-arg>
<constructor-arg value="250000" type="float"></constructor-arg>
</bean>
<bean id="car2" class="com.atguigu.spring.helloworld.Car">
<constructor-arg value="ChangAnMazda"></constructor-arg>
<!-- 若字面值中包含特殊字符, 则可以使用 DCDATA 来进行赋值. (了解) -->
<constructor-arg>
<value><![CDATA[<ATARZA>]]></value>
</constructor-arg>
<constructor-arg value="180" type="int"></constructor-arg>
</bean>
注入普通属性
<beanid="user"class="cn.itcast.j_di_simple.User">
<propertyname="name"value="张三"></property>
<propertyname="age"value="18"></property>
<propertyname="gender"value="true"></property>
</bean>
注入null
<!-- 注入null值 -->
<propertyname="comment">
<null/>
</property>
注入依赖对象第一种:注入有名对象
<!-- 定义一个bean -->
<beanid="userDao"class="cn.itcast.j_di_bean.UserDaoImpl"/>
<!-- 使用ref属性指定要注入的bean(id或是name属性) -->
<beanid="userService"class="cn.itcast.j_di_bean.UserServiceImpl">
<propertyname="userDao"ref="userDao"></property>
</bean>
第二种:注入无名对象
<!--在property中直接定义一个bean,不需要指定id或name,指定了也无效 -->
<beanid="userService2"class="cn.itcast.j_di_bean.UserServiceImpl">
<propertyname="userDao">
<beanid="userDao2"class="cn.itcast.j_di_bean.UserDaoImpl">
<property name=”dataSource”value=”dataSource”></property>
</bean>
</property>
</bean>
注入集合属性
在 Spring中可以通过一组内置的 xml 标签(例如: <list>, <set> 或 <map>) 来配置集合属性.
配置 java.util.List 类型的属性, 需要指定 <list> 标签, 在标签里包含一些元素. 这些标签可以通过 <value> 指定简单的常量值, 通过 <ref> 指定对其他 Bean 的引用. 通过<bean> 指定内置 Bean 定义. 通过 <null/> 指定空元素. 甚至可以内嵌其他集合.
数组的定义和 List 一样, 都使用 <list>
配置 java.util.Set 需要使用 <set> 标签, 定义元素的方法与 List 一样.
Java.util.Map 通过 <map> 标签定义, <map> 标签里可以使用多个 <entry> 作为子标签. 每个条目包含一个键和一个值.
必须在 <key> 标签里定义键
因为键和值的类型没有限制, 所以可以自由地为它们指定 <value>, <ref>, <bean> 或 <null> 元素.
可以将 Map 的键和值作为 <entry> 的属性定义: 简单常量使用 key 和 value 来定义; Bean 引用通过 key-ref 和 value-ref 属性定义
使用 <props> 定义 java.util.Properties, 该标签使用多个 <prop> 作为子标签. 每个 <prop> 标签必须定义 key 属性.
// 集合属性要定义为接口类型
//集合类型可以不初始化,因为会注入一个新的集合对象
//集合最好指定强类型,以便Spring转型,如果未指定,就会是String型的值
//如果没有指定强类型,也可以在配置时指定:
// <valuetype="java.lang.Integer">10</value>
private Set<String>addressSet =new HashSet<String>();
private Set<Integer>numberSet;
private List<String>addressList;
private String[]addressArray;
private Map<String, String>addressCodeMap;
private PropertiesuserProperties;
<beanid="user"class="cn.itcast.k_di_collection.User">
<propertyname="name"value="张三"></property>
<!-- Set类型 -->
<propertyname="addressSet">
<set>
<value>上地东路27号</value>
<value>上地东路28号</value>
</set>
</property>
<!-- Set类型 -->
<propertyname="numberSet">
<set>
<valuetype="java.lang.Integer">1</value>
<valuetype="java.lang.Integer">2</value>
</set>
</property>
<!-- List类型,数组类型与List配置完全一样(具体代码略) -->
<propertyname="addressList">
<list>
<value>上地东路27号</value>
<value>上地东路28号</value>
</list>
</property>
<!-- Map类型,要指定key-value对 -->
<propertyname="addressCodeMap">
<map>
<entrykey="上地东路27号"value="100085"></entry>
<entrykey="软件园9号楼"value="100086"></entry>
</map>
</property>
<!-- Properties类型,只能是String型的值 -->
<propertyname="userProperties">
<props>
<propkey="firstName">hello</prop>
<propkey="lastName">world</prop>
</props>
</property>
</bean>
使用基本的集合标签定义集合时, 不能将集合作为独立的 Bean 定义, 导致其他 Bean 无法引用该集合, 所以无法在不同 Bean 之间共享集合.
可以使用 util schema 里的集合标签定义独立的集合 Bean. 需要注意的是, 必须在 <beans> 根元素里添加 util schema 定义。
xmlns:util="http://www.springframework.org/schema/util"
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"
<!-- 声明集合类型的 bean -->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>
<bean id="user2" class="com.atguigu.spring.helloworld.User">
<property name="userName" value="Rose"></property>
<!-- 引用外部声明的 list -->
<property name="cars" ref="cars"></property>
</bean>
为了简化 XML 文件的配置,越来越多的 XML 文件采用属性而非子元素配置信息。
Spring 从 2.5 版本开始引入了一个新的 p 命名空间,可以通过 <bean> 元素属性的方式配置 Bean 的属性。
使用 p 命名空间后,基于 XML 的配置方式将进一步简化。
xmlns:p="http://www.springframework.org/schema/p"
<bean id="user3" class="com.atguigu.spring.helloworld.User"
p:cars-ref="cars" p:userName="Titannic"></bean>