https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config
์ธ๋ถ ์ค์ ์ด๋?
์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฌ์ฉํ๋ ์ฌ๋ฌ ์ค์ ๊ฐ๋ค์ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋๋ ๋ฐ์๋ค๊ฐ ์ ์ํ ์ ์๋ ๊ธฐ๋ฅ
application.properties
๊ฐ์ฅ ์ค์ํ ์ค์ ํ์ผ์ application.properties ํ์ผ์ด๋ค.
spring boot๊ฐ ์๋์ผ๋ก ๋ก๋ฉํ๋ ํ์ผ ์ด๋ฆ์ผ๋ก, ๊ท์ฝ(convention)์ด๋ค.
key value ํํ๋ก ์ ์ํ๋ค. ๋ค ๋ฌธ์์ด์ด๋ค.
springboot intializr ์ ์๋์ผ๋ก ์์ฑ๋๋ ํ์ผ๋ก, ๋ฐ์ ์ฐ์ ์์์์ 15์์์ ํด๋นํ๋ค.
Spring Boot์ ์ธ๋ถ ์ค์ ์ข ๋ฅ
-
properties
-
YAML
-
ํ๊ฒฝ ๋ณ์
-
์ปค๋งจ๋ ๋ผ์ธ ์๊ท๋จผํธ
โก ํ๋กํผํฐ ์ฐ์ ์์
-
์ ์ ํ ๋๋ ํ ๋ฆฌ์ ์๋ spring-boot-dev-tools.properties
-
@TestPropertySource(locations="classpath:/test.properties") ใ ก> ๋ด๊ฐ ์ฐ๊ณ ์ ๋ฆฌํ ์ฝ๋ # , + ์๋์ ๊ฐ์ด properties ์์ฑ๋ ์ธ ์ ์๋ค.
-
@SpringBootTest(properties="jueun.name=jueun3") ใ ก> ๋ด๊ฐ ์ฐ๊ณ ์ ๋ฆฌํ ์ฝ๋ #
-
์ปค๋งจ๋ ๋ผ์ธ ์๊ท๋จผํธ ใ ก> java -jar target/demo-0.0.1-SNAPSHOT.jar --jueun.name=hoit1302
-
SPRING_APPLICATION_JSON (ํ๊ฒฝ ๋ณ์ ๋๋ ์์คํ ํ๋กํฐ) ์ ๋ค์ด์๋ ํ๋กํผํฐ
-
ServletConfig ํ๋ผ๋ฏธํฐ
-
ServletContext ํ๋ผ๋ฏธํฐ
-
java:comp/env JNDI ์ ํธ๋ฆฌ๋ทฐํธ
-
System.getProperties() ์๋ฐ ์์คํ ํ๋กํผํฐ
-
OS ํ๊ฒฝ ๋ณ์
-
RandomValuePropertySource
-
JAR ๋ฐ์ ์๋ ํน์ ํ๋กํ์ผ์ฉ application properties
-
JAR ์์ ์๋ ํน์ ํ๋กํ์ผ์ฉ application properties ใ ก> application-{profile}.properties #
-
JAR ๋ฐ์ ์๋ application properties
-
JAR ์์ ์๋ application properties ใ ก> springboot intializr ์ ์๋์ผ๋ก ์์ฑ๋๋ ํ์ผ
-
@PropertySource
-
๊ธฐ๋ณธ ํ๋กํผํฐ (SpringApplication.setDefaultProperties) ใ ก> ์๋ฌด๊ฒ๋ ์ ์ผ์ ๋
test/resources/application.properties ์์ ๋
testApplication์ ์คํ ์
๋น๋๋ฅผ ํ ๋ src ๋ฐ์ ์๋ ๊ฒ(src/main/java, src/main/resources)์ ๋น๋๋ฅผ ํด์ classpath์ ๋๊ณ ๊ทธ ๋ค์์ test ์ฝ๋๋ฅผ ๋น๋ํด์ classpath์ ๋๋๋ค. ๊ทธ ๋ application.properties๊ฐ test ํด๋ ๋ฐ์ ์๋ ๊ฒ์ผ๋ก ์ค๋ฒ๋ผ์ด๋ฉ๋๋ค.
์ด ์ฝ๋ฉ ๋ฐฉ์์ ํฐ ๋จ์ ์ด ์๋ค. #
test/resources/test.properties ์์ ๋ #
์ฐ์ ์์ 2์์์ธ @TestPropertySource(locations="classpath:/test.properties") ์ ์ฐ๊ฒฐ๋๋ ์ด์ผ๊ธฐ์ด๋ค.
test/resources์ application.properties๋ฅผ ์ง์ฐ๊ณ test.properties์ ์ผ๋ค.
๋น๋๋ฅผ ํ ๋ src ๋ฐ์ ์๋ ๊ฒ(src/main/java, src/main/resources)์ ๋น๋๋ฅผ ํด์ classpath์ ๋๊ณ
๊ทธ ๋ค์์ test ์ฝ๋๋ฅผ ๋น๋ํด์ classpath์ ๋๋๋ค.
๊ทธ ๋ application.properties์ test ํด๋ ๋ฐ์ ์๋ test.properties๋ ํ์ผ ๋ช
์ด ๋ค๋ฅด๊ธฐ ๋๋ฌธ์ ๋ ๋ค ์จ์ ํ ์๋ค.
๊ทธ๋ฐ๋ฐ ์คํํ ์ด ํ
์คํธ์์ @TestPropertySource(locations = "classpath:/test.properties")๋ก
์ถ๊ฐ๋ก test.properties์ ์ฌ์ฉํ๋ผ๊ณ ํ๋ค.
2์์๋ก ์ฐ์ ์์๊ฐ ๋๊ธฐ์ ๋์ผํ ํค๊ฐ์ด ์์ ๋๋ง, ์ค๋ฒ๋ผ์ด๋ฉํ๋ค.
๊ทธ๋์ main ๋ฐ application.properties ์ ์๋ JUEUN์ด ์๋, test.properties์ ์๋ jueun22๊ฐ ํ์ธ๋๋ค.
โก application.properties ์ฐ์ ์์

