亲宝软件园·资讯

展开

Spring注解生命周期 Spring注解开发生命周期原理解析

Jimmyhe 人气:0
想了解Spring注解开发生命周期原理解析的相关内容吗,Jimmyhe在本文为您仔细讲解Spring注解生命周期的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Spring,注解,生命周期,下面大家一起来学习吧。

生命周期

initMethod和destroyMethod

Bean定义

public class Car {
  public Car() {
    System.out.println("car constructor");
  }

  public void init(){
    System.out.println("car init");
  }

  public void destroy(){
    System.out.println("car destroy");
  }
}

配置类

​ 多实例容器不会管理这个bean

@Configuration
public class MyCOnfigOfLifeCycle {

  @Bean(initMethod = "init",destroyMethod = "destroy")
  public Car car(){
    return new Car();
  }
}

获取容器

  @Test
  public void test01(){
    printBeans(applicationContext);

    // 关闭容器
    applicationContext.close();
  }

  private void printBeans(AnnotationConfigApplicationContext applicationContext){
    String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
    for (String beanDefinitionName : beanDefinitionNames) {
      System.out.println(beanDefinitionName);
    }
  }

InitializingBean和DisposableBean

Bean定义

public class Cat implements InitializingBean, DisposableBean {
  @Override
  public void destroy() throws Exception {
    System.out.println("cat destroy");
  }

  @Override
  public void afterPropertiesSet() throws Exception {
    System.out.println("cat initialize");
  }
}

实现了InitializingBean和Disposable后,也可以实现初始化和销毁的方法

PostConstruct和PreDestroy

Bean定义

public class Dog {
  public Dog(){
    System.out.println("dog constructor");
  }

  @PostConstruct
  public void init(){
    System.out.println("dog init");
  }

  @PreDestroy
  public void destroy(){
    System.out.println("dog destroy");
  }
}

BeanPostProcessor

bean

定义

public class MyBeanPostProcessor implements BeanPostProcessor {


  @Override
  public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("before init" + beanName);
    return bean;
  }

  @Override
  public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    System.out.println("after init" + beanName);
    return bean;
  }
}

加入到bean中,可以发现每个bean初始化前后都会执行功能

postProcessBeforeInitialization方法的返回值会被Spring容器作为处理后的Bean注册到容器中。如果你在postProcessBeforeInitialization方法中重新构造了一个Bean进行返回,而不是返回参数中的bean;那么你返回的Bean将会被注册到Spring容器中。而原来在Spring中配置的Bean(被Spring实例化的Bean)将会被覆盖。

一旦这两个方法返回null,就不会执行后面的postProcessBefore/AfterInitialization,(在applyBeanPostProcessorsBeforeInitialization中)

    for(Iterator var4 = this.getBeanPostProcessors().iterator(); var4.hasNext(); result = current) {
      BeanPostProcessor processor = (BeanPostProcessor)var4.next();
      current = processor.postProcessBeforeInitialization(result, beanName);
      if (current == null) {
        return result;
      }
    }

步骤:

populateBean给Bean赋值

initializeBean{ applyBeanPostProcessorsBeforeInitialization,

​ invokeInitMethods, // 自定义初始化

​ applyBeanPostProcessorsAfterInitialization}

底层使用

bean赋值,bean注入,@autowire,生命周期注解功能,@Async

加载全部内容

相关教程
猜你喜欢
用户评论