Java程序啟動時初始化數(shù)據(jù)的四種方式
更新時間:2024年02月04日 09:49:02 作者:huixiyang
本文主要介紹了Java程序啟動時初始化數(shù)據(jù)的四種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
方式一: 利用 @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");
}
}方式二: 實(shí)現(xiàn)類 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");
}
}方式三: 實(shí)現(xiàn)類 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");
}
}到此這篇關(guān)于Java程序啟動時初始化數(shù)據(jù)的四種方式的文章就介紹到這了,更多相關(guān)Java 初始化數(shù)據(jù) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
maven導(dǎo)入本地倉庫jar包,報:Could?not?find?artifact的解決
這篇文章主要介紹了maven導(dǎo)入本地倉庫jar包,報:Could?not?find?artifact的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
SpringBoot3整合Hutool-captcha實(shí)現(xiàn)圖形驗證碼
在整合技術(shù)框架的時候,想找一個圖形驗證碼相關(guān)的框架,看到很多驗證碼的maven庫不再更新了或中央倉庫下載不下來,還需要多引入依賴,后面看到了Hutool圖形驗證碼(Hutool-captcha)中對驗證碼的實(shí)現(xiàn),所以本文介紹了SpringBoot3整合Hutool-captcha實(shí)現(xiàn)圖形驗證碼2024-11-11
springboot+chatgpt+chatUI Pro開發(fā)智能聊天工具的實(shí)踐
本文主要介紹了springboot+chatgpt+chatUI Pro開發(fā)智能聊天工具的實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Netty分布式NioEventLoop優(yōu)化selector源碼解析
這篇文章主要介紹了Netty分布式NioEventLoop優(yōu)化selector源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03

