마이크로 프레임 워크 스프링 부팅은 최근 매우 인기가 있습니다. 저자는 또한 트렌드를 따르고 더 이상 아무 말도하지 않았다. 이제 구성 파일을 읽는 예를 제시하겠습니다.
먼저 POM 파일의 다음 JAR 패키지에 의존해야합니다.
<pectionies> <pectinement> <groupId> org.springframework.boot </groupid> <artifactid> Spring-Boot-Starter-web </artifactid> </dependency> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> test> </종속성> </종속성>
둘째, 스프링 부츠 스타트 업 클래스가 필요합니다
@SpringBootApplication @EnableConfigurationProperties ({propsconfig.class, ymlconfig.class}) public class readApplication {public static void main (String [] args) {springApplication.run (readApplication.class, args); }}맞습니다. PropsConfig.class 및 ymlConfig.class는 @EnableConFigurationProperties 주석에 각각 지적 된 소품 및 YML 구성 파일을 읽는 클래스입니다. 다음으로 읽기 속성 및 YML 구성 파일의 특정 구현을 각각 구현합니다.
1. 속성 구성 파일을 읽습니다
classPath 아래에 Application.Properties 파일을 다음과 같이 일반 내용으로 배치하십시오.
Mas Mas Master.ds.MaxOpenPreparedStatements = 100 Mas
소품 구성 클래스를 읽는 것은 매우 간단합니다. 기본적으로 pojo/vo 클래스이며 클래스에 @ConfigurationProperties 주석을로드하십시오.
@configurationProperties (prefix = "mas 개인 문자열 URL; 개인 문자열 사용자 이름; 개인 문자열 비밀번호; 개인 문자열 필터; 개인 문자열 maxactive; 개인 문자열 이니셜 크기; 개인 문자열 Maxwait; 공개 문자열 getDriverClassName () {return driver className; } public void setDriverClassName (String DriverClassName) {this.driverClassName = DriverClassName; } public String getUrl () {return url; } public void seturl (String URL) {this.url = url; } public String getUserName () {return username; } public void setusername (String username) {this.username = username; } public String getPassword () {return password; } public void setpassword (문자열) 비밀번호) {this.password = password; } public String getFilters () {리턴 필터; } public void setFilters (문자열 필터) {this.filters = 필터; } public String getMaxActive () {return maxactive; } public void setMaxActive (String maxactive) {this.maxActive = maxActive; } public String getInitialSize () {return initialsize; } public void setInitialSize (문자열 이니셜 크기) {this.initialSize = 이니셜 크기; } public String getMaxWait () {return maxWait; } public void setMaxwait (String Maxwait) {this.maxwait = maxwait; }} 단위 테스트 클래스
@runwith (springJunit4classRunner.class) @SpringBootTest (classs = readApplication.class) 공개 클래스 readApplicationPropStests {@autowired private propsconfig propsconfig; @Test public void testDisplayPropSvalue () {String driverclassName = propsConfig.getDriverClassName (); 문자열 url = propsconfig.geturl (); 문자열 username = propsconfig.getusername (); 문자열 암호 = propsconfig.getPassword (); 문자열 필터 = propsconfig.getFilters (); 문자열 maxactive = propsconfig.getMaxActive (); 문자열 이니셜 크기 = propsconfig.getInitialSize (); 문자열 maxwait = propsconfig.getmaxwait (); System.out.println ( "DriverClassName->" + DriverClassName); System.out.println ( "url->" + url); System.out.println ( "username->" + username); System.out.println ( "password->" + password); System.out.println ( "이니셜 크기 ->" + 이니셜 크기); System.out.println ( "Maxwait->" + Maxwait); }}콘솔에서 테스트 컨텐츠 출력을 볼 수 있습니다.
DriverClassName-> com.mysql.jdbc.driver url-> jdbc : mysql : // localhost : 3306/테스트 사용자 이름 -> 루트 암호 -> 루트 이니셜 크기 -> 1 maxwait -> 60000
2. YML 구성 파일을 읽으십시오
ClassPath 아래에 Application.yml 파일을 다음과 같이 일반 내용으로 배치하십시오.
MyProps : #custom 속성 및 값 SimpleProp : SimplePropvalue ArrayProps : 1,2,3,4,5 ListProp1 : - 이름 : ABC 값 : abcvalue- 이름 : efg 값 : efgvalue listprop2 : - config2value1- config2vavlue2 mapProps : value1 : value1
YML 구성 파일의 클래스를 읽으십시오.
@configurationProperties (prefix = "myProps") // application.yml public class ymlconfig {private String simpleProp; 개인 문자열 [] ArrayProps; 개인 목록 <map <string, String >> listProp1 = new ArrayList <> (); // prop1 개인 목록에서 속성 값을 수신합니다. listprop2 = new ArrayList <> (); // prop2 private map <string, string> mapProps = new Hashmap <> ()에서 속성 값을 수신합니다. // prop1에서 속성 값을받습니다 public string getImpleProp () {return simpleProp; } public void setSimpleProp (String SimpleProp) {this.simpleProp = SimpleProp; } public list <map <string, String >> getListProp1 () {return listProp1; } public list <string> getListProp2 () {return listProp2; } public String [] getArrayProps () {return arrayProps; } public void setArrayProps (String [] arrayProps) {this.arrayProps = ArrayProps; } public map <string, String> getMactrops () {return mapProps; } public void setMactrops (map <string, String> mapProps) {this.mactrops = mapProps; }} 단위 테스트 클래스
@runwith (springJunit4classRunner.class) @SpringBootTest (classs = readApplication.class) 공개 클래스 readApplicationymltests {@autowired private ymlconfig ymlconfig; @test public void testdisplayymlvalue ()는 jsonProcessingException {system.out.println ( "SimpleProp :" + ymlConfig.getSimpleProp ()); ObjectMapper ObjectMapper = new ObjectMapper (); System.out.println ( "ArrayProps :" + ObjectMapper.writeValueAsString (ymlConfig.getArrayProps ())); System.out.println ( "ListProp1 :" + ObjectMapper.writeValueAsstring (ymlConfig.getListProp1 ())); System.out.println ( "ListProp2 :" + ObjectMapper.writeValueAsString (ymlConfig.getListProp2 ())); System.out.println ( "MapProps :" + ObjectMapper.writeValueAsString (ymlConfig.getMactrops ())); }} 콘솔에서 테스트 컨텐츠 출력을 볼 수 있습니다.
SimpleProp : SimplePropvalue ArrayProps : [ "1", "2", "3", "4", "5"] ListProp1 : [{ "name": "abc", "value": "abcvalue"}, { "name": "efg", "value": "efgvalue"}] listprop2 : [ "config2value1", "config2vavlue2"] mapProps : { "key1": "value1", "key2": "value2"} 놀랍지 않나요? Spring의 ApplicationContext.xml 파일없이 부드럽게 실행될 수 있습니다.
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.