외부설정파일(1)

오진현·2023년 3월 21일
0

Spring

목록 보기
4/7

외부 설정 파일 :설정 값들을 애플리케이션 안 또는 밖에 파일로 설정
● properties
● YAML
● 환경변수
● 커맨드 라인 아규먼트

ex)
Application.properties

dean.name = dean

이미 정의되어 있는 파일 안에 설정 하는 방법

@Value("${dean.name}") 과 같이 사용 할 수 있다.

설정 파일의 우선순위

  1. 유저 홈 디렉토리에 있는 spring-boot-dev-tools.properties
  2. 테스트에 있는 @TestPropertySource
  3. @SpringBootTest 애노테이션의 properties 애트리뷰트
  4. 커맨드 라인 아규먼트
  5. SPRING_APPLICATION_JSON (환경 변수 또는 시스템 프로티) 에 들어있는
    프로퍼티
  6. ServletConfig 파라미터
  7. ServletContext 파라미터
  8. java:comp/env JNDI 애트리뷰트
  9. System.getProperties() 자바 시스템 프로퍼티
  10. OS 환경 변수
  11. RandomValuePropertySource
  12. JAR 밖에 있는 특정 프로파일용 application properties
  13. JAR 안에 있는 특정 프로파일용 application properties
  14. JAR 밖에 있는 application properties
  15. JAR 안에 있는 application properties
  16. @PropertySource
  17. 기본 프로퍼티 (SpringApplication.setDefaultProperties)

application.properties 우선 순위 (높은게 낮은걸 덮어 씁니다.)
1. file:./config/
2. file:./
3. classpath:/config/
4. classpath:/

4순위 커맨드라인

java -jar target/deanoh-0.0.1-SNAPSHOT.jar --dean.name=dean

Test용 프로퍼티 정의

DeanohApplicationTest.class


@RunWith(value = SpringRunner.class)
@SpringBootTest
class DeanohApplicationTests {

	@Autowired
	Environment environment;

	@Test
	void contextLoads() {
		assertThat(environment.getProperty("dean.name"))
				.isEqualTo("dean");
	}

}

Test의 resources 파일 적용

프로퍼티에 랜덤값 주기

dean.name = dean
dean.age = ${random.int} // 랜덤 사용
server.port = 0 // 서버는 0

프로퍼티를 읽는 순서

src 에 프로퍼티 읽어서 빌드 후 test 에 프로퍼티를 오버라이드 한다.

테스트 프로퍼티 관리

src에 프로퍼티 생성.
@TestPropertySource(locations = "classpath:/test.propertise") 사용.

profile
s나야

0개의 댓글