spring bean流程图,一个bean实例的加载全过程。示意图

spring ioc 2_ide



代码演示:


package com.mr.li.test;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.mr.li.config.AppConfig;
import com.mr.li.service.Person;
import com.mr.li.service.impl.Man;

import lombok.extern.slf4j.Slf4j;


@Slf4j
public class Test {

public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Person person = ctx.getBean(Man.class);
person.behavior();
ctx.close();

}
}



bean的加载过程


package com.mr.li.service.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import com.mr.li.service.Animal;
import com.mr.li.service.Person;

/**
*
* @describe 实现的这些springframework包下的类可重写他们的方法去自定义自己的一些实现,使用的IOC必须是实现了ApplicationContext接口的IOC否则不会调用这些方法。
*
* @author li.yanlong@icjhd.com
*
* @date 2021-8-18 14:46:10
*/
@Component
public class Man implements Person, BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean, DisposableBean{

@Qualifier("cat")
@Autowired
private Animal animal;

@Override
public void behavior() {
animal.behavior();
}

@Override
public void destroy() throws Exception {
System.out.println("DisposableBean.destroy bean销毁方法");
}

@Override
public void afterPropertiesSet() throws Exception {
System.out.println("InitializingBean.afterPropertiesSet bean初始化方法。在设置了所有bean属性并满足了BeanFactoryAware、ApplicationContextAware等之后,由包含的BeanFactory调用。");
}

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware.applicationContext 回调设置ioc对象,不过通常拿到此对象会用此对象初始化一些数据,并不会对此对象本身改变什么");
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware.setBeanFactory bean工厂对当前对象的回调,提供了beanFactory对象,可利用beanFactory做一些处理 ");
}

@Override
public void setBeanName(String name) {
System.out.println("BeanNameAware.setBeanName 在创建当前对象的时候设置当前对象的bean名称,设置好的名称是: " + name);
}
}


前置处理器,和后置处理器回调方法


package com.mr.li.service.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

@Component
public class BeanPostProcessorExample implements BeanPostProcessor {

/**
* 可通过处理器过滤做些事情,如通过beanName可在加载类的时候过滤出某个类然后对其操作
*/
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("前置处理器,在加载每个bean之前调用: " + bean.getClass().getSimpleName() + " beanName: " + beanName);
return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
}

//注意,这个Bean后置处理器将对所有的Bean有效
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("后置处理器,在加载每个bean之后调用: " + bean.getClass().getSimpleName() + " beanName: " + beanName);
return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
}
}