Spring-Bean的生命周期

简介: Spring-Bean的生命周期

✨生命周期

  • 生命周期︰从创建到消亡的完整过程
  • bean生命周期: bean从创建到销毁的整体过程
  • bean生命周期控制:在bean创建后到销毁前做一些事情

✨配置的方式

🔥提供生命周期的控制方法

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...");
    }
}

🔥配置生命周期的控制方法

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

✨接口控制

🔥实现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...");

    }


}

✨案例:

🔥BookDao接口

package com.study.dao;

public interface BookDao {
    void save();
}

🔥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...");
    }
}

🔥BookService接口

package com.study.service;

public interface BookService {
    void save();
}

🔥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...");

    }


}

🔥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>

🔥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...
 */
相关文章
|
26天前
|
Java 开发者 Spring
解析Spring中Bean的生命周期
解析Spring中Bean的生命周期
20 2
|
2月前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
54 1
|
2月前
|
XML Java 数据格式
Spring 应用上下文探秘:生命周期解析与最佳实践
Spring 应用上下文探秘:生命周期解析与最佳实践
83 0
|
17天前
|
XML druid Java
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
Spring5系列学习文章分享---第二篇(IOC的bean管理factory+Bean作用域与生命周期+自动装配+基于注解管理+外部属性管理之druid)
25 0
|
26天前
|
Java 开发者 Spring
Spring 中 Bean 的生命周期
Spring 中 Bean 的生命周期
15 2
|
2月前
|
Java Spring 容器
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
37 1
Spring注解开发,bean的作用范围及生命周期、Spring注解开发依赖注入
|
18天前
|
Java Spring
|
19天前
|
缓存 算法 Java
spring-三级缓存-生命周期-spring事务-IOC-AOP
spring-三级缓存-生命周期-spring事务-IOC-AOP
|
19天前
|
Java 开发者 Spring
Spring的Bean的生命周期各个阶段扩展方法
Spring的Bean的生命周期各个阶段扩展方法
|
2月前
|
XML Java 数据格式
Spring框架学习 -- Bean的生命周期和作用域
Spring框架学习 -- Bean的生命周期和作用域
29 2