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

java项目启动时,执行某方法

1. J2EE项目

在Servlet类中重写init()方法,这个方法会在Servlet实例化时调用,即项目启动时调用。

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;public class MyServlet extends HttpServlet {@Overridepublic void init() throws ServletException {super.init();// 在项目启动时执行的代码System.out.println("Servlet 初始化时执行的代码");}
}

配置web.xml

web.xml文件中配置Servlet,以确保它在项目启动时加载。

<servlet><servlet-name>myServlet</servlet-name><servlet-class>com.example.MyServlet</servlet-class><load-on-startup>1</load-on-startup>
</servlet>

<load-on-startup>元素的值为1,表示Servlet将在项目启动时加载。

2. Spring框架,使用@PostConstruct注解

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;@Component
public class MyComponent {@PostConstructpublic void init() {// 在项目启动时执行的代码System.out.println("Spring @PostConstruct 初始化时执行的代码");}
}

3. Spring框架,使用ApplicationListener接口

Spring的ApplicationListener接口允许我们监听Spring应用上下文的启动事件,从而在项目启动时执行代码。 

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;@Component
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {// 在项目启动时执行的代码System.out.println("Spring ApplicationListener 初始化时执行的代码");}
}

4. Spring框架,实现CommandLineRunner或ApplicationRunner接口

实现CommandLineRunner接口,并重写run方法。

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class MyCommandLineRunner implements CommandLineRunner {@Overridepublic void run(String... args) throws Exception {// 在项目启动时执行的代码System.out.println("Spring CommandLineRunner 初始化时执行的代码");}
}

实现ApplicationRunner接口,并重写run方法。

import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.ApplicationArguments;
import org.springframework.stereotype.Component;@Component
public class MyApplicationRunner implements ApplicationRunner {@Overridepublic void run(ApplicationArguments args) throws Exception {// 在项目启动时执行的代码System.out.println("Spring ApplicationRunner 初始化时执行的代码");}
}

http://www.lryc.cn/news/520271.html

相关文章:

  • 详解如何自定义 Android Dex VMP 保护壳
  • Grails应用http.server.requests指标数据采集问题排查及解决
  • 开源临床试验软件OpenClinica的安装
  • 网络安全 | 网络安全法规:GDPR、CCPA与中国网络安全法
  • 深入学习 Python 爬虫:从基础到实战
  • element plus 使用 upload 组件达到上传数量限制时隐藏上传按钮
  • 音频DSP的发展历史
  • 2025低代码与人工智能AI新篇
  • 【HarmonyOS Next NAPI 深度探索1】Node.js 和 CC++ 原生扩展简介
  • redis的学习(四)
  • C# winform 多线程 UI更新数据 报错:无法访问已释放的对象。
  • error: linker `link.exe` not found
  • Vue.js组件开发-如何使用moment.js
  • Linux第二课:LinuxC高级 学习记录day01
  • 《DOM NodeList》
  • Nginx代理同域名前后端分离项目的完整步骤
  • uniapp页面高度设置(铺满可视区域、顶部状态栏高度、底部导航栏高度)
  • 解锁 RAG 技术:从原理、论文研读走向实战应用RAG
  • HTML5实现好看的中秋节网页源码
  • 数字孪生笔记 1 工业数字孪生的意义
  • 013:深度学习之神经网络
  • 计算机网络(四)网络层
  • 【ArcGIS微课1000例】0138:ArcGIS栅格数据每个像元值转为Excel文本进行统计分析、做图表
  • Linux 中统计进程的线程数 | 查看进程的线程
  • 【深度学习 】训练过程中loss出现nan
  • Linux - 什么是线程和线程的操作
  • windows及linux 安装 Yarn 4.x 版本
  • 如何设计一个 RPC 框架?需要考虑哪些点?
  • 初学stm32 --- DAC输出三角波和正弦波
  • 开源cJson用法