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

深圳找做兼职女上班的网站/网络销售是干嘛的

深圳找做兼职女上班的网站,网络销售是干嘛的,英文网站如何做seo,php网站建设 关键技术目录 一、静态代码块 二、构造方法 三、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.whsansanxincailiao.cn/news/30345942.html

相关文章:

  • 建设工程方面的资料在哪个网站下载比较方便/网站快速收录的方法
  • 自己做网站处理图片用什么软件下载/最新黑帽seo教程
  • 珠海公众号开发公司/潍坊seo外包平台
  • 如何建一个自己的网站/如皋网站制作
  • wordpress做单页销售网站/小广告
  • 做网站的销售话术/seo云优化公司
  • 郑州响应式网站/品牌运营策略
  • 西安做网站设计的公司/智慧教育
  • seo网站关键词优化排名/杭州网站运营十年乐云seo
  • 四川省建设厅网站证/sem 优化价格
  • wordpress 仿f/windows优化软件
  • wordpress文章无法中文版/5g网络优化培训
  • 网站建设固定资产投资/百度快照怎么使用
  • 潮安区建设局网站/网页设计论文
  • 如何创建一个公司/新站seo外包
  • 网站开发一个页面多少钱/潍坊在线制作网站
  • 国外做鞋子的网站有哪些/上海关键词排名软件
  • 新疆生产建设兵团公安局网站/seo站内优化
  • 大型网站建设行情/青岛百度快速排名优化
  • 企业网站建设方案书模板/黄页网推广服务
  • 黄山网站建设公司/山东今日头条新闻
  • 国产4k高清电视十大排名/武汉网站营销seo方案
  • 网站如何做才能被360收录/找回今日头条
  • 专门做油画交流的网站/嘉兴seo报价
  • python和wordpress/西安seo
  • 太原模板建站定制/关键词排名优化工具
  • seo网站排名优化软件/如何快速提升自己
  • 张家港专业的网站制作公司/站长统计app下载
  • asp做旅游网站毕业论文/南宁seo产品优化服务
  • 广西注册公司网站/seo关键词排名优化怎样