A03Application.java
package com.lucifer.itheima.a03;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class A03Application {
public static void main(String[] args) {
//执行结果为
// 2023-11-06 10:41:10.761 INFO 54360 --- [ main] com.lucifer.itheima.a03.LifeCycleBean : 构造
// 2023-11-06 10:41:10.764 INFO 54360 --- [ main] com.lucifer.itheima.a03.LifeCycleBean : 依赖注入
// 2023-11-06 10:41:10.765 INFO 54360 --- [ main] com.lucifer.itheima.a03.LifeCycleBean : 初始化
// 2023-11-06 10:41:11.060 INFO 54360 --- [ main] com.lucifer.itheima.a03.LifeCycleBean : 销毁
ConfigurableApplicationContext context = SpringApplication.run(A03Application.class);
context.close();
}
}
LifeCycleBean.java
package com.lucifer.itheima.a03;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
@Slf4j
public class LifeCycleBean {
public LifeCycleBean(){
log.info("构造");
}
@Autowired
public void autowire(@Value("${JAVA_HOME}") String home) {
log.info("依赖注入:{}",home);
}
@PostConstruct
public void init() {
log.info("初始化");
}
@PreDestroy
public void destroy(){
log.info("销毁");
}
}
MyBeanPostProcessor.java
package com.lucifer.itheima.a03;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.stereotype.Component;
@Component
@Slf4j
//执行结果为
//2023-11-06 11:50:39.728 INFO 47300 --- [ main] c.l.itheima.a03.MyBeanPostProcessor : <<<<<<实例化之前执行,这里返回的对象会替换掉原来的bean
// 2023-11-06 11:50:39.730 INFO 47300 --- [ main] com.lucifer.itheima.a03.LifeCycleBean : 构造
// 2023-11-06 11:50:39.732 INFO 47300 --- [ main] c.l.itheima.a03.MyBeanPostProcessor : <<<<<<实例化之后,这里如果返回false会跳过依赖注入阶段
// 2023-11-06 11:50:39.732 INFO 47300 --- [ main] c.l.itheima.a03.MyBeanPostProcessor : <<<<<<初始化之前执行,这里返回的对象会替换掉原来的bean,如PostConstruct,@ConfigurationProperties
//2023-11-06 11:50:39.732 INFO 47300 --- [ main] com.lucifer.itheima.a03.LifeCycleBean : 初始化
// 2023-11-06 11:50:39.732 INFO 47300 --- [ main] c.l.itheima.a03.MyBeanPostProcessor : <<<<<<初始化之后执行,这里返回的对象会替换掉原来的bean,如代理增强
// 2023-11-06 11:50:40.034 INFO 47300 --- [ main] c.l.itheima.a03.MyBeanPostProcessor : <<<<<<销毁之前执行,如@PreDestroy
//2023-11-06 11:50:40.035 INFO 47300 --- [ main] com.lucifer.itheima.a03.LifeCycleBean : 销毁
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor, DestructionAwareBeanPostProcessor {
@Override
public void postProcessBeforeDestruction(Object bean,String beanName) throws BeansException{
if (beanName.equals("lifeCycleBean"))
log.info("<<<<<<销毁之前执行,如@PreDestroy");
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass,String beanName) throws BeansException{
if (beanName.equals("lifeCycleBean")){
log.info("<<<<<<实例化之前执行,这里返回的对象会替换掉原来的bean");
}
// 返回null的话就不会替换掉原来的对象
return null;
}
@Override
public boolean postProcessAfterInstantiation(Object bean,String beanName) throws BeansException{
if (beanName.equals("lifeCycleBean")) {
log.info("<<<<<<实例化之后,这里如果返回false会跳过依赖注入阶段");
return false;
}
return true;
}
@Override
public PropertyValues postProcessProperties(PropertyValues pvs,Object bean,String beanName) throws BeansException{
if (beanName.equals("lifeCycleBean")) {
log.info("<<<<<<依赖注入阶段执行,如@Autowired,@Value,@Resource");
}
return pvs;
}
@Override
public Object postProcessBeforeInitialization(Object bean,String beanName) throws BeansException{
if (beanName.equals("lifeCycleBean")) {
log.info("<<<<<<初始化之前执行,这里返回的对象会替换掉原来的bean,如PostConstruct,@ConfigurationProperties");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean,String beanName) throws BeansException{
if (beanName.equals("lifeCycleBean")){
log.info("<<<<<<初始化之后执行,这里返回的对象会替换掉原来的bean,如代理增强");
}
return bean;
}
}
package com.lucifer.itheima.a03;
import java.util.ArrayList;
import java.util.List;
public class TestMethodTemplate {
public static void main(String[] args) {
MyBeanFactory beanFactory = new MyBeanFactory();
beanFactory.addBeanPostProcessor(bean-> System.out.println("解析 @Autowired"));
beanFactory.addBeanPostProcessor(bean-> System.out.println("解析 @Resource"));
beanFactory.getBean();
}
// 模板方法 Template Method Pattern
static class MyBeanFactory {
public Object getBean() {
Object bean = new Object();
System.out.println("构造 "+bean);
System.out.println("依赖注入 "+ bean); // @Autowired,@Resource
for (BeanPostProcessor processor : processors) {
processor.inject(bean);
}
System.out.println("初始化 "+bean);
return bean;
}
private List<BeanPostProcessor> processors = new ArrayList<>();
public void addBeanPostProcessor(BeanPostProcessor processor) {
processors.add(processor);
}
}
static interface BeanPostProcessor{
public void inject(Object bean); //对依赖注入阶段的扩展
}
}