spring5 IOC(一)

IOC 容器

  1. IOC

    • 控制反转,把对象的创建和对象的调用,交给Spring 进行管理
    • 使用IOC目的:为了耦合度降低
    • 入门案例就是IOC的使用
  2. IOC

    • xml解析,工厂模式,反射

    • 工厂模式

      耦合度降低最低限度

    • IOC解耦过程

      1. XML 配置过程

        <bean id="user" class="com.atguigu.spring5.User"></bean>

      2. 有service 和dao 类 ,创建工厂类

      class UesrFactory{
          public staitc UserDao getDao() {
              String calssvalue=class属性值; //1.xml 解析
              //通过反射创建对象
      		Class clazz=Class.forName(classvalue);
              return (UserDao)clazz.newInstance();
          }
      }
      
  3. IOC 接口(BeanFactory)

    1. IOC 思想基于IOC容器完成,IOC 容器底层就是对象工厂
    2. Spring 提供IOC 容器实现的两种方式:(两个接口)
      • BeanFactory:IO容器基本实现,是Spring 内部的使用接口,不提供

        加载配置文件时不会创建对象,在获取对象(使用)才去创建对象

      • ApplicationContext:BeanFactory接口的子接口,提供了更多更强大的功能,一般由开发人员进行使用。

        加载配置文件时候就会把配置文件对象进行创建

  4. IOC 操作 Bean 管理(基于xml)

    1. 什么是Bean 管理

      两个操作

      Spring 创建对象

      spring 注入属性

    2. Bean 管理操作有两种方式

      • 基于xml配置文件方式实现

        基于xml 方式创建对象

        
        <!--配置User类对象的创建-->
        <bean id="user" class="com.atguigu.spring5.User"></bean>
        
        

        (1) 在spring配置文件,使用bean 标签,标签里面添加对应属性,就可以实现对象
        (2) 在bean 常用的属性基于xml方式注入属性

        • id 属性 给对象起了个标识 唯一标识
        • class 属性 类全路径(file class )

        ( 3 ) 创建对象时候,默认也是执行无参数构造方法完成对象创建

      • 基于xml 方式注入属性

        • DI : 依赖注入,就是注入属性

          (1) 使用set方法进行注入

          public class Book {
          /*
          * 演示使用set方法进行注入属性*/
          
              private String bname;
              private String bauthor;
          
              public void setBauthor(String bauthor) {
                  this.bauthor = bauthor;
              }
              // Set方法注入
              public void setBname(String bname) {
                  this.bname = bname;
              }
               public void testDemo() {
                  System.out.println(bname+"bname"+bauthor+"bauthor");
          
              }
              
          }
          
          <!--1. set注入属性-->
          <bean id="bool" class="com.atguigu.spring5.Book">
              <!--使用property完成属性注入
              name: 类里面属性名称
              value:向属性注入的值
              -->
              <property name="bname" value="易晶晶"></property>
              <property name="bauthor" value="降龙十八掌"></property>
          </bean>
          

          (2) 使用有参数的构造进行注入

          /*
          * 使用有参数构造注入
          * */
          public class Orders {
          
              private String oname;
              private String address;
          // 有参构造
              public Orders(String oname, String address) {
                  this.oname = oname;
                  this.address = address;
              }
          
              public void  ordersTest() {
                  System.out.println("oname = " +oname);
                  System.out.println("address = " + address);
              }
          }
          
      <bean id="orders" class="com.atguigu.spring5.Orders">
          <constructor-arg name="oname" value="computer"></constructor-arg>
          <constructor-arg name="address" value="china"></constructor-arg>
      </bean>
      ```
      
      ```java
          @Test
      public  void  testOrders() {
              // 1.加载Spring 配置文件
              ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
              //获取配置创建的对象
              Orders orders = context.getBean("orders", Orders.class);
              System.out.println("orders = " + orders);
              orders.ordersTest();
      
          }
      ```
      
  5. 3IOC 操作Bean 管理(基于注解)

    • p名称空间注入 可以简化基于xml配置方式

      1. 添加p名称空间在配置文件中

        <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"
               xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        
      <!--p方式-->
          <bean id="book" class="com.atguigu.spring5.Book" p:bname="九阳神功" p:bauthor="无名氏">
      
    • xml 注入其他类型属性

    1.字面量

    (1) null 值

        <bean id="book" class="com.atguigu.spring5.Book">
    <!--        使用property完成属性注入
            name: 类里面属性名称
            value:向属性注入的值-->
            <property name="bname" value="易晶晶"></property>
            <property name="bauthor" value="降龙十八掌"></property>
            <!--null值-->
            
            <property name="address">
                <null></null>
            </property>
        </bean>
    

    (2) 属性值特殊符号

        <bean id="book" class="com.atguigu.spring5.Book">
    <!--        使用property完成属性注入
            name: 类里面属性名称
            value:向属性注入的值-->
            <property name="bname" value="易晶晶"></property>
            <property name="bauthor" value="降龙十八掌"></property>
            <!--null值-->
        <!--    <property name="address">
                <null></null>
            </property>
    -->
            <!--属性值包含特殊符号
             1.把<> 进行转义
             2.把带特殊符号内容写到CDATA
    
            -->
            <property name="address">
                <value ><![CDATA[<<南京>>]]></value>
                </property>
        </bean>
    

    (3) 注入属性-外部bean

    • 创建两个类service类和dao类

    • 在service 调用dao里面的方法

      <!--1 service 和bao 对象的创建-->
      <bean id="userService" class="com.atguigu.spring5.service.UserService">
          <!--注入userDao对象
          name 属性值:类里面属性名称
          ref属性:创建userDao对象bean 标签id值
          -->
          <property name="userDao" ref="userDaoImpl"></property>
      </bean>
      <bean id="userDaoImpl" class="com.atguigu.spring5.dao.UserDaoimpl"></bean>
      

    (4) 注入属性-内部bean和级联赋值

    • 一对多关系;
      一个部门有多个员,一个员工属于一个部门

      部门是一 员工是多

      在实体类之间表示一对多关系

      /**
       *
       * 部门类
       */
      public class Dapt {
      
          private String dname;
      
          public void setDname(String dname) {
      
              this.dname = dname;
          }
      
      
      


    ​ }
    java
    /**
    * 员工类
    */
    public class Emp {
    private String ename;
    private String gender;


    ​ //员工属于某一个部门,使用对象形式存在
    ​ private Dept dept;


    ​ public void setDept(Dept dept) {
    ​ this.dept = dept;
    ​ }

    public void setEname(String ename) {
    this.ename = ename;
    }

        public void setGender(String gender) {
            this.gender = gender;
        }
    


    ​ }
    ​ ```

    • 在Spirng配置文件中进行配置

      <!--内部bean-->
      <bean id="emp" class="com.atguigu.spring5.bean.Emp">
          <!--设置两个普通属性-->
          <property name="ename" value="lucy"></property>
          <property name="gender" value=""></property>
          
          <!--设置对象类型属性-->
          <property name="dept" >
              <bean id="dept" class="com.atguigu.spring5.bean.Dept">
                  <property name="dname" value="安保部"></property>
              </bean>
          </property>
      </bean>
      

    ( 5 ) 注入属性-级联赋值

    <!--级联赋值-->
    <bean id="emp" class="com.atguigu.spring5.bean.Emp">
        <!--设置两个普通属性-->
        <property name="ename" value="lucy"></property>
        <property name="gender" value=""></property>
    
        <!--级联赋值-->
    
        <property name="dept" ref="dept"></property>
        
    
    </bean>
    <bean id="dept" class="com.atguigu.spring5.bean.Dept">
        <property name="dname" value="财务部"></property>
    </bean>
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值