SpringBoot如何指定某些類優(yōu)先啟動
一、需求
1、項目中對某些IP地址和端口做了限制,只有寫在配置文件的內(nèi)容(ip)才可以訪問項目。
2、在進(jìn)行測試案例運行時保證讀取配置文件中ip和port的類(CbeConfig)得提前運行。
二、工作
1、如下的測試時肯定不行
@Test public void getCbeTest(){ CbeConfig cbeConfig = new CbeConfig(); System.out.println("IP是" + cbeConfig.getIp()); System.out.println("Port是" + cbeConfig.port); }
2、保證CbeConfig類在程序運行起來的那一刻先存在,先寫一個讀取配置的類,程序運行起來后讀取到配置后,然后再將讀取的參數(shù)設(shè)置到另一個類(CbeConfigAfter)中,以后提取參數(shù)。都使用CbeConfigAfter。
CbeConfigBefore類實現(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("我第一個啟動"); 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); } }
此時再運行g(shù)etCbeByAfterTest測試類,OK,搞定。
雖然搞定,但是我還是仍有心有疑慮,請有幸讀完的同志挑挑毛病。謝謝。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java輸入多個數(shù)據(jù)(不確定),排序,并求最大值的方法
今天小編就為大家分享一篇java輸入多個數(shù)據(jù)(不確定),排序,并求最大值的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07將List集合中的map對象轉(zhuǎn)為List<對象>形式實例代碼
這篇文章主要介紹了將List集合中的map對象轉(zhuǎn)為List<對象>形式實例代碼,具有一定借鑒價值,需要的朋友可以參考下2018-01-01ActiveMQ結(jié)合Spring收發(fā)消息的示例代碼
這篇文章主要介紹了ActiveMQ結(jié)合Spring收發(fā)消息的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-10-10JavaWeb dbutils執(zhí)行sql命令并遍歷結(jié)果集時不能查到內(nèi)容的原因分析
這篇文章主要介紹了JavaWeb dbutils執(zhí)行sql命令并遍歷結(jié)果集時不能查到內(nèi)容的原因分析及簡單處理方法,文中給大家介紹了javaweb中dbutils的使用,需要的朋友可以參考下2017-12-12SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案
這篇文章主要介紹了SpringBoot2.0整合SpringCloud Finchley @hystrixcommand注解找不到解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08基于servlet的執(zhí)行原理與生命周期(全面解析)
下面小編就為大家分享一篇servlet的執(zhí)行原理與生命周期全面解析,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12