DI依赖注入

一、构造器注入

1)下标赋值

<bean id="user" class="com.along.pojo.User">
   <constructor-arg index="0" value="along"/>
</bean>

2)类型(不推荐)

<bean id="user" class="com.along.pojo.User">
    <constructor-arg type="java.lang.String" value="along"/>
</bean>

3)参数名

<bean id="user" class="com.along.pojo.User">
    <constructor-arg name="name" value="along"/>
</bean>

二、Set方式注入(重点)

依赖注入:set注入。(依赖:bean对象的创建依赖于容器。注入:bean对象中的所有属性,由容器来注入。)Set方式注入实质上就是在容器创建对象时使用实体类中属性的set方法给每个属性赋值。

创建一个学生实体类

public class Student {
    private String name;
    private Address address;
    private String[] books;
    private List<String> hobbys;
    private Map<String,String> card;
    private Set<String> games;
    private String wife;
    private Properties info;
}

xml文件中注入

    <bean id="student" class="com.along.pojo.Student">
            <!--第一种,普通值注入,value-->
            <property name="name" value="阿龙"/>
            <!--第二种,Bean注入,ref-->
            <property name="address" ref="address"/>
            <!--数组注入-->
            <property name="books">
                <array>
                    <value>红楼梦</value>
                    <value>三国演义</value>
                    <value>西游记</value>
                    <value>水浒传</value>
                </array>
            </property>
            <!--List注入-->
            <property name="hobbys">
                <list>
                    <value></value>
                    <value></value>
                    <value>rap</value>
                    <value>篮球</value>
                </list>
            </property>
            <!--Map注入-->
            <property name="card">
                <map>
                    <entry key="身份证" value="121212100001010615"/>
                    <entry key="银行卡" value="45454545454545"/>
                </map>
            </property>
            <!--Set注入-->
            <property name="games">
                <set>
                    <value>LOL</value>
                    <value>COC</value>
                </set>
            </property>
            <!--NULL-->
            <property name="wife">
                <null/>
            </property>
            <!--Properties-->
            <property name="info">
                <props>
                    <prop key="id">1231544</prop>
                    <prop key="username">小龙</prop>
                    <prop key="userpassword">456321</prop>
                </props>
            </property>
        </bean>

三、c命名和p命名空间注入(使用的时候需要在头文件中加入相关链接)

<?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="user1" class="com.along.pojo.User" p:name="阿龙" p:age="18" scope="prototype"/>
        <bean id="user2" class="com.along.pojo.User" c:name="小龙" c:age="20"/>
</beans>

 

posted @ 2023-09-06 11:11  数星观月  阅读(5)  评论(0编辑  收藏  举报