IOC

三:

  一:控制反转

  二:将对象的创建,管理,配置工作交给spring

  三:修改配置文件达到修改功能的目的;不在需要修改程序。

依赖注入方式:

  一:构造器注入

    一:下标赋值

  <bean id="wangwu" class="com.nbg.pojo.User">
       <!--下标赋值 value:给构造参数赋值--> 
    <constructor-arg index="0" value="3"/>
    <constructor-arg index="1" value="doctor"/>
  </bean>

    二:类型

    <bean id="zhaoliu" class="com.nbg.pojo.User">
        <!--类型-->
        <constructor-arg type="int" value="4"/>
        <constructor-arg type="java.lang.String" value="criminal"/>
    </bean>

    三:参数名

    <bean id="lisi" class="com.nbg.pojo.User" name="lsi">
        <!--name:构造参数名称-->
        <constructor-arg name="id" value="2"/>
        <constructor-arg name="name" value="teacher"/>
    </bean>

  二:set方式注入

@Data
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;
}

@Data
public class Address {
    private String address;
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="address" class="com.nbg.pojo.Address">
        <property name="address" value="北京"/>
    </bean>
    <bean id="zhangsan" class="com.nbg.pojo.Student">
        <!--普通注入-->
        <property name="name" value="老张"/>
        <property name="address" ref="address"/>
        <!--数组-->
        <property name="books">
            <array>
                <value>三国演义</value>
                <value>红楼梦</value>
                <value>水浒传</value>
                <value>西游记</value>
            </array>
        </property>
        <!--map-->
        <property name="card">
            <map>
                <entry key="id" value="1"/>
                <entry key="name" value="张三"/>
                <entry key="count" value="99"/>
            </map>
        </property>
        <!--set-->
        <property name="games">
            <set>
                <value>lol</value>
                <value>bob</value>
                <value>coc</value>
            </set>
        </property>
        <!--list-->
        <property name="hobbys">
            <list>
                <value>打游戏</value>
                <value>学习</value>
            </list>
        </property>
        <!--properties-->
        <property name="info">
            <props>
                <prop key="driver">Driver</prop>
                <prop key="url">3306/mybatis</prop>
                <prop key="username">root</prop>
                <prop key="password">root</prop>
            </props>
        </property>
        <!--null-->
        <property name="wife">
            <null/>
        </property>
    </bean>

  三:拓展方式注入

<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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="u" class="com.nbg.pojo.User" p:age="18" p:name="普京"/>
    <bean id="us" class="com.nbg.pojo.User" c:_0="20" c:_1="梅德韦杰夫"/>
</beans>

   p:properties   属性命名

   c:contstroctor   构造器命名

  

posted @ 2020-12-08 16:16  nbg  阅读(37)  评论(0编辑  收藏  举报