Java程序啟動時(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程序啟動時(shí)初始化數(shù)據(jù)的四種方式的文章就介紹到這了,更多相關(guān)Java 初始化數(shù)據(jù) 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Security實(shí)現(xiàn)微信公眾號網(wǎng)頁授權(quán)功能
這篇文章主要介紹了Spring Security中實(shí)現(xiàn)微信網(wǎng)頁授權(quán),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
java數(shù)據(jù)庫連接池和數(shù)據(jù)庫連接示例
這篇文章主要介紹了java數(shù)據(jù)庫連接池和數(shù)據(jù)庫連接示例,需要的朋友可以參考下2014-05-05
關(guān)于Springboot日期時(shí)間格式化處理方式總結(jié)
這篇文章主要介紹了關(guān)于Springboot日期時(shí)間格式化處理方式總結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
java.lang.Void 與 void的比較及使用方法介紹
這篇文章主要介紹了java.lang.Void 與 void的比較及使用方法介紹,小編覺得挺不錯(cuò)的,這里給大家分享一下,需要的朋友可以參考。2017-10-10
SpringCloud Gateway的基本入門和注意點(diǎn)詳解
這篇文章主要介紹了SpringCloud Gateway的基本入門和注意點(diǎn),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
學(xué)習(xí)spring事務(wù)與消息隊(duì)列
這篇文章主要為大家詳細(xì)介紹了spring事務(wù)與消息隊(duì)列,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10

