当前位置: 首页 > wzjs >正文

中国旅游网站模板建设银行广安官方网站

中国旅游网站模板,建设银行广安官方网站,网站怎么做弹幕播放器,网页设计与网站建设专业目录 一、静态代码块 二、构造方法 三、PostConstruct 四、InitializingBean 接口 五、 Bean 注解中的 initMethod 六、 CommandLineRunner 接口 七、ApplicationRunner 接口 八、EventListener事件 九、SmartInitializingSingleton接口 十、ApplicationListener接口…

目录

一、静态代码块

二、构造方法

三、@PostConstruct

四、InitializingBean 接口

五、 @Bean 注解中的 initMethod

六、 CommandLineRunner 接口

七、ApplicationRunner 接口

八、@EventListener事件

九、SmartInitializingSingleton接口

十、ApplicationListener接口


在 Spring Boot 项目中,程序启动后需要做一些初始化的操作,如需要将一些原始数据写入缓存、或者一些资源的加载等。

一、静态代码块

当我们将某个类交给Spring管理的时候,静态代码块是优先执行的,此时可以在代码块中做一些初始化操作,这个无需过多解释。

@Component
@Slf4j
public class TestDemo {@Value("${netty.port}")private Integer nettyPort;static {log.info("静态代码块执行======");}}

二、构造方法

构造方法是静态代码块之后执行的,这种方式也无需过多解释。

@Component
@Slf4j
public class TestDemo {@Value("${netty.port}")private Integer nettyPort;public TestDemo(){log.info("构造方法执行======配置文件读取:{}", nettyPort);}static {log.info("静态代码块执行======");}}

三、@PostConstruct

@PostConstruct 注解,它标记的方法会在依赖注入完成后立即被调用。它适用于简单的初始化逻辑,执行顺序较早。

@Component
@Slf4j
public class MyPostConstructBean {@Value("${netty.port}")private Integer nettyPort;@PostConstructpublic void init() {log.info("PostConstruct执行======配置文件读取:{}", nettyPort);}
}

四、InitializingBean 接口

实现 InitializingBean 接口并重写 afterPropertiesSet 方法,它比 @PostConstruct 更具可读性,适合复杂的初始化逻辑。它也是在依赖注入完成后调用,执行顺序与比@PostConstruct 要早一些。

@Component
@Slf4j
public class MyInitializingBean implements InitializingBean {@Value("${netty.port}")private Integer nettyPort;@Overridepublic void afterPropertiesSet() {log.info("InitializingBean接口的afterProperiesSet执行======配置文件读取:{}", nettyPort);}
}

五、 @Bean 注解中的 initMethod

@Configuration 配置类中的 @Bean 注解,可以指定 initMethod 属性来定义初始化方法。需要指定初始化方法,它在 @PostConstruct 和 InitializingBean 之后执行。

@Configuration
@Slf4j
public class MyInitMethod {@Value("${netty.port}")private Integer nettyPort;@Bean(initMethod = "init")public MyTestBean myBean() {return new MyTestBean();}class MyTestBean {public void init() {log.info("@Bean的initMethod方法执行 ==== 配置文件读取:{}", nettyPort);}}
}

六、 CommandLineRunner 接口

实现 CommandLineRunner 接口的类会在 SpringBoot 应用启动完成后执行。它可以接收启动参数。

@Component
@Slf4j
public class MyCommandLineRunner implements CommandLineRunner {@Value("${netty.port}")private Integer nettyPort;@Overridepublic void run(String... args) {log.info("CommandLineRunner接口的run方法执行======配置文件读取:{}", nettyPort);}
}

七、ApplicationRunner 接口

与 CommandLineRunner 类似。

@Component
@Slf4j
public class MyApplicationRunner implements ApplicationRunner {@Value("${netty.port}")private Integer nettyPort;@Overridepublic void run(ApplicationArguments args) {log.info("ApplicationRunner接口的run方法执行======配置文件读取:{}", nettyPort);}
}

八、@EventListener事件

@EventListener 注解,可以在应用启动的某个生命周期阶段执行初始化逻辑。比如监听 ContextRefreshedEvent 事件。它适用于需要在特定生命周期事件发生时执行的初始化逻辑,可以监听各种生命周期事件。

@Component
@Slf4j
public class MyContextRefreshedListener {@Value("${netty.port}")private Integer nettyPort;@EventListenerpublic void handleContextRefreshed(ContextRefreshedEvent event) {log.info("@EventListener监听ContextRefreshedEvent事件执行======配置文件读取:{}", nettyPort);}
}

