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