-
file:./config/
-
file:./
-
classpath:/config/
-
classpath:/
ํ๋กํผํฐ์ ๊ฐ์ ์ฐธ์กฐํ๋ @Value
@Component
public class SampleRunner implements ApplicationRunner {
@Value("${jueun.name}")
private String name;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("=====================");
System.out.println(name);
System.out.println("=====================");
}
}
SpringEL ์ ์ฌ์ฉํ ์ ์์ง๋ง ์ ์๋ ๋ฐ์์ ์ค๋ช ํ๋ Property Type Conversion, @Validated ๊ธฐ๋ฅ๋ค์ ์ ๋ถ ์ฌ์ฉ ๋ชปํ๋ค.
* ๋ณด์ถฉ
๋๋ค๊ฐ ์ค์ ํ๊ธฐ
-
${random.int}, ${random.double}
-
${random.int(0,100)} ใ ก> ๊ณต๋ฐฑ์ด ์์ผ๋ฉด ์๋ฌ๋๋ค.
-
์ฃผ์, ํฌํธ๋ฒํธ๋ 0์ผ๋ก ์ค์ ํ๋ ๊ฒ์ด ๋๋ค๊ฐ!
ํ๋ ์ด์ค ํ๋
-
jueun.id=hoit1302
-
jueun.email=${jueun.id}@ewhain.net
Command Line Properties false
-
SpringApplication.setAddCommandLineProperties(false)
test ํด๋์ resources ๋ง๋ค๊ธฐ
Project Structure > Module, Content Root์ text/resources ์๋ค๋ฉด ์ถ๊ฐํ๊ธฐ
@ConfigurationProperties
ํ์ -์ธ์ดํ ํ๋กํผํฐ
์ฌ๋ฌ ํ๋กํผํฐ๋ฅผ ๋ฌถ์ด์ ์ฝ์ด์ฌ ์ ์๋ค.
๋น์ผ๋ก ๋ฑ๋กํด์ ๋ค๋ฅธ ๋น์ ์ฃผ์ ํ ์ ์๋ค.
์๋ ConfigurationProperties์ ์ฌ์ฉํ๋ ค๋ฉด EnableConfigurationProperties(JueunProperties.class) ์ค์ ํด์ฃผ์ด์ผ ํ์ง๋ง ์ด๋ฏธ ๋ด๋ถ์ ์ผ๋ก @EnableConfigurationProperties ๋ฑ๋ก์ด ๋์ด ์๋ค.
์ฐ๋ฆฌ๊ฐ ํด์ผ ํ ์ผ์ @Component ๋๋ @Bean์ ๋ถ์ฌ์ฃผ๋ ์ผ์ด๋ค.
@Component
@Component
@ConfigurationProperties("jueun") // jueun์ด๋ผ๋ ํค ๊ฐ์ผ๋ก ํ๋กํผํฐ ์ฌ์ฉ. getter/setter ํ์
public class JueunProperties {
private String name;
private int age;
private String fullName;
// getter/setter ์์ฑ
}
jueun.name=JUEUN
jueun.age=${random.int(0,100)}
jueun.fullName=${jueun.name} Park
@Component
public class SampleRunner implements ApplicationRunner {
@Autowired
JueunProperties jueunProperties;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("=====================");
System.out.println(jueunProperties.getName());
System.out.println(jueunProperties.getAge());
System.out.println(jueunProperties.getFullName());
System.out.println("=====================");
}
}
@Bean
Third-party Configuration ์ ์ฌ์ฉํ๋ค.
ํํ๊ฒ ์ฐ์ด๋ case๋ ์๋๋ค.
@SpringBootApplication
public class Application {
// Third-party Configuration
// ์ด ์ ํ๋ฆฌ์ผ์ด์
์์ ์๋ ๊ฒ์ด ์๋๋ผ JAR ํ์ผ ์์ ์๊ฑฐ๋
// ์คํ๋ง์ด ์ ๊ณตํ๋ ํ๋กํผํฐํด๋์ค๋ @Component๋ฅผ ๋ชป ๋ถ์ด๋๊น @Bean์ผ๋ก.
@ConfigurationProperties("server")
@Bean
public ServerProperties serverProperties(){
return new ServerProperties();
}
public static void main(String[] args) {
SpringApplication app = new SpringApplication(Application.class);
app.setWebApplicationType(WebApplicationType.NONE);
app.run(args);
}
}
Relaxed Binding
- context-path
- context_path
- contextPath
- CONTEXTPATH
Property Type Conversion
@DurationUnit์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
@DurationUnit(ChronoUnit.SECONDS)
private Duration sessionTimeout = Duration.ofSeconds(30);
jueun.sessionTimeout=25
@DurationUnit์ ์ฌ์ฉํ์ง ์๊ณ suffix๋ก ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ
private Duration sessionTimeout = Duration.ofSeconds(30);
jueun.sessionTimeout=25s
@Validated
<!-- dependency ์ถ๊ฐ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
JSR-303 ( @NotNull, @NotEmpty, @Size(min=0, max=100) )
@Component
@ConfigurationProperties("jueun") // jueun์ด๋ผ๋ ํค ๊ฐ์ผ๋ก ํ๋กํผํฐ ์ฌ์ฉ. getter/setter ํ์
@Validated // Properties ์ ๋ค์ด์ค๋ ๊ฐ๋ค์ ๊ฒ์ฆํ ์ ์๋ค.
public class JueunProperties {
@NotEmpty
private String name;
}
๋น์ด์๋๋ก ์ค์ ํด์ฃผ๋ฉด
jueun.name=
Failure Analyzer๋ก ์์๊ฒ ์ค๋ฅ๋ฅผ ์ถ๋ ฅํด์ค๋ค.

'Springboot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[spring boot] logging ์ปค์คํฐ๋ง์ด์ง (0) | 2021.01.08 |
---|---|
[spring boot] logging ๊ธฐ๋ณธ ์ค์ (0) | 2021.01.08 |
@Profile (0) | 2021.01.08 |
SpringApplication (0) | 2021.01.06 |
์คํ๋ง๋ถํธ, ๋ ๋ฆฝ์ ์ผ๋ก ์คํ ๊ฐ๋ฅํ JAR (0) | 2021.01.06 |
์คํ๋ง ๋ถํธ ํฌํธ ๋ฒํธ ๋ณ๊ฒฝ, ๋๋คํฌํธ, ํฌํธ ๋ฒํธ ํ์ธํ๊ธฐ (0) | 2021.01.05 |