自己電腦做網(wǎng)站訪問快嗎小時seo百度關(guān)鍵詞點擊器
目錄
使用@PostConstruct注解
實現(xiàn)InitializingBean接口
實現(xiàn)CommandLineRunner接口
實現(xiàn)ApplicationRunner接口
使用@EventListener注解監(jiān)聽ApplicationReadyEvent事件
應(yīng)用啟動完成之前或者之后,我們需要拿數(shù)據(jù)庫中的一些數(shù)據(jù)加載到本地緩存中。這些數(shù)據(jù)一般都是不經(jīng)常變更而且有被經(jīng)常讀取的,并且數(shù)據(jù)量不是很大的。這樣操作可以減少一些數(shù)據(jù)庫的壓力。下面是我們常用的實現(xiàn)方式,具體的要用那種,看對應(yīng)各自應(yīng)用的設(shè)計和需求。
使用@PostConstruct
注解
在需要加載數(shù)據(jù)的組件中,可以使用@PostConstruct
注解的方法,該方法會在Spring容器初始化該類之后自動調(diào)用
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class CacheInitializer { @Autowired private YourRepository yourRepository; // 假設(shè)你有一個訪問數(shù)據(jù)庫的Repository @Autowired private CacheManager cacheManager; // 假設(shè)你使用Spring Cache進行緩存管理 @PostConstruct public void init() { // 從數(shù)據(jù)庫中獲取數(shù)據(jù) List<YourData> dataList = yourRepository.findAll(); // 將數(shù)據(jù)加載到緩存中 dataList.forEach(data -> { cacheManager.getCache("yourCacheName").put(data.getId(), data); }); }
}
實現(xiàn)InitializingBean接口
它只包含一個方法?afterPropertiesSet()
。當(dāng)一個 bean 的所有屬性都被 Spring 容器設(shè)置之后(即通過依賴注入),afterPropertiesSet()
?方法會被自動調(diào)用
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class CacheInitializingBean implements InitializingBean { @Autowired private YourRepository yourRepository; // 假設(shè)你有一個訪問數(shù)據(jù)庫的Repository @Autowired private CacheManager cacheManager; // 假設(shè)你使用Spring Cache進行緩存管理 @Override public void afterPropertiesSet() throws Exception { // 在所有屬性被設(shè)置之后,執(zhí)行此方法 // 從數(shù)據(jù)庫中獲取數(shù)據(jù)并加載到緩存中 List<YourData> dataList = yourRepository.findAll(); dataList.forEach(data -> { cacheManager.getCache("yourCacheName").put(data.getId(), data); }); }
}
注:使用?InitializingBean
?接口與使用?@PostConstruct
?注解的效果類似,但它們在 Spring 生命周期中的調(diào)用時機略有不同。@PostConstruct
?注解的方法是在依賴注入完成后立即調(diào)用,而?InitializingBean
?的?afterPropertiesSet()
?方法則是在所有屬性被設(shè)置之后調(diào)用。在大多數(shù)情況下,這兩個選項可以互換使用,具體選擇哪個取決于你的個人喜好和項目需求。
實現(xiàn)CommandLineRunner
接口
CommandLineRunner
接口提供了一個run
方法,該方法會在Spring Boot應(yīng)用啟動后立即執(zhí)行。
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component; @Component
public class CacheRunner implements CommandLineRunner { @Autowired private YourRepository yourRepository; @Autowired private CacheManager cacheManager; @Override public void run(String... args) throws Exception { // 從數(shù)據(jù)庫中獲取數(shù)據(jù)并加載到緩存中 List<YourData> dataList = yourRepository.findAll(); dataList.forEach(data -> { cacheManager.getCache("yourCacheName").put(data.getId(), data); }); }
}
實現(xiàn)ApplicationRunner
接口
ApplicationRunner
接口類似于CommandLineRunner
,但提供了更靈活的參數(shù)處理方式
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component; @Component
public class CacheApplicationRunner implements ApplicationRunner { @Autowired private YourRepository yourRepository; @Autowired private CacheManager cacheManager; @Override public void run(ApplicationArguments args) throws Exception { // 從數(shù)據(jù)庫中獲取數(shù)據(jù)并加載到緩存中 List<YourData> dataList = yourRepository.findAll(); dataList.forEach(data -> { cacheManager.getCache("yourCacheName").put(data.getId(), data); }); }
}
使用@EventListener
注解監(jiān)聽ApplicationReadyEvent
事件
Spring Boot在啟動完成后會發(fā)布一個ApplicationReadyEvent
事件,你可以監(jiān)聽這個事件并在事件觸發(fā)時執(zhí)行加載數(shù)據(jù)的邏輯。
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component; @Component
public class CacheInitializer { @Autowired private YourRepository yourRepository; @Autowired private CacheManager cacheManager; @EventListener(ApplicationReadyEvent.class) public void onApplicationReadyEvent() { // 從數(shù)據(jù)庫中獲取數(shù)據(jù)并加載到緩存中 List<YourData> dataList = yourRepository.findAll(); dataList.forEach(data -> { cacheManager.getCache("yourCacheName").put(data.getId(), data); }); }
}
需要注意的是,加載數(shù)據(jù)的操作應(yīng)該盡可能快地完成,避免延遲應(yīng)用的啟動時間。如果加載數(shù)據(jù)量非常大,你可能需要考慮異步加載或延遲加載的策略。