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

简单个人网站制作网站统计代码

简单个人网站制作,网站统计代码,如何做网站评估分析,wordpress 的子主题目录 一、静态代码块 二、构造方法 三、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://www.dtcms.com/wzjs/245792.html

相关文章:

  • 南宁企业网站江苏营销型网站建设
  • 机关网站建设前期准备工作产品怎么进行推广
  • 常州做半导体的公司宁波seo网络推广咨询价格
  • 但是网站相关内容和程序并没有建设完_其次网站公司给我公司的公司网站制作
  • 点对点视频网站开发株洲24小时新闻
  • 商务网站建设教学视频网络推广优化方案
  • 门户网网站建设功能需求表魔贝课凡seo课程好吗
  • 购物网站开发的背景与意义陕西百度代理公司
  • 亿唐为什么2005年做虚拟网站北京搜索引擎关键词优化
  • wordpress 主题 最简单太原网站建设方案优化
  • 彩票网站建设柏推广注册app拿佣金
  • 东圃手机网站建设steam交易链接在哪里
  • 网站建设方案备案广西seo快速排名
  • 织梦下载网站模板做网站公司哪家好
  • 网站建设的售后服务怎么写郑州做网络营销渠道
  • 网站方案制作网站设计模板网站
  • 昆明培训网站建设旺道seo推广有用吗
  • 贵阳网站建设有限公司百度关键词搜索热度
  • 邯郸市属于哪个省搜索引擎推广seo
  • 设计论坛seo是什么意思的缩写
  • 重庆建设工程信息网查安全员的seo谷歌
  • 网页无法访问是什么意思厦门seo排名外包
  • 母婴网站的功能设计搜索引擎地址
  • 婚纱网站源码线上营销推广方法
  • 网站建设 招聘需求谷歌优化方法
  • 类似谷德设计网的网站快速排名点击工具
  • 行业网站推广方案最近一周的新闻大事10条
  • 做瞹瞹嗳视频网站活动营销案例100例
  • 来个网站2021能用的东莞网站建设排名
  • 免费企业查询网站知名品牌营销策略