目录标题
- @PostConstruct注解
- @EventListener方式
- InitializingBean的afterPropertiesSet方法
- 实现ApplicationRunner接口重写run方法
- 实现AplicationContextAware接口重写setApplicationContext
- 实现ServletContextListener接口contextInitialized方法
- 实现ServletContextAware接口setServletContext 方法
 
下面是总结的几种Spring容器启动时,做自定义初始化操做的方法;也是比较常用,一般做缓存预热,生成消费者启动,数据加载等操作,先于程序运行之前进行数据准备。
@PostConstruct注解
将要执行的方法所在的类交个Spring容器扫描(@Component),并且在要执行的方法上添加@PostConstruct注解执行,它用于指定一个方法在Bean被初始化后自动调用,用于执行一些特定的初始化逻辑,如建立数据库连接、加载配置文件、启动线程等。
@Slf4j
@Component
public class PostConstructTest {
    @PostConstruct
    public void postConstruct() {
        log.info("启动时自动执行  @PostConstruct 注解方法");
    }
}
@EventListener方式
将要执行的方法所在的类交个Spring容器扫描(@Component),并且在要执行的方法上添加@EventListener注解执行
 用于监听Spring事件并执行相应的操作。使用@EventListener注解的方法可以用来处理Spring中的各种事件
使用@EventListener注解的方法有以下要求:
方法必须是非静态的。
 方法的参数可以是Spring框架中的事件对象或自定义的对象,方法只会接收与其参数类型匹配的事件。
 方法返回类型可以是任意类型,但通常是void类型。
@Slf4j
@Component
public class EventListenerTest {
    @EventListener
    public void handleContextRefreshed(ContextRefreshedEvent event) {
        System.out.println("ContextRefreshedEvent received.");
    }
}
InitializingBean的afterPropertiesSet方法
这个是在bean 初始化的时候执行
 
 通过实现InitializingBean 接口,然后重写afterPropertiesSet()方法,当Bean的属性设置完成后,Spring容器会自动调用该方法。
使用示例如下
public class MyBean implements InitializingBean {
    private String name;
    
    public void setName(String name) {
        this.name = name;
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
        // 在属性设置完成后执行的初始化逻辑,这里面可以做一些数据初始化,本地缓存预热等操做
        System.out.println("Bean '" + name + "' is being initialized...");
    }
}
afterPropertiesSet()方法是InitializingBean接口中定义的一个方法,用于在Bean属性设置完成后执行一些初始化逻辑,可以通过实现该接口来实现自定义的初始化行为。
实现ApplicationRunner接口重写run方法
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {
    /**
     * 用于指示bean包含在SpringApplication中时应运行的接口。可以定义多个ApplicationRunner bean
     * 在同一应用程序上下文中,可以使用有序接口或@order注释对其进行排序。
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        log.info("启动时自动执行 ApplicationRunner 的 run 方法");
        Set<String> optionNames = args.getOptionNames();
        for (String optionName : optionNames) {
            log.info("这是传过来的参数[{}]", optionName);
        }
        String[] sourceArgs = args.getSourceArgs();
        for (String sourceArg : sourceArgs) {
            log.info("这是传过来sourceArgs[{}]", sourceArg);
        }
    }
}
实现AplicationContextAware接口重写setApplicationContext
当一个Bean实现了该接口并重写了setApplicationContext方法,Spring容器会在Bean创建后自动调用这个方法,将容器的上下文对象ApplicationContext传递给该方法。
@Component
public class MyBean implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
        // 在这里可以添加一些初始化的操作
    }
}
实现ServletContextListener接口contextInitialized方法
该方法会在填充完普通Bean的属性,但是还没有进行Bean的初始化之前执行
@Component
public class ServletContextListenerImpl implements ServletContextListener {
    /**
     * 在初始化Web应用程序中的任何过滤器或Servlet之前,将通知所有ServletContextListener上下文初始化。
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.info("启动时自动执行 ServletContextListener 的 contextInitialized 方法");
    }
}
实现ServletContextAware接口setServletContext 方法
它会在填充普通bean属性之后但在初始化之前调用
@Component
public class ServletContextAwareImpl implements ServletContextAware {
    /**
     * 在填充普通bean属性之后但在初始化之前调用
     * 类似于InitializingBean's 的 afterPropertiesSet 或自定义init方法的回调
     */
    @Override
    public void setServletContext(ServletContext servletContext) {
        log.info("启动时自动执行 ServletContextAware 的 setServletContext 方法");
    }
}
如有遗漏,欢迎补充,最后感谢您的阅览!













![深入理解强化学习——马尔可夫决策过程:占用度量-[基础知识]](https://img-blog.csdnimg.cn/direct/c2cba3d315284b52a5b67830ab89b722.png)