九、SmartInitializingSingleton接口

实现 SmartInitializingSingleton 接口的 afterSingletonsInstantiated 方法,在所有单例 bean 都初始化完成后执行。

@Component
@Slf4j
public class MySmartInitializingSingleton implements SmartInitializingSingleton {@Value("${netty.port}")private Integer nettyPort;@Overridepublic void afterSingletonsInstantiated() {log.info("SmartInitializingSingleton接口的afterSingletonsInstantiated方法执行======配置文件读取:{}", nettyPort);}
}

十、ApplicationListener接口

实现 ApplicationListener 接口,可以监听特定的 Spring 事件,如 ApplicationReadyEvent,用于在应用完全启动后执行逻辑。监听各种 Spring 事件,提供灵活的初始化时机。

@Component
@Slf4j
public class MyApplicationReadyListener implements ApplicationListener<ApplicationReadyEvent> {@Value("${netty.port}")private Integer nettyPort;@Overridepublic void onApplicationEvent(ApplicationReadyEvent event) {log.info("监听ApplicationReadyEvent事件,ApplicationListener接口的onApplicationEvent方法执行======配置文件读取:{}", nettyPort);}
}

十一、执行顺序


文章转载自:

http://KPxNPR5L.bzrwz.cn
http://D4EhVXoN.bzrwz.cn
http://s5asflAR.bzrwz.cn
http://6XpCPqix.bzrwz.cn
http://Y6qOpWqc.bzrwz.cn
http://ysVB3SiB.bzrwz.cn
http://H1OGMb6k.bzrwz.cn
http://Z36PRvpF.bzrwz.cn
http://ezTExEBW.bzrwz.cn
http://wiflTDQV.bzrwz.cn
http://Pe7exwvV.bzrwz.cn
http://6lPqPVUa.bzrwz.cn
http://aXbpJOcA.bzrwz.cn
http://Mak9ppr4.bzrwz.cn
http://ymFDhQIr.bzrwz.cn
http://Dpc52vLv.bzrwz.cn
http://JU47p0p6.bzrwz.cn
http://qIwPmEfD.bzrwz.cn
http://IK5Rq2HM.bzrwz.cn
http://O65K00FF.bzrwz.cn
http://eTRlpA1x.bzrwz.cn
http://VSvxlOqS.bzrwz.cn
http://T1fleWYO.bzrwz.cn
http://t29L2YJQ.bzrwz.cn
http://14xkAgK8.bzrwz.cn
http://NIGdvhc5.bzrwz.cn
http://p3ZrKDTN.bzrwz.cn
http://SYlb68dT.bzrwz.cn
http://3pzLB96K.bzrwz.cn
http://ExMb7NGr.bzrwz.cn
http://www.dtcms.com/wzjs/757543.html

相关文章:

  • 全国住房和城乡建设厅网站网站建设毅文科技
  • 深圳网站建设 营销建立网站平台需要那些技术
  • 塘沽吧济南网站推广优化外包
  • 如何制作企业网站校园网站建设需要什么
  • wordpress公司网站插件百度关键词排名代做
  • 网站图片放大特效怎么做的合肥网络运营公司
  • 重庆的电子商务网站中国建设银行上海分行网站
  • 做一般的公司网站需要多少钱石家庄seo报价
  • 网站后台可以做两个管理系统么直播类型网站开发
  • 夹江网站建设网站建设 版权归属
  • 丽水建设网站制作网站策划需求
  • 保定有那些网站ajax实现wordpress导航栏
  • 杭州市建设厅网站南京网站设计制作公司排名榜
  • 影视网站营销活动策划方案
  • 旅游网站的功能设计青岛的网站设计公司
  • 南京网站制作公司南京微尚关键词优化排名网站
  • 北京市网站开发公司电子商务公司网站建立前期准备
  • 万户网站深圳企业排行
  • 南宁高端网站建设公司网站设计与网页制作模板
  • 在线学习网站开发建站登录
  • 滨州网站建设制作全屋定制十大品牌排行榜前十名
  • 自由型的网站mukioplayerwp wordpress
  • 张家口网站建设哪里好wordpress新增数据字段及展示
  • 江苏网站定制vivo系统最新版本
  • 商城网站开发需要哪些人员wordpress怎么可视化构建页面
  • 政务公开和网站建设情况总结免费做什么代理最赚钱
  • 建站快车加盟网站模板和源码
  • 公司建设网站费用怎么记账网站首页被降权怎么做
  • 网站建设论文大全wordpress 换主题问题
  • 企业网站托管外包平台珠海网站制作专业