• Day 85


    _Spring技术bean实例化构造方法

    1. bean本质上就是对象:创建bean使用构造方法完成

    2. 在这里插入图片描述

    3. 在xml文件中配置bean在这里插入图片描述

    4. 最后在test文件中编写java程序实现BookDaoImpl的调用执行

      • package AppTest;
        
        public class App_01 {
            public static void main(String[] args) {
                // 加载配置文件
                ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext.xml");
        
                BookDao bookDao = (BookDao) apt.getBean("bookDao");
                bookDao.run();
        
            }
        }
        ====================================
        实现类已运行
        this is BookDao ...
        
        Process finished with exit code 0
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17

    _Spring技术FactoryBean的实例化

    1. 在这里插入图片描述

    2. 在applicationContext.xml文件中配置bean在这里插入图片描述

    3. 在test文件中编写java程序实现通过FactoryBean的形式创建实例化对象

      • package AppTest;
        
        import com.Alvis.dao.BookDao;
        import org.springframework.context.ApplicationContext;
        
        public class App_01 {
            public static void main(String[] args) {
                // 加载配置文件
                ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext.xml");
        
                BookDao bookDao = (BookDao) apt.getBean("bookDao");
                bookDao.run();
        
                System.out.println("-----------------------------");
        
                BookDao bookDao_factory = (BookDao) apt.getBean("bookDao_Factory");
                bookDao_factory.run();
        
            }
        }
        =================================
        实现类已运行
        this is BookDao ...
        -----------------------------
        实现类已运行
        this is BookDao ...
        
        Process finished with exit code 0
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
      • 在这里插入图片描述

    _Spring技术Bean的生命周期

    1. 生命周期:从创建到消亡的完整过程

    2. 在applicationContext.xml中的bean里面配置init参数和destory参数在这里插入图片描述

    3. 在test文件中编写java程序实现对PaperDaoImpl实现类的调用

      • 此为PaperDaoImpl实现类:在这里插入图片描述

      • 此为test文件中的java主程序:

      • package AppTest;
        
        import com.Alvis.dao.PaperDao;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        
        public class App_02 {
            public static void main(String[] args) {
                // 加载配置文件
                ClassPathXmlApplicationContext cpx = new ClassPathXmlApplicationContext("applicationContext.xml");
                cpx.registerShutdownHook();
        
                // 此时为ClassPathXmlApplicationContext的调用执行
                PaperDao paperDao = (PaperDao) cpx.getBean("paperDao");
                paperDao.run();
            }
        }
        =======================================
        实现类已运行
        init 已执行
        this is PaperDao...
        destory 已执行
        
        Process finished with exit code 0
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
      • 在这里插入图片描述

    4. bean的生命周期

      • 初始化容器
        • 创建对象(内存分配)
        • 执行构造方法
        • 执行属性注入(set操作)
        • 执行bean初始化方法
      • 使用bean
        • 执行业务操作
      • 关闭、销毁容器
        • 执行bean的销毁方法
  • 相关阅读:
    Android子线程可以更新UI
    LogBack的日志报错解决办法 org.xml.sax.SAXNotRecognizedException
    编写HTTP协议代理的一些知识(源码)
    健康先行微信小程序设计与实现
    MongoDB集群之复制集,分片
    Android Handler/Looper视角看UI线程的原理
    ros2 代码风格检查
    Windows安装docker踩坑
    R语言安装及基础知识
    克隆虚拟机 - hyperv
  • 原文地址:https://blog.csdn.net/ALVIS_108/article/details/126192730