SpringBoot如何指定某些類優(yōu)先啟動(dòng)
一、需求
1、項(xiàng)目中對(duì)某些IP地址和端口做了限制,只有寫在配置文件的內(nèi)容(ip)才可以訪問項(xiàng)目。
2、在進(jìn)行測(cè)試案例運(yùn)行時(shí)保證讀取配置文件中ip和port的類(CbeConfig)得提前運(yùn)行。
二、工作
1、如下的測(cè)試時(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)行起來(lái)的那一刻先存在,先寫一個(gè)讀取配置的類,程序運(yùn)行起來(lái)后讀取到配置后,然后再將讀取的參數(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;
}
}
測(cè)試類
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測(cè)試類,OK,搞定。
雖然搞定,但是我還是仍有心有疑慮,請(qǐng)有幸讀完的同志挑挑毛病。謝謝。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot啟動(dòng)后啟動(dòng)內(nèi)嵌瀏覽器的方法
- 通過(guò)代碼實(shí)例了解SpringBoot啟動(dòng)原理
- SPRING BOOT啟動(dòng)命令參數(shù)及源碼詳析
- SpringBoot打印啟動(dòng)時(shí)異常堆棧信息詳解
- 通過(guò)idea創(chuàng)建Spring Boot項(xiàng)目并配置啟動(dòng)過(guò)程圖解
- spring boot 監(jiān)聽容器啟動(dòng)代碼實(shí)例
- springboot 熱啟動(dòng)的過(guò)程圖解
- Spring Boot詳細(xì)打印啟動(dòng)時(shí)異常堆棧信息詳析
- springboot 場(chǎng)景啟動(dòng)器使用解析
相關(guān)文章
java輸入多個(gè)數(shù)據(jù)(不確定),排序,并求最大值的方法
今天小編就為大家分享一篇java輸入多個(gè)數(shù)據(jù)(不確定),排序,并求最大值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
將List集合中的map對(duì)象轉(zhuǎn)為L(zhǎng)ist<對(duì)象>形式實(shí)例代碼
這篇文章主要介紹了將List集合中的map對(duì)象轉(zhuǎn)為L(zhǎng)ist<對(duì)象>形式實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
ActiveMQ結(jié)合Spring收發(fā)消息的示例代碼
這篇文章主要介紹了ActiveMQ結(jié)合Spring收發(fā)消息的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
JavaWeb dbutils執(zhí)行sql命令并遍歷結(jié)果集時(shí)不能查到內(nèi)容的原因分析
這篇文章主要介紹了JavaWeb dbutils執(zhí)行sql命令并遍歷結(jié)果集時(shí)不能查到內(nèi)容的原因分析及簡(jiǎn)單處理方法,文中給大家介紹了javaweb中dbutils的使用,需要的朋友可以參考下2017-12-12
SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案
這篇文章主要介紹了SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
基于servlet的執(zhí)行原理與生命周期(全面解析)
下面小編就為大家分享一篇servlet的執(zhí)行原理與生命周期全面解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12

