zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Spring依赖注入

2023-09-27 14:26:26 时间

依赖注入

依赖注入(Dependency Injection,DI)。

  • 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
  • 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .

一、构造器注入

1.使用无参构造创建对象,默认!

<!--无参构造器-->
<bean id="user" class="nuc.ss.pojo.User">
    <property name="name" value="狂神"/>
</bean>

2.假设我们要使用有参构造创建对象。

  1. 下标赋值

    <!--下标赋值-->
    <bean id="user" class="nuc.ss.pojo.User">
        <constructor-arg index="0" value="狂神说Java"/>
    </bean>
    
  2. 类型:不建议使用,同类型只能注入一个

    <bean id="user" class="nuc.ss.pojo.User">
        <constructor-arg type="java.lang.String" value="狂神"/>
    </bean>
    
  3. 参数名:建议使用

    <bean id="user" class="nuc.ss.pojo.User">
        <constructor-arg name="name" value="狂神"/>
    </bean>
    

二、set方式注入【重点】

  1. 普通值注入
    <bean id="student" class="nuc.ss.pojo.Student">
        <property name="name" value="狂神"/>
    </bean>
    
  2. Bean注入 ref
    <bean id="address" class="nuc.ss.pojo.Address">
        <property name="address" value="西安"/>
    </bean>
    
    <bean id="student" class="nuc.ss.pojo.Student">
        <property name="address" ref="address"/>
    </bean>
    
  3. 数组注入
    <bean id="student" class="nuc.ss.pojo.Student">
        <property name="books">
            <array>
                <value>红楼梦</value>
                <value>西游记</value>
                <value>水浒传</value>
                <value>三国演义</value>
            </array>
        </property>
    </bean>
    
  4. list注入
    <bean id="student" class="nuc.ss.pojo.Student">
        <property name="hobbies">
            <list>
                <value>听歌</value>
                <value>敲代码</value>
                <value>看电影</value>
            </list>
        </property>
    </bean>
    
  5. map注入
    <bean id="student" class="nuc.ss.pojo.Student">
        <property name="card">
            <map>
                <entry key="身份证" value="111111111111111111"/>
                <entry key="银行卡" value="22222222222222222222"/>
            </map>
        </property>
    </bean>
    
  6. set注入
    <bean id="student" class="nuc.ss.pojo.Student">
        <property name="games">
            <set>
                <value>LOL</value>
                <value>COC</value>
                <value>BOB</value>
            </set>
        </property>
    </bean>
    
  7. null注入
    <bean id="student" class="nuc.ss.pojo.Student">
        <property name="wife">
            <null/>
        </property>
    </bean>
    
  8. Properties注入
    <bean id="student" class="nuc.ss.pojo.Student">
        <property name="info">
            <props>
                <prop key="driver">com.mysql.jdbc.Driver</prop>
                <prop key="url">localhost://3306</prop>
                <prop key="username">root</prop>
                <prop key="password">admin</prop>
            </props>
        </property>
    </bean>
    

三、拓展方式注入(p命令空间和c命令空间

  1. 引入xml约束
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:c="http://www.springframework.org/schema/c"
    
  2. p命名空间(类似set注入)
    <bean id="user" class="nuc.ss.pojo.User" p:name="狂神" p:age="18"/>
    
  3. c命名空间(类似构造器注入)
    <bean id="user" class="nuc.ss.pojo.User" c:age="18" c:name="狂神2"/>