一、需求
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,搞定。
雖然搞定,但是我還是仍有心有疑慮,請有幸讀完的同志挑挑毛病。謝謝。
三、補充總結
1、自定義文件不能使用.yml結尾
今天又再次嘗試,發現上面的方法太LOW了,其實可以直接自己定義一個person.properties。注意不能定義為person.yml文件,因為如果這樣定義,是無法加載到person.yml中的內容(親測過)。
person.name=xx
person.age=20
2、Bean的類
import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @Getter @Setter @PropertySource("classpath:defineConfig/person.properties") @ConfigurationProperties(prefix = "person") public class Person { String name; Integer age; }
3、使用這樣的方式去注入Person
@Autowired
Person person;
4、測試類
import cn.com.acca.ramsibe.config.authinfo.AirlineAuthInfo; import cn.com.acca.ramsibe.config.authinfo.AuthIpInfo; import cn.com.acca.ramsibe.config.authinfo.Person; import cn.com.acca.ramsibe.config.ibeconfig.IbeConfigInfo; import cn.com.acca.ramsibe.config.ibeconfig.IbeServer; import lombok.extern.java.Log; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest @Log public class IbeConfigTest { @Autowired Person person; @Test public void PersonTest() { System.out.println("年齡" + person.getAge()); } }
