您好,登錄后才能下訂單哦!
小編給大家分享一下SpringBoot指定某些類優先啟動的實現方法,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
一、需求
1、項目中對某些IP地址和端口做了限制,只有寫在配置文件的內容(ip)才可以訪問項目。
2、在進行測試案例運行時保證讀取配置文件中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類在程序運行起來的那一刻先存在,先寫一個讀取配置的類,程序運行起來后讀取到配置后,然后再將讀取的參數設置到另一個類(CbeConfigAfter)中,以后提取參數。都使用CbeConfigAfter。
CbeConfigBefore類實現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("設置完畢"); } 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); } }
此時再運行getCbeByAfterTest測試類,OK,搞定。
雖然搞定,但是我還是仍有心有疑慮,請有幸讀完的同志挑挑毛病。謝謝。
看完了這篇文章,相信你對“SpringBoot指定某些類優先啟動的實現方法”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。