• Bean的生命周期(五)


    文章目录


    生命周期

    在这里插入图片描述

    一、配置的方式

    1.提供生命周期的控制方法

    package com.study.dao.impl;
    import com.study.dao.BookDao;
    
    public class BookDaoImpl implements BookDao {
        public void save() {
            System.out.println("book dao save...");
        }
    
        private void destroy() {
            System.out.println("destroy...");
        }
    
        private void init() {
            System.out.println("init...");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    2.配置生命周期的控制方法

    <bean id="bookDao" class="com.study.dao.impl.BookDaoImpl" init-method="init" destroy-method="destroy"></bean>
    
    • 1

    二、接口控制

    1.实现InitializingBean, DisposableBean接口

    package com.study.service.impl;
    
    import com.study.dao.BookDao;
    import com.study.dao.impl.BookDaoImpl;
    import com.study.service.BookService;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {
        private BookDao bookDao;
        public void save() {
            System.out.println("BookServiceImpl...");
        }
    
        public void setBookDao(BookDao bookDao) {
            this.bookDao = bookDao;
        }
    
        public void destroy() throws Exception {
            System.out.println("Service Destroy...");
    
        }
    
        public void afterPropertiesSet() throws Exception {
            System.out.println("Service afterPropertiesSet...");
    
        }
    
    
    }
    
    
    • 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
    • 29
    • 30
    • 31

    三、案例:

    1.BookDao接口

    package com.study.dao;
    
    public interface BookDao {
        void save();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2.BookDaoImpl实现类

    package com.study.dao.impl;
    import com.study.dao.BookDao;
    
    public class BookDaoImpl implements BookDao {
        public void save() {
            System.out.println("book dao save...");
        }
    
        private void destroy() {
            System.out.println("destroy...");
        }
    
        private void init() {
            System.out.println("init...");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    3.BookService接口

    package com.study.service;
    
    public interface BookService {
        void save();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    4.BookServiceImpl实现类

    package com.study.service.impl;
    
    import com.study.dao.BookDao;
    import com.study.dao.impl.BookDaoImpl;
    import com.study.service.BookService;
    import org.springframework.beans.factory.DisposableBean;
    import org.springframework.beans.factory.InitializingBean;
    
    public class BookServiceImpl implements BookService, InitializingBean, DisposableBean {
        private BookDao bookDao;
        public void save() {
            System.out.println("BookServiceImpl...");
        }
    
        public void setBookDao(BookDao bookDao) {
            this.bookDao = bookDao;
        }
    
        public void destroy() throws Exception {
            System.out.println("Service Destroy...");
    
        }
    
        public void afterPropertiesSet() throws Exception {
            System.out.println("Service afterPropertiesSet...");
    
        }
    
    
    }
    
    
    • 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
    • 29
    • 30
    • 31

    5.applicationContext.xml配置文件

    <?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    
     <bean id="bookDao" class="com.study.dao.impl.BookDaoImpl" init-method="init" destroy-method="destroy"></bean>
      <!--<bean id="bookDao" class="com.study.dao.impl.BookDaoImpl"></bean>-->
      <bean id="bookService" class="com.study.service.impl.BookServiceImpl">
        <property name="bookDao" ref="bookDao"></property>
      </bean>
    
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    6.Test测试类

    package com.study;
    
    import com.study.dao.BookDao;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            BookDao bean = context.getBean(BookDao.class);
            bean.save();
            context.close();
            //context.registerShutdownHook();
    
        }
    }
    /*
    init...
    book dao save...
    destroy...
     */
    
    
    /*
    init...
    Service afterPropertiesSet...
    book dao save...
    Service Destroy...
    destroy...
     */
    
    • 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
    • 29
    • 30
  • 相关阅读:
    双线性差值应用
    nginx rewrite(重定向)
    软件设计文档示例模板 - 学习/实践
    2023年中国酒类新零售行业发展概况分析:线上线下渠道趋向深度融合[图]
    TensorFlow实战教程(十九)-Keras搭建循环神经网络分类案例及RNN原理详解
    TMGM外汇平台: 纽元未来走势,新西兰即将降息
    记一次 腾讯会议 的意外崩溃分析
    【‘Integer(int)‘ 已经过时了】
    Spring面试题16:Spring框架中的单例bean是线程安全的吗?Spring框架中bean的生命周期?哪些是重要的bean生命周期方法?
    Vue的渲染原理
  • 原文地址:https://blog.csdn.net/qq_43514330/article/details/125414145