首页 > 编程知识 正文

react生命周期详解,简述spring生命周期

时间:2023-05-06 14:15:17 阅读:182260 作者:4662

Spring生命周期详细信息1、传统的JAVA Bean生命周期是使用Java关键字new实例化Bean,然后Bean即可使用。 当不再使用bean时,Java会自动进行垃圾回收。 二、Spring bean的生命周期Spring的生命周期更为复杂,接下来是bean的结构过程。

1. Spring启动,查找并加载需要被Spring管理的bean,进行Bean的实例化

2. Bean实例化后将Bean的引入和值注入到Bean的属性中

3. 如果Bean实现了BeanNameAware接口的话,Spring将Bean的id传递给setBeanName()方法

BeanNameAware角色:让Bean获取BeanFactory配置的名称。

4. 如果Bean实现了BeanFactoryAware接口的话,Spring将调用setBeanFactory()方法,将BeanFactory容器实例传入

BeanFactoryAware角色:让Bean获取对构成他们的BeanFactory的引用。

5. 如果Bean实现了ApplicationContextAware接口的话,Spring将调用Bean的setApplicationContext()方法,将bean所在应用上下文引用传入

为什么使用ApplicationContextAware接口:在spring项目中,类之间的关系由spring容器管理,但在一个项目中并非由spring容器管理的某些类在这种情况下,无法用常规方式注入bean。 这时,spring为我提供了ApplicationContextAware接口。 您可以创建实现ApplicationContextAware的工具类,从工具类中获取所需的bean,然后由spring容器中不存在的类调用。

6. 如果Bean实现了BeanPostProcessor接口,Spring就将调用他们的postProcessBeforeInitialization()方法

postprocessbeforeinitialization ()方法:在初始化方法之前调用

7. 如果Bean实现了InitializingBean接口,Spring将调用他们的afterPropertiesSet()方法。类似的,如果bean使用init-method声明了初始化方法,该方法也会被调用

afterPropertiesSet ()方法:初始化方法。 如果使用init-method声明初始化方法,请执行afterPropertiesSet ()方法,然后执行init-method声明的方法。

8. 如果Bean实现了BeanPostProcessor接口,Spring就将调用他们的postProcessAfterInitialization()方法

postProcessAfterInitialization ()方法:初始化方法后调用

9. 此时,Bean已经准备就绪,可以被应用程序使用了。他们将一直驻留在应用上下文中,直到应用上下文被销毁

10. 如果bean实现了DisposableBean接口,Spring将调用它的destory()接口方法,同样,如果bean使用了destory-method 声明销毁方法,该方法也会被调用

destory (方法:在销毁前调用。 如果使用destory-method声明销毁方法,请执行destory ()方法,然后执行destory-method声明中的方法。

三. Spring单例和多例管理1、单例管理对象

scope=”singleton ",即缺省情况下,在容器启动时实例化。 但是,也可以通过指定Bean节点的lazy-init=”true "来延迟Bean的初始化。 在这种情况下,只有在首次获取Bean时,即第一次请求Bean时,才会进行Bean初始化。

2、非单例管理对象

如果scope=”prototype ",容器将延迟bean的初始化。 当Spring读取xml文件时,不会立即创建对象,而是在第一次请求bean时初始化,例如在调用getBean方法时。 第一次请求每个prototype的bean时,Spring容器会通过调用构造函数创建此对象,然后调用init-method属性值指定的方法。 放弃对象时,Spring容器不会调用任何方法。 因为是非单例的,所以有很多这种类型的对象。 一旦Spring容器传递了此对象,它就不再管理此对象。

四.测试代码Car.class

publicclasscarimplementsbeannameaware,BeanFactoryAware,Application

ContextAware,InitializingBean,DisposableBean { private String carName; public Car(){ //实例化对象 System.out.println("Car Initializing "); } public void setBeanFactory(BeanFactory beanFactory) throws BeansException { //BeanFactory容器实例传入 System.out.println("Car.setBeanFactory invoke"); } public void setBeanName(String name) { //setBeanName方法 System.out.println("Car.setBeanName invoke"); } public void destroy() throws Exception { //销毁方法调用 System.out.println("Car.destory invoke"); } public void afterPropertiesSet() throws Exception { //afterPropertiesSet方法 System.out.println("Car.afterPropertiesSet invoke"); } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { //调用setApplicationContext方法 System.out.println("Car.setApplicationContext invoke"); } public String getBookName() { return bookName; }//注入值 public void setCarName(String carName) { this.carName = carName; System.out.println("setCarName: Car name has set."); } public void myPostConstruct(){ System.out.println("Car.myPostConstruct invoke"); } // 自定义初始化方法 @PostConstruct public void springPostConstruct(){ System.out.println("@PostConstruct"); } public void myPreDestory(){ System.out.println("Car.myPreDestory invoke"); System.out.println("---------------destroy-----------------"); } // 自定义销毁方法 @PreDestroy public void springPreDestory(){ System.out.println("@PreDestory"); } @Override protected void finalize() throws Throwable { System.out.println("------inside finalize-----"); }}

自定义实现BeanPostProcessor的MyBeanPostProcessor.class

public class MyBeanPostProcessor implements BeanPostProcessor { // 容器加载的时候会加载一些其他的bean,会调用初始化前和初始化后方法 // 这次只关注Car(bean)的生命周期 public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if(bean instanceof Car){ System.out.println("MyBeanPostProcessor.postProcessBeforeInitialization"); } return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if(bean instanceof Car){ System.out.println("MyBeanPostProcessor.postProcessAfterInitialization"); } return bean; }}

在resources 目录下新建Bean-Lifecycle.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描bean --> <context:component-scan base-package="com.bean.lifecycle"/> <!-- 实现了用户自定义初始化和销毁方法 --> <bean id="car" class="com.bean.lifecycle.Car" init-method="myPostConstruct" destroy-method="myPreDestory"> <!-- 注入bean 属性名称 --> <property name="carName" value="BW" /> </bean> <!--引入自定义的BeanPostProcessor--> <bean class="com.bean.lifecycle.MyBeanPostProcessor"/></beans>

做一个测试类

public class Test{ public static void main(String[] args) throws InterruptedException { // 为面试而准备的Bean生命周期加载过程 ApplicationContext context = new ClassPathXmlApplicationContext("Bean-Lifecycle.xml"); Car car= (Car)context.getBean("car"); System.out.println("Car name = " + car.getCarName()); ((ClassPathXmlApplicationContext) context).destroy(); }}

输出结果如下

Car Initializing setCarName: Car name has set.Car.setBeanName invokeCar.setBeanFactory invokeCar.setApplicationContext invokeMyBeanPostProcessor.postProcessBeforeInitialization@PostConstructCar.afterPropertiesSet invokeCar.myPostConstruct invokeMyBeanPostProcessor.postProcessAfterInitializationCar name = MB@PreDestoryCar.destory invokeCar.myPreDestory invoke---------------destroy-----------------

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。