1. 구성 문서 호출
시작 후 브라우저에서 http : // localhost : 18080/user/test를 직접 입력 하고 구성 파일에서 구성 내용을 인쇄하십시오.
2. 객체 Bean 호출을 바인딩합니다
때로는 속성이 너무 많아서 속성 필드에 하나씩 바인딩하기에는 너무 피곤합니다. 공무원은 물체 콩을 묶는 것을지지합니다. 여기서 우리는 configbean.java 클래스를 만듭니다. 상단에서는 주석 @configurationProperties (prefix = "com")를 사용하여 어느 것을 사용할 것인지 표시해야합니다.
@ConfigurationProperties (prefix = "com") public class configbean {private String name; 개인 문자열 ID; // getter and setter를 생략}구성 후 @enableconfigurationProperties를 스프링 부트 엔트리 클래스에 추가하고로드 할 콩을 지정해야합니다. configbean.class를 쓰지 않으면 Bean 클래스에 추가하십시오.
@springbootApplication@enableConfigurationProperties ({configbean.class}) public class 챕터 2Application {public static void main (String [] args) {springApplication.run (chapter2application.class, args); }}마지막으로 다음과 같이 컨트롤러에서 Configbean을 사용할 수 있습니다.
@RestControllerPublic Class USERCONTROLLER {@autowired Configbean Configbean; @requestmapping ( "/") public String hexo () {return configbean.getName ()+configBean.getId (); }} 3. 매개 변수 사이의 참조
다음 설정과 마찬가지로 Application.Properties에서 각 매개 변수를 직접 참조 할 수도 있습니다.
com.name = "zhang san"com.id = "8"com.psrinfo = $ {com.name} 번호 $ {com.id}로 번호가 매겨집니다.이런 식으로 PSRINFO 속성 만 사용할 수 있습니다.
4. 새로 작성된 구성 파일을 사용하십시오
우리는 다음과 같이 새로운 Bean 클래스를 만듭니다.
@configuration @configurationProperties (prefix = "com.md") @propertySource ( "classPath : test.Properties") public class configtestbean {private String name; 개인 문자열 원함; // getter and setter를 생략} 가장 중요한 것은 주석을 추가하는 것입니다 :@PropertySource("classpath:test.properties")
5. 구성 파일 우선 순위
application.properties 및 application.yml 파일은 4 개의 위치에 배치 할 수 있습니다.
마찬가지로,이 목록은 우선 순위에 따라 정렬됩니다.
또한 동일한 우선 순위 위치에 Application.Properties 및 Application.YML이 있으면 Application.YML의 속성이 Application.Properties의 속성을 무시합니다.
추신 : springboot의 application.properties 파일을 살펴 보겠습니다.
SpringBoot는 Application.Properties 파일을 읽습니다. 일반적으로 3 가지 방법이 있습니다
1. @Value 예 : 예를 들어 :
@Value("${spring.profiles.active}")private String profileActive;----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------2. @configurationProperties 예를 들어 :
@component@configurationProperties (locations = "classpath : application.properties", prefix = "test") public class testProperties {String URL; String key;}다른 클래스에서 사용되면 TestProperties를 직접 주입하여 관련 값에 액세스 할 수 있습니다.
3. 예를 들어 환경 사용 :
개인 환경 Env; Env.GetProperty ( "test.url");
ENV 방법은 덜 효율적입니다
참고 : @ConfigurationProperties는 위치가 지정되는 한 다른 .properties 파일에서도 사용할 수 있습니다.
요약
위의 것은 Spring Boot 구성 파일 응용 프로그램을 사용하는 것입니다. 편집기가 소개했습니다. 모든 사람에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 모든 사람에게 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!