Day 84

本文通过Spring的IoC(控制反转)和DI(依赖注入)入门案例,展示了如何管理Service与Dao,配置IoC容器,获取Bean。测试结果显示,BookDaoImpl与BookServiceImpl之间的连接成功。进一步探讨了DI的应用,强调在Service中不使用new操作符创建Dao对象,而是通过IoC容器注入。最后,展示了Spring中Bean的基础配置方法。
摘要由CSDN通过智能技术生成

_Spring技术IoC入门案例

  1. 管理什么?(Service 与 Dao)

  2. 如何将被管理的对象告知IoC容器?(配置)

  3. 被管理的对象交给IoC容器,如何获取到IoC容器?(接口)

  4. IoC容器得到后,如何从容器中获取bean?(接口方法)

  5. 在这里插入图片描述

  6. 两个实现类包的文件java代码为:

    • 在这里插入图片描述

    • 在这里插入图片描述

  7. 测试类文件中的java代码为:

    • package Application;
      
      import dao.impl.BookDaoImpl;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      import org.springframework.context.support.GenericApplicationContext;
      import service.Impl.BookServiceImpl;
      
      public class App_02 {
          public static void main(String[] args) {
              // 加载配置文件
              ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext.xml");
      
              // 获取资源
      //        BookDaoImpl bookDao = (BookDaoImpl) apt.getBean("BookDao");
      //        bookDao.run();
      
              BookServiceImpl bookService = (BookServiceImpl) apt.getBean("BookService");
              bookService.run();
      
          }
      }
      ============================================
      this is BookService
      this is BookDao
      
      Process finished with exit code 0
      

_Spring技术DI案例

  1. 基于IoC管理bean

  2. Service中使用new形式创建的Dao对象是否保留?(否)

  3. Service中需要Dao对象如何进入到Service中?(提供方法)

  4. 在这里插入图片描述

  5. 在这里插入图片描述

  6. 在test文件中的java代码不需要做变化

    • package Application;
      
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      import service.Impl.BookServiceImpl;
      
      public class App_02 {
          public static void main(String[] args) {
              // 加载配置文件
              ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext.xml");
      
              // 获取资源
              BookServiceImpl bookService = (BookServiceImpl) apt.getBean("bookService");
              bookService.run();
          }
      }
      =========================================
      this is BookService
      this is BookDao
      
      Process finished with exit code 0
      
      
    • 能够输出两条信息说明在BookDaoImpl与BookServiceImpl之间建立的连接没有问题

_Spring技术bean基础配置

  1. 在这里插入图片描述

  2. 在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值