Spring——Spring_XML实现DI

1.实现DI1

1.1DI给对象属性赋值

【1】通过set方法给对象属性赋值

<bean id="user2" class="org.example.User" >
        <property name="userId" value="1"></property>
        <property name="uname" value="漳卅"></property>
        <property name="password" value="abcdef"></property>
    </bean>

【2】通过有参构造方法给对象属性赋值

<bean id="user3" class="org.example.User" >
        <constructor-arg name="userId" value="1" ></constructor-arg>
        <constructor-arg name="uname" value="宿舍" ></constructor-arg>
        <constructor-arg name="password" value="asdads" ></constructor-arg>
    </bean>

【3】通过p名称空间和c名称空间实现DI

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">


<bean id="user4" class="org.example.User"p:userId="5" p:uname="晓东" p:password="123456" ></bean>
<bean id="user6" class="org.example.User"c:userId="6" c:uname="1东" c:password="123456" ></bean>

2.实现DI2

2.1注入空值和特殊符号

3.实现DI3

3.1对象引用的方式

分为外部引用和内部引用,外部引用使用的多一些

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="date1" class="java.util.Date"></bean>
    <bean id="mouse" class="org.example.Mouse" >
        <property name="name" value="杰瑞"></property>
        <!--引用外部bean-->
        <property name="brith" ref="date1"></property>
    </bean>


    <bean id="cat" class="org.example.Cat">
    <property name="name" value="汤姆"></property>
        <!--引用内部bean-->
    <property name="mouse1">
        <bean class="org.example.Mouse">
            <property name="name" value="杰瑞"></property>
            <property name="brith" >
                <bean class="java.util.Date"></bean>
            </property>
        </bean>
    </property>
    </bean>
</beans>

4.实现DI4

4.1集合属性注入

<?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:p="http://www.springframework.org/schema/p"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="student" class="org.example.Student">
        <!--数组属性注入-->
        <property name="books">
            <array>
                <value>JAVA</value>
                <value>Mysql</value>
                <value>Spring</value>
            </array>
        </property>
        <!--set集合注入-->
        <property name="bookset">
            <set>
                <value>JAVA</value>
                <value>Mysql</value>
                <value>Spring</value>
            </set>
        </property>
        <!--list集合注入-->
        <property name="bookList">
            <list>
                <value>JAVA</value>
                <value>Mysql</value>
                <value>Spring</value>
            </list>
        </property>
        <!--map集合注入-->
        <property name="bookmap">
            <map>
                <entry key="JAVA" value="马士兵"></entry>
                <entry key="Golang" value="马士兵"></entry>
                <entry key="JVM" value="马士兵"></entry>
            </map>
        </property>
        <!--list对象集合注入-->
        <property name="bookList2">
            <list>
                <bean id="b1" class="org.example.Book">
                    <property name="bname"  value="JAVA"></property>
                    <property name="author" value="马士兵"></property>
                </bean>
                <bean id="b2" class="org.example.Book">
                    <property name="bname"  value="Go"></property>
                    <property name="author" value="马士兵"></property>
                </bean>
                <bean id="b3" class="org.example.Book">
                    <property name="bname"  value="JVM"></property>
                    <property name="author" value="马士兵"></property>
                </bean>
                <!--<ref bean="b1"></ref>
                <ref bean="b2"></ref>
                <ref bean="b3"></ref>-->
            </list>
        </property>
    </bean>

    <!--<bean id="b1" class="org.example.Book">
        <property name="bname" value="wang"></property>
        <property name="author" value="xue"></property>
    </bean>
    <bean id="b2" class="org.example.Book">
        <property name="bname" value="hu"></property>
        <property name="author" value="guo"></property>
    </bean>-->
</beans>

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值