当前位置:   article > 正文

Spring面试题目--请讲解一下Spring bean的生命周期_springbean生命周期面试

springbean生命周期面试

Spring bean的生命周期

A. 5步骤版本
1.实例化
2.依赖注入
3.初始化
4.使用
5.销毁

A.1 代码示例

#创建一个对象,自定义销毁以及初始化方法
package com.ruoyi.bizsys.domain;


public class AccountInfo {

    private String acctNo;

    public AccountInfo(String acctNo) {
        this.acctNo = acctNo;
    }


    public AccountInfo() {
        System.out.println("1.实例化");
    }

    public String getAcctNo() {
        return acctNo;
    }


    public void setAcctNo(String acctNo) {

        System.out.println("2.依赖注入");
    }

    public void myInit(){
        System.out.println("3.初始化");
    }

    public void myDestory(){
        System.out.println("5.销毁");
    }
}
  • 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
  • 32
  • 33
  • 34
  • 35
<!-- 在配置文件中配置初始化方法 销毁方法 以及属性-->
<?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="AccountInfo" class="com.ruoyi.bizsys.domain.AccountInfo" init-method="myInit" destroy-method="myDestory">
        <property name="acctNo" value="123456789"></property>
    </bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
public class MyTest {

   //测试类测试5个步骤
    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml");
        Object  o = context.getBean(AccountInfo.class);
        System.out.println("4.使用");
        ((ClassPathXmlApplicationContext) context).close();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

A.2 输出结果
在这里插入图片描述

B. 7步骤版本
1.实例化
2.依赖注入
3.初始化前 BeanPostProcessor before方法 ⇒ postProcessBeforeInitialization(Object obj, String s)
4.初始化
5.初始化结束时 BeanPostProcessor after方法 ⇒ postProcessAfterInitialization(Object obj, String s)
6.使用
7.销毁
B. 1 代码示例

package com.ruoyi.bizsys.domain;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        if (bean instanceof AccountInfo){
            System.out.println("3.初始化前 BeanPostProcessor before方法 ");
        }
        return bean;
    }

    /**
     * 同上,不过时在初始化之后执行
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof AccountInfo){
            System.out.println("5.初始化后 BeanPostProcessor after 方法 ");
        }
        return bean;
    }
}

  • 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
  • 32
  • 33
<?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="AccountInfo" class="com.ruoyi.bizsys.domain.AccountInfo" init-method="myInit" destroy-method="myDestory">
        <property name="acctNo" value="123456789"></property>
    </bean>
    <bean id="MyBeanPostProcessor" class="com.ruoyi.bizsys.domain.MyBeanPostProcessor">
    </bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
package com.ruoyi.bizsys.domain;


public class AccountInfo {

    private String acctNo;

    public AccountInfo(String acctNo) {
        this.acctNo = acctNo;
    }


    public AccountInfo() {
        System.out.println("1.实例化");
    }

    public String getAcctNo() {
        return acctNo;
    }


    public void setAcctNo(String acctNo) {

        System.out.println("2.依赖注入");
    }

    public void myInit(){
        System.out.println("4.初始化");
    }

    public void myDestory(){
        System.out.println("7.销毁");
    }
}

  • 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
  • 32
  • 33
  • 34
  • 35
package com.ruoyi.bizsys.test;

import com.ruoyi.bizsys.domain.AccountInfo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml");
        Object  o = context.getBean(AccountInfo.class);
        System.out.println("4.使用");
        ((ClassPathXmlApplicationContext) context).close();
    }

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml");
        Object  o = context.getBean(AccountInfo.class);
        System.out.println("6.使用");
        ((ClassPathXmlApplicationContext) context).close();
    }
}

  • 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

B. 2 输出结果
在这里插入图片描述
C. 10步骤版本
1.实例化
2.依赖注入
3.BeanNameAware,BeanFactoryAware,ApplicationContextAware方法执行啦
【BeanNameAware#setBeanName,BeanFactoryAware#setBeanFactory,ApplicationContextAware#setApplicationContext】
4.初始化前 BeanPostProcessor before方法 ⇒ postProcessBeforeInitialization(Object obj, String s)
5.InitializingBean 方法执行啦
6.初始化
7.初始化结束时 BeanPostProcessor after方法 ⇒ postProcessAfterInitialization(Object obj, String s)
8.使用
9.DisposableBean 方法执行啦
10.销毁
C. 1 代码示例

package com.ruoyi.bizsys.domain;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;

public class AccountInfo implements  BeanFactoryAware, InitializingBean, DisposableBean {

    private String acctNo;

    public AccountInfo(String acctNo) {
        this.acctNo = acctNo;
    }


    public AccountInfo() {
        System.out.println("1.实例化");
    }

    public String getAcctNo() {
        return acctNo;
    }


    public void setAcctNo(String acctNo) {

        System.out.println("2.依赖注入");
    }

    public void myInit(){
        System.out.println("6.初始化");
    }

    public void myDestory(){
        System.out.println("10.销毁");
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        System.out.println("3.BeanNameAware,BeanFactoryAware,ApplicationContextAware方法执行啦" );
    }


    @Override
    public void destroy() throws Exception {
        System.out.println("9.DisposableBean 方法执行啦");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("5.InitializingBean 方法执行啦" );
    }
}

  • 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
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
<?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="AccountInfo" class="com.ruoyi.bizsys.domain.AccountInfo" init-method="myInit" destroy-method="myDestory">
        <property name="acctNo" value="123456789"></property>
    </bean>
    <bean id="MyBeanPostProcessor" class="com.ruoyi.bizsys.domain.MyBeanPostProcessor">
    </bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
package com.ruoyi.bizsys.domain;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

        if (bean instanceof AccountInfo){
            System.out.println("4.初始化前 BeanPostProcessor before方法 ");
        }
        return bean;
    }

    /**
     * 同上,不过时在初始化之后执行
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof AccountInfo){
            System.out.println("7.初始化后 BeanPostProcessor after 方法 ");
        }
        return bean;
    }
}

  • 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
  • 32
  • 33
package com.ruoyi.bizsys.test;

import com.ruoyi.bizsys.domain.AccountInfo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {

    @Test
    public void test1(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml");
        Object  o = context.getBean(AccountInfo.class);
        System.out.println("4.使用");
        ((ClassPathXmlApplicationContext) context).close();
    }

    @Test
    public void test2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml");
        Object  o = context.getBean(AccountInfo.class);
        System.out.println("6.使用");
        ((ClassPathXmlApplicationContext) context).close();
    }

    @Test
    public void test3(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring/SpringConfig.xml");
        Object  o = context.getBean(AccountInfo.class);
        System.out.println("8.使用");
        ((ClassPathXmlApplicationContext) context).close();
    }
}

  • 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
  • 32
  • 33
  • 34

C. 2 输出结果
在这里插入图片描述

可以根据代码一步步进行累加,逐步理解10步骤的意思即可

转载:https://www.bilibili.com/video/BV1L14y1S7cf/?spm_id_from=333.337.search-card.all.click&vd_source=6451bb187cdcf8a07158b6acb30c5cac

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/740623
推荐阅读
相关标签
  

闽ICP备14008679号