SpringBoot扩展机制

启动生命周期

一.Spring boot初始化器扩展

package com.lx.conmon.extend.intializer;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * Spring boot初始化器扩展
 * @author liu wei ping
 * @version 1.0
 * @date 2023-07-02 10:10:04
 */
public class PeopleInitializer implements ApplicationContextInitializer {
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        System.out.println("PeopleInitializer#initialize....");
    }
}

二.Spring boot监听器扩展 容器启动中

package com.lx.conmon.extend.listener;

import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationListener;

/**
 * Spring boot监听器扩展 容器启动中
 * @author liu wei ping
 * @version 1.0
 * @date 2023-07-02 10:15:03
 */
public class PeopleStartingListener implements ApplicationListener<ApplicationStartingEvent> {
    @Override
    public void onApplicationEvent(ApplicationStartingEvent event) {
        System.out.println("PeopleStartingListener#PeopleStartingListener....");
    }
}

三.Spring boot监听器扩展 容器启动完成

package com.lx.conmon.extend.listener;

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;

/**
 * Spring boot监听器扩展 容器启动完成
 * @author liu wei ping
 * @version 1.0
 * @date 2023-07-02 10:17:10
 */
public class PeopleStartedListener  implements ApplicationListener<ApplicationStartedEvent> {
    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        System.out.println("PeopleStartedListener#onApplicationEvent....");
    }
}

四.执行BeanFactory的后置处理器

package com.lx.conmon.extend.beanFactory;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

/**
 * 执行BeanFactory的后置处理器
 * @author liu wei ping
 * @version 1.0
 * @date 2023-07-02 10:29:24
 */
@Component
public class PeopleBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        Arrays.asList(beanFactory.getBeanDefinitionNames()).forEach(beanDefinitionName -> System.out.println(beanDefinitionName));
        System.out.println("PeopleBeanFactoryPostProcessor...");
    }

}

五.向BeanFactory中注册bean的postprocessor,用于后续bean创建的拦截操作

package com.lx.conmon.extend.beanPostProcessor;

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

/**
 * 向BeanFactory中注册bean的postprocessor,用于后续bean创建的拦截操作
 * @author liu wei ping
 * @version 1.0
 * @date 2023-07-02 10:40:20
 */
@Component
public class PeopleBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (beanName.equals("userApiServiceImpl")) {
            System.out.println("找到了userApiServiceImpl: " + bean);
        }
        return null;
    }
}

六.容器启动完成的回调

package com.lx.conmon.extend.runner;

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * 容器启动完成的回调
 * @author liu wei ping
 * @version 1.0
 * @date 2023-07-02 10:26:31
 */
@Component
public class PeopleApplicationRunner implements ApplicationRunner {
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("PeopleApplicationRunner#run.....");
    }
}

七. 上面初始化器和监听器一.二.三基于spi机制扩展

1.在resources/META-INF创建spring.factories文件

org.springframework.context.ApplicationContextInitializer=com.lx.conmon.extend.intializer.PeopleInitializer

org.springframework.context.ApplicationListener=com.lx.conmon.extend.listener.PeopleStartedListener,  com.lx.conmon.extend.listener.PeopleStartingListener

2.如图:

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值