<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd "> <!--配置bean id:给配置的类起个后续在容器中获取用的id class:类所在的路径,通过反射的方式在IOC容器中创建bean,要求bean必须有无参构造方法 --> <!--1、属性注入,通过setter方法注入,开发中最常用的注入方式--> <bean id="user" class="com.spring.cn.config.collections.UserBean"> <property name="username" value="liangd"/> <property name="age" value="23"/> </bean> <!--2、通过构造器方法注入--> <bean id="product5" class="com.spring.cn.config.collections.ProductBean"> <!-- <constructor-arg value="36" index="2"/> <constructor-arg value="南极人" index="0"/> <constructor-arg value="保暖" index="1"/>--> <constructor-arg value="365" type="double"/> <constructor-arg value="保暖衣" type="java.lang.String"/> <constructor-arg value="保暖" type="java.lang.String"/> </bean> <!--如果IOC容器中有两个一样的bean,在获取bean时不能用Class来获取--> <!--不加参数根据构造器参数的顺序来赋值 1、value 赋值 2、index 参数下标 3、type 类型 4、name 名称 5、ref 引用类型可以用ref,例如UserService,基本数据类型可以用value赋值,例如int、Long等 使用构造器的注入的属性值可以指定参数的位置和参数的类型,来区分重载的构造器 --> <bean id="product6" class="com.spring.cn.config.collections.ProductBean"> <constructor-arg value="秋裤" type="java.lang.String"/> <constructor-arg value="保暖" type="java.lang.String"/> <constructor-arg value="36" type="int"/> </bean> <!--value属性可以拿出来,如果有特殊字符,可以用<![CDATA[]]>处理--> <bean id="product7" class="com.spring.cn.config.collections.ProductBean"> <constructor-arg type="java.lang.String"> <value><![CDATA[<南极人…^>]]></value> </constructor-arg> <constructor-arg value="保暖" type="java.lang.String"/> <constructor-arg value="36" type="int"/> </bean> <!--集合测试--> <!--List测试--> <bean id="companyList" class="com.spring.cn.config.collections.CompanyListBean"> <property name="name" value="阿里巴巴"/> <property name="address" value="四川成都"/> <property name="productBeanList"> <!--使用list节点为list属性类型赋值, List、Set和数组赋值方式差不多--> <list> <ref bean="product5"/> <ref bean="product6"/> <!--也可以用内部bean--> <bean class="com.spring.cn.config.collections.ProductBean"> <constructor-arg type="java.lang.String"> <value><![CDATA[<南极人…^>]]></value> </constructor-arg> <constructor-arg value="保暖" type="java.lang.String"/> <constructor-arg value="36" type="int"/> </bean> </list> <!--<set>--> <!--<ref config="product5"/>--> <!--</set>--> <!--<array>--> <!--<ref config="product5"/>--> <!--</array>--> </property> </bean> <!--配置Map属性值--> <bean id="companyMap" class="com.spring.cn.config.collections.CompanyMapBean"> <property name="name" value="阿里巴巴"/> <property name="address" value="四川成都"/> <property name="productBeanMap"> <!--用map节点以及entry子节点 对Map类型的成员变量赋值--> <map> <entry key="AA" value-ref="product5"/> <entry key="BB" value-ref="product6"/> </map> </property> </bean> <!--通过Properties配置属性--> <bean id="dataSource" class="com.spring.cn.config.collections.DataSource"> <property name="properties"> <props> <prop key="username">root</prop> <prop key="password">123456</prop> <prop key="url">jdbc:mysql//localhost:3306?</prop> <prop key="driver">com.mysql.cj.jdbc.driver</prop> </props> </property> </bean> <!--将集合独立出来,以便其它bean调用--> <util:list id="productUtil"> <ref bean="product6"/> <ref bean="product7"/> </util:list> <!--测试引用工类类赋值--> <bean id="companyUtil" class="com.spring.cn.config.collections.CompanyListBean"> <property name="name" value="阿里巴巴"/> <property name="address" value="四川成都"/> <property name="productBeanList" ref="productUtil"/> </bean> <!--测试通过P标签赋值,需要先导包,相比于传统的配置要简单一些--> <bean id="companyP" class="com.spring.cn.config.collections.CompanyListBean" p:name="腾讯" p:address="深圳" p:productBeanList-ref="productUtil"> </bean> </beans>