Java程序启动时初始化数据的四种方式
更新时间:2024年02月04日 09:49:02 作者:huixiyang
本文主要介绍了Java程序启动时初始化数据的四种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
方式一: 利用 @PostConstruct 注解
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @Author yangxihui
* @Date 2023-11-29 15:06
* @Description
*/
@Component
public class MyInitTest {
@PostConstruct
public void init(){
System.out.println("------------init");
}
}方式二: 实现类 InitializingBean
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
/**
* @Author yangxihui
* @Date 2023-11-29 15:06
* @Description
*/
@Component
public class MyInitTest implements InitializingBean{
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("------------init");
}
}方式三: 实现类 CommandLineRunner
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
/**
* @Author yangxihui
* @Date 2023-11-29 15:24
* @Description
*/
@Component
public class MyInitTest implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("------------init");
}
}
方式四: springboot main方法中
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class LoginApplication {
public static void main(String[] args) {
SpringApplication.run(LoginApplication.class, args);
System.out.println("------------init");
}
}到此这篇关于Java程序启动时初始化数据的四种方式的文章就介绍到这了,更多相关Java 初始化数据 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
您可能感兴趣的文章:
相关文章
maven导入本地仓库jar包,报:Could not find artifact的解决
这篇文章主要介绍了maven导入本地仓库jar包,报:Could not find artifact的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-03-03
SpringBoot3整合Hutool-captcha实现图形验证码
在整合技术框架的时候,想找一个图形验证码相关的框架,看到很多验证码的maven库不再更新了或中央仓库下载不下来,还需要多引入依赖,后面看到了Hutool图形验证码(Hutool-captcha)中对验证码的实现,所以本文介绍了SpringBoot3整合Hutool-captcha实现图形验证码2024-11-11
springboot+chatgpt+chatUI Pro开发智能聊天工具的实践
本文主要介绍了springboot+chatgpt+chatUI Pro开发智能聊天工具的实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-04-04
Netty分布式NioEventLoop优化selector源码解析
这篇文章主要介绍了Netty分布式NioEventLoop优化selector源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-03-03


最新评论