๐ 1. ํ๊ฒฝ ๋ณ์๋?
YAML ๋ณ์, ํ๋กํผํฐ ๋ณ์, ์ค์ ๋ณ์ ๋ผ๊ณ ๋ ๋ถ๋ฅธ๋ค.
ํ๊ฒฝ ๋ณ์๋ ์ ํ๋ฆฌ์ผ์ด์ ์คํ ์ ํ์ํ ์ค์ ๊ฐ์ ์๋ฏธํ๋ค.
์ด๋ฅผ ํตํด ์ฝ๋ ์์ ์์ด ํ๊ฒฝ์ ๋ณ๊ฒฝํ ์ ์๊ณ , ์ ์ง๋ณด์๋ ์ฌ์์ง๋ค!
๐ 2. ํ๊ฒฝ ๋ณ์ ์ค์ ํ๊ธฐ
Spring Boot์์๋ ํ๊ฒฝ ๋ณ์๋ฅผ application.yml ๋๋ application.properties์ ์ ์ฅํ๋ค.
โก๏ธ ์ด ๊ฐ๋ค์ @Value๋ @ConfigurationProperties๋ฅผ ์ฌ์ฉํด์ ์ฝ๋์์ ๊ฐ์ ธ์ฌ ์ ์๋ค!
๐ YAML ์ฌ์ฉ (application.yml)
atchfile:
upload:
path: /upload/files
๐ Properties ์ฌ์ฉ (application.properties)
atchfile.upload.path=/upload/files
๐ 3. @Value
@Value ์ ๋ํ ์ด์ ์ ์ฌ์ฉํ๋ฉด ํ๊ฒฝ ๋ณ์ ๊ฐ์ ์ฃผ์ ๋ฐ๋๋ค.
@Value("${atchfile.upload.path}")
private String atchfileUploadPath;
โ ๏ธ ํ์ง๋ง ํ๊ฒฝ ๋ณ์๊ฐ ๋ง์์ง๋ฉด @Value๋ก ํ๋์ฉ ์ฃผ์ ๋ฐ๋ ๋ฐฉ์์ ๊ด๋ฆฌ๊ฐ ์ด๋ ต๋ค!
๐ 4. @ConfigurationProperties
ํ๊ฒฝ ๋ณ์๊ฐ ๋ง์์ง๋ฉด, @Value๋ณด๋ค๋ ์ ์ฉ ํด๋์ค๋ฅผ ๋ง๋ค์ด์ ํ ๋ฒ์ ๊ด๋ฆฌํ๋ ๊ฒ ์ข์!
1๏ธโฃ ์ค์ ํด๋์ค ๋ง๋ค๊ธฐ
@Configuration
@ConfigurationProperties(prefix = "atchfile.upload")
@Getter
@Setter
public class AtchFileConfig {
private String path;
}
2๏ธโฃ ํ๊ฒฝ ๋ณ์ ๊ฐ ์๋ ์ฃผ์
@Service
public class FileService {
private final AtchFileConfig atchFileConfig;
public FileService(AtchFileConfig atchFileConfig) {
this.atchFileConfig = atchFileConfig;
}
public void printPath() {
System.out.println("Upload Path: " + atchFileConfig.getPath());
}
}
โก๏ธ ์ด๋ ๊ฒ ํ๋ฉด atchFileConfig.getPath()๋ฅผ ํธ์ถํ์ฌ ํ๊ฒฝ ๋ณ์ ๊ฐ์ ์ฝ๊ฒ ๊ฐ์ ธ์ฌ ์ ์๋ค!
๐ฅ @Value vs @ConfigurationProperties
๋น๊ต ํญ๋ชฉ | @Value | @ConfigurationProperties |
์ฌ์ฉ ๋ฐฉ์ | ๊ฐ๋ณ ๋ณ์ ์ฃผ์ | ์ค์ ํด๋์ค๋ก ๊ด๋ฆฌ |
ํ๊ฒฝ ๋ณ์ ๊ฐ์ | ์ ์ ๋ ์ ํฉ | ๋ง์ ๋ ์ ํฉ |
์ ์ง๋ณด์์ฑ | ๊ด๋ฆฌ ์ด๋ ค์ | ์ ์ง๋ณด์ ํธ๋ฆฌ |
์ ์ฐ์ฑ | ํ์ ๋ณํ ๋ถํธ | ์๋ ๋ณํ ์ง์ |
๐ ๊ฒฐ๋ก
- ํ๊ฒฝ ๋ณ์๊ฐ ์ ์ ๋ → @Value ์ฌ์ฉ
- ํ๊ฒฝ ๋ณ์๊ฐ ๋ง์ ๋ → @ConfigurationProperties ์ฌ์ฉ (โ ์ถ์ฒ!)
'Backend > spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[error] java.lang.IllegalArgumentException: Name for argument of type [int] not specified (0) | 2025.02.23 |
---|---|
[error] getOutputStream() has already been called for this response (0) | 2025.02.23 |
[JAVA] ์ฟ ํค(Cookie) ์ค์ ๋ฐฉ๋ฒ (0) | 2025.02.23 |
[Spring Security] CSRF & CORS ๊ฐ๋ ๋ฐ ์ค์ ๋ฐฉ๋ฒ (0) | 2025.02.23 |
[error] conversionServicePostProcessor Bean ์ค๋ณต ์ค๋ฅ (0) | 2025.02.23 |