SpringBoot如何指定某些類優(yōu)先啟動(dòng)
一、需求
1、項(xiàng)目中對(duì)某些IP地址和端口做了限制,只有寫在配置文件的內(nèi)容(ip)才可以訪問項(xiàng)目。
2、在進(jìn)行測試案例運(yùn)行時(shí)保證讀取配置文件中ip和port的類(CbeConfig)得提前運(yùn)行。
二、工作
1、如下的測試時(shí)肯定不行
@Test
public void getCbeTest(){
CbeConfig cbeConfig = new CbeConfig();
System.out.println("IP是" + cbeConfig.getIp());
System.out.println("Port是" + cbeConfig.port);
}
2、保證CbeConfig類在程序運(yùn)行起來的那一刻先存在,先寫一個(gè)讀取配置的類,程序運(yùn)行起來后讀取到配置后,然后再將讀取的參數(shù)設(shè)置到另一個(gè)類(CbeConfigAfter)中,以后提取參數(shù)。都使用CbeConfigAfter。
CbeConfigBefore類實(shí)現(xiàn)ApplicationRunner接口的run方法
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class CbeConfigBefore implements ApplicationRunner {
@Value("${cbe.ip}")
public String ip;
@Value("${cbe.port}")
public Integer port;
@Override
public void run(ApplicationArguments applicationArguments) throws Exception {
System.out.println("我第一個(gè)啟動(dòng)");
System.out.println("哈哈ip" + ip);
System.out.println("哈哈port" + port);
CbeConfigAfter cbeConfigAfter = CbeConfigAfter.getInstance();
cbeConfigAfter.setIp(ip);
cbeConfigAfter.setPort(port);
System.out.println("設(shè)置完畢");
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
CbeConfigAfter類寫成單例模式
package com.example.demo;
import lombok.Getter;
import lombok.Setter;
public class CbeConfigAfter {
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
String ip;
int port;
private static CbeConfigAfter cbeConfigAfter;
private CbeConfigAfter (){}
public static synchronized CbeConfigAfter getInstance() {
if (cbeConfigAfter == null) {
cbeConfigAfter = new CbeConfigAfter();
}
return cbeConfigAfter;
}
}
測試類
package com.example.demo.controllerTest;
import com.example.demo.CbeConfigAfter;
import com.example.demo.CbeConfigBefore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class CbeTest {
@Test
public void getCbeByAfterTest(){
CbeConfigAfter instance = CbeConfigAfter.getInstance();
System.out.println("IP是" + instance.getIp());
System.out.println("端口是" + instance.getPort());
}
@Test
public void getCbeBeforeTest(){
CbeConfigBefore cbeConfig = new CbeConfigBefore();
System.out.println("IP是" + cbeConfig.getIp());
System.out.println("Port是" + cbeConfig.port);
}
}
此時(shí)再運(yùn)行g(shù)etCbeByAfterTest測試類,OK,搞定。
雖然搞定,但是我還是仍有心有疑慮,請(qǐng)有幸讀完的同志挑挑毛病。謝謝。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot啟動(dòng)后啟動(dòng)內(nèi)嵌瀏覽器的方法
- 通過代碼實(shí)例了解SpringBoot啟動(dòng)原理
- SPRING BOOT啟動(dòng)命令參數(shù)及源碼詳析
- SpringBoot打印啟動(dòng)時(shí)異常堆棧信息詳解
- 通過idea創(chuàng)建Spring Boot項(xiàng)目并配置啟動(dòng)過程圖解
- spring boot 監(jiān)聽容器啟動(dòng)代碼實(shí)例
- springboot 熱啟動(dòng)的過程圖解
- Spring Boot詳細(xì)打印啟動(dòng)時(shí)異常堆棧信息詳析
- springboot 場景啟動(dòng)器使用解析
相關(guān)文章
springboot整合SSE的項(xiàng)目實(shí)踐
SSE是一種可以主動(dòng)從服務(wù)端推送消息的技術(shù),本文主要介紹了springboot整合SSE的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
java?Map接口子類HashMap遍歷與LinkedHashMap詳解
這篇文章主要介紹了java?Map接口子類HashMap遍歷與LinkedHashMap詳解,Map接口下的集合與Collection接口下的集合,它們存儲(chǔ)數(shù)據(jù)的形式不同,感興趣的小伙伴可以參考下面文章詳細(xì)內(nèi)容介紹2022-06-06
springboot結(jié)合前端實(shí)現(xiàn)網(wǎng)頁跳轉(zhuǎn)功能實(shí)例
今天處理Springboot統(tǒng)一異常攔截的時(shí)候,遇到了頁面跳轉(zhuǎn)的問題,這篇文章主要給大家介紹了關(guān)于springboot結(jié)合前端實(shí)現(xiàn)網(wǎng)頁跳轉(zhuǎn)功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12
Eclipse的Debug調(diào)試技巧大全(總結(jié))
這篇文章主要介紹了Eclipse的Debug調(diào)試技巧大全(總結(jié)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12
Java中將多個(gè)PDF文件合并為一個(gè)PDF的方法步驟
這篇文章主要給大家介紹了關(guān)于Java中將多個(gè)PDF文件合并為一個(gè)PDF的方法步驟, Java PDF合并是指將多個(gè)PDF文件合并成一個(gè)PDF文件的過程,需要的朋友可以參考下2023-09-09
Java實(shí)現(xiàn)圖片旋轉(zhuǎn)、指定圖像大小和水平翻轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖像旋轉(zhuǎn),指定圖像大小,水平翻轉(zhuǎn)圖像,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
詳解用JWT對(duì)SpringCloud進(jìn)行認(rèn)證和鑒權(quán)
這篇文章主要介紹了詳解用JWT對(duì)SpringCloud進(jìn)行認(rèn)證和鑒權(quán),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

