반응형
[ 프로퍼티 우선순위 ]
- 유저 홈 디렉토리에 있는 spring-boot-dev-tools.properties
- 테스트에 있는 @TestPropertySource
- @SpringBootTest 애노테이션의 properties 애트리뷰트
- 커맨드 라인 아규먼트
- SPRING_APPLICATION_JSON (환경 변수 또는 시스템 프로티) 에 들어있는 프로퍼티
- ServletConfig 파라미터
- ServletContext 파라미터
- java:comp/env JNDI 애트리뷰트
- System.getProperties() 자바 시스템 프로퍼티
- OS 환경 변수
- RandomValuePropertySource
- JAR 밖에 있는 특정 프로파일용 application properties
- JAR 안에 있는 특정 프로파일용 application properties
- JAR 밖에 있는 application properties
- JAR 안에 있는 application properties
- @PropertySource
- 기본 프로퍼티 (SpringApplication.setDefaultProperties)
-> 높은게 낮은걸 오버라이딩 함
[ application.properties 파일 우선 순위 (높은게 낮은걸 덮어 씁니다.) ]
- file:./config/
- file:./
- classpath:/config/
- classpath:/
[ 15번 우선순위 - JAR 안에 있는 application properties ]
naeun.name = naeun
application.properties
@Component
public class SampleRunner implements ApplicationRunner {
@Value("${naeun.name}")
private String name;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("====================");
System.out.println(name);
System.out.println("====================");
}
}
SampleRunner.java
[ 4번 우선순위 - 커맨드라인 아규먼트 ]
mvn clean package
mvn package
java -jar target/spring-application-0.0.1-SNAPSHOT.jar --naeun.name=keesun
터미널
-> keesun으로 변경되어 찍힘
[ test 파일 안의 application.properties ]
@RunWith(SpringRunner.class)
@SpringBootTest
class ApplicationTests {
@Autowired
Environment environment;
// 스프링에 있는 Environment import 하
@Test
void contextLoads() {
assertThat(environment.getProperty("naeun.name"))
.isEqualTo("whiteship");
}
}
ApplicationTests
naeun.name = whiteship
test 파일 안의 application.properties
-> whiteship 이라고 찍힘
[ 2, 3번 우선순위 - 테스트에 있는 @TestPropertySource, @SpringBootTest 애노테이션의 properties 애트리뷰트 ]
@TestPropertySource(properties = "naeun.name=keesun3")
@SpringBootTest(properties = "naeun.name=keesun2")
* 너무 관리하기 힘들면 test.properties 라는 다른 이름의 프로퍼티스 생성해서 관리하면됨
[ random value 사용하기 ]
naeun.age = ${random.int}
[ 궁금한 점 ]
AssertThat 이 뭔지?
test 는 왜 있는지?
classpath 가 src?
반응형
'Programming > Spring' 카테고리의 다른 글
활용 5) 프로파일(profile) (0) | 2020.10.11 |
---|---|
활용 4) 외부 설정 : 타입 세이프 프로퍼티 @ConfigurationProperties (0) | 2020.10.11 |
기초 1) 스프링 빈(Bean) (0) | 2020.10.10 |
활용 2) SpringApplication 2 : 이벤트 리스너, ApplicationArguments (0) | 2020.10.10 |
활용 1) SpringApplication 1 : 커스터마이징, 배너 (0) | 2020.10.10 |