欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring Bean 生命周期测试

程序员文章站 2023-11-06 09:28:40
本文代码GitHub地址 Bean的生命周期是开始创建到销毁的过程。需要实现相关的类BeanNameAware ,DisposableBean, InitializingBean ,并注册InstantiationAwareBeanPostProcessor。 Bean类实现BeanNameAwar ......

 本文代码github地址

bean的生命周期是开始创建到销毁的过程。需要实现相关的类beannameaware   ,disposablebean, initializingbean ,并注册instantiationawarebeanpostprocessor。

 

 

bean类实现beannameaware   ,disposablebean, initializingbean 接口

package com.bean.life.entity;

import org.springframework.beans.beansexception;
import org.springframework.beans.factory.*;
import org.springframework.stereotype.component;


@component
public class user implements beanfactoryaware
    , beannameaware
    , initializingbean
    , disposablebean
{
    @override
    public void setbeanfactory(beanfactory beanfactory) throws beansexception {
        system.out.println("beanfactoryaware  setbeanfactory");
    }

    @override
    public void setbeanname(string s) {
        system.out.println("beannameaware接口:  setbeanname = " + s);
    }

    @override
    public void afterpropertiesset() throws exception {
        system.out.println("initializingbean接口:  afterpropertiesset");
    }

    @override
    public void destroy() throws exception {
        system.out.println("disposablebean接口:  destroy");

    }
@postconstruct public void init(){ system.out.println("postconstruct"); } @predestroy public void destory(){ system.out.println("predestroy"); } }

 注册instantiationawarebeanpostprocessor接口。

这里的instantiationawarebeanpostprocessoradapter是 instantiationawarebeanpostprocessor 的子孙类。

package com.bean.life.entity;

import org.springframework.beans.beansexception;
import org.springframework.beans.propertyvalues;
import org.springframework.beans.factory.config.instantiationawarebeanpostprocessoradapter;
import org.springframework.stereotype.component;

import java.beans.propertydescriptor;
import java.lang.reflect.constructor;


@component
public class myinstantiationawarebeanpostprocessor extends
        instantiationawarebeanpostprocessoradapter {


    public myinstantiationawarebeanpostprocessor() {
        super();
    }

    @override
    public constructor<?>[] determinecandidateconstructors(class<?> beanclass, string beanname) throws beansexception {

        if(beanname.equals("user")){
            system.out.println("myinstantiationawarebeanpostprocessor接口: determinecandidateconstructors: " + beanname);
        }

        return super.determinecandidateconstructors(beanclass, beanname);
    }

    @override
    public object getearlybeanreference(object bean, string beanname) throws beansexception {
        if(beanname.equals("user")){
            system.out.println("myinstantiationawarebeanpostprocessor接口: getearlybeanreference: " + beanname);
        }

        return super.getearlybeanreference(bean, beanname);
    }

    @override
    public object postprocessbeforeinstantiation(class<?> beanclass, string beanname) throws beansexception {
        if(beanname.equals("user")){
            system.out.println("myinstantiationawarebeanpostprocessor接口: postprocessbeforeinstantiation: " + beanname);
        }

        return super.postprocessbeforeinstantiation(beanclass, beanname);
    }

    @override
    public boolean postprocessafterinstantiation(object bean, string beanname) throws beansexception {
        if(beanname.equals("user")){
            system.out.println("myinstantiationawarebeanpostprocessor接口: postprocessafterinstantiation : " + beanname);
        }

        return super.postprocessafterinstantiation(bean, beanname);
    }

    @override
    public propertyvalues postprocesspropertyvalues(propertyvalues pvs, propertydescriptor[] pds, object bean, string beanname) throws beansexception {

        if(beanname.equals("user")){
            system.out.println("myinstantiationawarebeanpostprocessor接口:  postprocesspropertyvalues: " + beanname);
        }
        return super.postprocesspropertyvalues(pvs, pds, bean, beanname);
    }

    @override
    public object postprocessbeforeinitialization(object bean, string beanname) throws beansexception {
        if(beanname.equals("user")){
            system.out.println("myinstantiationawarebeanpostprocessor接口: postprocessbeforeinitialization: " + beanname);
        }

        return super.postprocessbeforeinitialization(bean, beanname);
    }

    @override
    public object postprocessafterinitialization(object bean, string beanname) throws beansexception {
        if(beanname.equals("user")){
            system.out.println("myinstantiationawarebeanpostprocessor接口:  postprocessafterinitialization: " + beanname);
        }

        return super.postprocessafterinitialization(bean, beanname);
    }
}

 

输出

mybeanfactorypostprocessor ---  beanfactorypostprocessor
myinstantiationawarebeanpostprocessor接口: postprocessbeforeinstantiation: user
myinstantiationawarebeanpostprocessor接口: determinecandidateconstructors: user
myinstantiationawarebeanpostprocessor接口: postprocessafterinstantiation : user
myinstantiationawarebeanpostprocessor接口:  postprocesspropertyvalues: user
beannameaware接口:  setbeanname = user
beanfactoryaware  setbeanfactory
myinstantiationawarebeanpostprocessor接口: postprocessbeforeinitialization: user
postconstruct
initializingbean接口:  afterpropertiesset
myinstantiationawarebeanpostprocessor接口:  postprocessafterinitialization: user

predestroy
disposablebean接口:  destroy

 

 生命周期图解

Spring Bean 生命周期测试

spring 实现按 init-method 和destory-method的三种方式

 

  •  方式1:xml配置方式
package com.bean.life.entity;

public class user
{

    public  void init(){
        system.out.println("postconstruct");
    }

    public  void destory(){
        system.out.println("predestroy");
    }
}

 

 

<bean id="user" class="com.bean.life.entity" init-method="init" destroy-method="destroy">
</bean>

 

 

  • 方式2:注解配置

添加@postconstruct 和@predestroy进行指定

package com.bean.life.entity;


@component
public class user
{

    @postconstruct
    public  void init(){
        system.out.println("postconstruct");
    }

    @predestroy
    public  void destory(){
        system.out.println("predestroy");
    }
}

 

  • 方式3: 使用注解@bean
public @interface bean {
    @aliasfor("name")
    string[] value() default {};

    @aliasfor("value")
    string[] name() default {};

    autowire autowire() default autowire.no;

    string initmethod() default "";

    string destroymethod() default "(inferred)";
}

 

@configuration
public class userconfig{
   
      @bean(initmethod="init",destorymethod="destory")
      public user user(){
      return new user();
    }
    
}