Spring 通过XML创建对象且注入属性 纯代码,慢慢品

纯代码,慢慢品
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/util
       https://www.springframework.org/schema/util/spring-util.xsd">

    <!--创建一个简单的bean 通过setter方法注入-->
    <bean id="user" class="com.duanhw.pojo.User">
        <property name="name" value="duan hong wei"></property>
        <property name="phone" value="17794286634"></property>
        <property name="email" value="704523046@qq.com"></property>
        <property name="age" value="34"></property>
        <property name="Idnumber" value="622301198611200818"></property>
    </bean>
   <!--通过带参构造方法DI 依赖注入 -->
   <bean id="book" class="com.duanhw.pojo.Book">
        <constructor-arg name="bookName" value="水浒传"></constructor-arg>
        <constructor-arg name="author" value="施耐庵"></constructor-arg>
        <constructor-arg name="publisher" value="西部出版社"></constructor-arg>
       <constructor-arg name="version">
           <null/>
       </constructor-arg>
   </bean>
    <!--通过带参构造方法DI 依赖注入 -->
    <bean id="book2" class="com.duanhw.pojo.Book2">
        <property name="name">
            <value><![CDATA[《水浒传》]]></value>
        </property>
        <property name="author" value="施耐庵"></property>
        <property name="publisher" value="西部出版社"></property>
    </bean>

    <!--外部bean-->
    <bean id="order" class="com.duanhw.pojo.Order">
        <property name="orderNo" value="001"></property>
        <property name="product" ref="productImpl"></property>
    </bean>

    <bean id="productImpl" class="com.duanhw.pojo.Product">
        <property name="name" value="玩具"></property>
        <property name="maker" value="浙江义乌小宝玩具厂"></property>
    </bean>

    <!--内部bean -->
    <bean id="employee" class="com.duanhw.pojo.Employee">
        <property name="name" value="丘处机"></property>
        <property name="gender" value="male"></property>
        <property name="dept">
            <bean id="deptment" class="com.duanhw.pojo.Department">
                <constructor-arg name="depName" value="销售部"></constructor-arg>
            </bean>
        </property>
    </bean>
   <!--级联赋值
       注意: 1.要求Car 类中 wheel 加入setter getter方法
       2. 先用ref引用外部bean 然后再次对property 对象进行赋值
   -->
    <bean id="car" class="com.duanhw.pojo.Car">
        <property name="brand" value="玛莎拉蒂"></property>
        <property name="driver" value="4驱"></property>
        <property name="wheel" ref="wheel"></property>
        <property name="wheel.name" value="米其林"></property>
    </bean>
    <bean id="wheel" class="com.duanhw.pojo.Wheel"></bean>
    <!--集合属性注入-->
    <bean id="mylib" class="com.duanhw.pojo.Libr">
        <property name="addr" ref="address"></property>
        <!--数组类型-->
        <property name="coureses">
            <array>
                <value>语文</value>
                <value>数学</value>
                <value>化学</value>
            </array>
        </property>
        <!--list类型-->
        <property name="specialty">
            <list>
                <value>文学</value>
                <value>计算机</value>
                <value>物理</value>
            </list>
        </property>
        <!--map类型-->
        <property name="technology">
            <map>
                <entry key="JAVA" value="SPRING"></entry>
                <entry key="NET" value="WINFORM"></entry>
            </map>
        </property>
        <!--set类型-->
        <property name="database">
            <set>
                <value>MySQL</value>
                <value>Oracle</value>
            </set>
        </property>
        <property name="students">
            <list>
                <ref bean="stu1"></ref>
                <ref bean="stu2"></ref>
            </list>
        </property>
    </bean>
    <bean id="stu1" class="com.duanhw.pojo.Student">
        <property name="name" value="李靖"></property>
        <property name="number" value="001"></property>
    </bean>
    <bean id="stu2" class="com.duanhw.pojo.Student">
        <property name="name" value="哪吒"></property>
        <property name="number" value="002"></property>
    </bean>
    <util:set id="address">
        <value>兰州</value>
        <value>天水</value>
        <value>白银</value>
        <value>酒泉</value>
    </util:set>
</beans>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值