1. Environment profile
ApplictionContext์์ ์ ๊ณตํ๋ ๋ ๋ค๋ฅธ ์ค์ํ ๊ธฐ๋ฅ
public interface ApplicationContext extends EnvironmentCapable, .. {}
ApplicationContext๋ EnvironmentCapable์ ์์ํ๊ณ ์๋ค.
๐คIoC์ปจํ ์ด๋์ ์ํฅ์ ์ฃผ๋ ์์ธ๋ค์๋ ๋ฌด์์ด ์์๊น?
DB์ ์์ ๋ณด, Server Port ๋ฑ์ ์ ๋ณด๊ฐ ์๋ค.
ํ๋์ ์์๋ฅผ ํตํด ์ดํด๋ณด์.
๊ฐ๋ฐ ์์๋ H2 Database๊ฐ ์ฌ์ฉ๋๋๋ก ๋น์ด ๋ฑ๋ก๋๋ค.
์ฌ์ฉํ๋ DataSource์ Connection ๋์์ด H2 Database์ธ ๊ฒ์ด๋ค.
์ด์ ์ค์๋ MySQL์ ์ฌ์ฉํ๋๋ก ๋ฐ๊ฟ์ผ ํ๋ค.
=> DB๊ฐ ๋ฐ๋๋ค.
=> ํ๊ฒฝ์ด ๋ฐ๋๋ค.
์คํ๋ง์ ํ๊ฒฝ์ Property์ Profile๋ก ์ ๊ณต๋๋ค.
Profile์ ๋ฐ๋ผ์ ํ๊ฒฝ์ด ๋ฐ๋๊ณ ๊ทธ์ ๋ฐ๋ผ Property๊ฐ ๋ฐ๋๋ค.
=> ํ๊ฒฝ์ ๋ฐ๋ผ์ Profile๊ณผ Property๊ฐ ๋ค๋ฅด๊ฒ ์ค์ ๋ Environment Object๊ฐ ์ฌ์ฉ๋๋ค.
์คํ๋ง์. properties์. yaml ํ์ผ, ๋ ๊ฐ์ง ํ์์ ์ฌ์ฉํ์ฌ Property๋ฅผ ๊ด๋ฆฌํ๋ค.
Property
key-value ํ์์ผ๋ก ๊ตฌ์ฑ๋๋ฉฐ, ๊ฐ๊ฐ์ ํค์ ๊ฐ์ ๋ฑํธ(=)๋ก ๊ตฌ๋ถ๋๋ค.
Propertyํ์ผ์ ์ค์ ํ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
1. ์ค์ ํ์ผ์ @PropertySource ์ ๋ ธํ ์ด์ ๋ถ์ด๊ธฐ
@PropertySource("application.properties")
public class AppConfiguration {
}
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
ConfigurableEnvironment environment = applicationContext.getEnvironment();
Integer property = environment.getProperty("kdt.minimum-order-amount", Integer.class);
applicationContext์ getEnvironment(). getProperty()๋ฅผ ํตํด์ ํ๋กํผํฐ ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์๋ค.
์์๋. properties์์ ์ง์ ํ key ๊ฐ์ ์์ฑํ๊ณ ํด๋น ๊ฐ์ ํ์ ์ ์ง์ ํ๊ณ ์ถ์ ๊ฒฝ์ฐ ๋ค์ ์ ์ด์ค๋ค.
2. ํน์ ํ๋์ @Value ์ ๋ ธํ ์ด์ ๋ถ์ด๊ธฐ
@Value ์ ๋ ธํ ์ด์ ์ ์ฃผ์ ํ ๊ฐ์ ์ง์ ํ ๋ ์ฌ์ฉํ๋ค.
์ฌ์ฉ ๋ฐฉ๋ฒ์ผ๋ก๋ 3๊ฐ์ง๊ฐ ์๋ค.
๐ ๋ณ์์ ์ง์ ์ฌ์ฉ
@Value("v1.1.1")
private String version;
"v1.1.1"์ด๋ผ๋ ๊ฐ์ version๋ณ์์ ์ฃผ์ ํ๋ค.
๐์คํ๋ง ํ๋กํผํฐ ์ฌ์ฉ
@Value("${kdt.version:v0.0.0}")
private String version;
kdt.version์ด๋ผ๋ ํ๋กํผํฐ ๊ฐ์ version ๋ณ์์ ์ฃผ์ ํ๋ค.
ํ๋กํผํฐ ๊ฐ์ ์คํ๋ง์ Environment ๊ฐ์ฒด์์ ๊ฐ์ ธ์จ๋ค.
๋ค์ ์ ์ :v0.0.0์ ์๋ฏธ๋ ํ๋กํผํฐ ๊ฐ์ด ์๋ ๊ฒฝ์ฐ ํด๋น ๊ฐ์ ์ฃผ์ ํ๋ค๋ ์๋ฏธ์ด๋ค.
Property Search Hierarchy
4, 5๋ฒ์ ๋ณด๋ฉด JVM์ ์์คํ ํ๋กํผํฐ๊ฐ ๋จผ์ ์ฒ๋ฆฌ๋๊ณ JVM ์์คํ ํ๊ฒฝ๋ณ์๊ฐ ์ฒ๋ฆฌ๋๋ ๊ฒ์ ์ ์ ์๋ค.
=> ๋ฒํธ๊ฐ ๋์์๋ก ์ฐ์ ์์๊ฐ ๋์
2. YAML๋ก ํ๋กํผํฐ ์์ฑ
YAML
๋ค์ฌ ์ฐ๊ธฐ์ ๊ณต๋ฐฑ์ ์ฌ์ฉํ์ฌ ๊ณ์ธต ๊ตฌ์กฐ๋ฅผ ํํํ๋ ํ์์ผ๋ก ์ฝ๋ก (:)๊ณผ ๋์(-)๋ฅผ ์ฌ์ฉํ์ฌ ๋งต๊ณผ ๋ฆฌ์คํธ๋ฅผ ํํํ๋ค.
db:
username: admin
password: secretpassword
server:
port: 8080
@ConfigurationProperties
์คํ๋ง๋ถํธ์์ ์ธ๋ถ ์์ฑ์ ํตํด์ ๋ณ๋์ ๋น์ ๋ง๋ค ์ ์๊ฒ @ConfigurationProperties ์ ๋
ธํ
์ด์
์ ์ง์ํ๋ค.
ํน์ ๊ทธ๋ฃน์ ์์ฑ์ ๋ชจ๋ธ๋งํ ์ ์๊ณ ๋น์ผ๋ก ๋ฑ๋กํด์ ์ฌ์ฉํ ์ ์๋ค.
3. Profile
ํน์ ํ๊ฒฝ ๋๋ ์กฐ๊ฑด์ ๋ฐ๋ผ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋์์ ๊ตฌ์ฑํ๋ ๋ฐฉ๋ฒ
๊ฐ๊ฐ์ ํ๋กํ์ผ์ ํน์ ํ ์ค์ ๊ฐ์ ๊ฐ์ง๊ณ ์๊ณ , ํด๋น ํ๋กํ์ผ์ด ํ์ฑํ๋๋ฉด ํด๋น ์ค์ ์ด ์ ์ฉ๋๋ค.
Spring ํ๋กํ์ผ์ ์ฌ์ฉํ๋ฉด ์๋ฅผ ๋ค์ด ๊ฐ๋ฐ ํ๊ฒฝ, ํ ์คํธ ํ๊ฒฝ, ์ด์ ํ๊ฒฝ ๋ฑ์์ ๊ฐ๊ฐ ๋ค๋ฅธ ์ค์ ์ ์ฌ์ฉํ ์ ์๋ค.
์ด๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ค์ํ ํ๊ฒฝ์์ ํ ์คํธํ๊ณ ๋ฐฐํฌํ ๋ ์ ์ฉํ๋ค.
Profile ์ค์ ๋ฐฉ๋ฒ
1. application.properties ๋๋ application.yaml ํ์ผ์ ์ด์ด์ spring.profiles.active ์์ฑ์ ์ค์ ํ๋ค.
spring.profiles.active=dev
...
+) application-dev.yaml, application-prod.yaml ํํ๋ก ํ์ผ ์ด๋ฆ์ ์์ฑํ๋ฉด ํด๋น ํ๋กํ์ผ์ ์ค์ ์ด ์ ์ฉ๋๋ค.
2. @Profile ์ ๋ ธํ ์ด์ ์ฌ์ฉ
@Component
@Profile("dev")
public class MyComponent {}
ํด๋์ค ๋ ๋ฒจ ๋๋ ๋ฉ์๋ ๋ ๋ฒจ์์ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ํด๋น ํ๋กํ์ผ์ด ํ์ฑํ๋ ๋๋ง ํด๋น ํด๋์ค ๋๋ ๋ฉ์๋๊ฐ ์ฌ์ฉ๋๋ค.
Intellij์์์ ๊ฐํธํ Profile ์ค์
1. Run/Debug Configuration์ Spring Boot Application์ค์ ์์ Active profile์ ์นธ์ ์ํ๋ profile์ ์ ๋ ฅํ๋ค.
2. Run/Debug Configuration์ Spring Boot Application์ค์ ์์ Profram arguments์ ์นธ์ ์ํ๋ profile์ ์ง์ ํ๋ ๋ช ๋ น์ด๋ฅผ ์ ๋ ฅํ๋ค.
--spring.profiles.active=dev
4. Resource
์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋ค๋ค ๋ณด๋ฉด ์ธ๋ถ ๋ฆฌ์์ค(์ด๋ฏธ์งํ์ผ, ํ ์คํธํ์ผ, ์๋ณตํธํ๋ฅผ ์ํ ํคํ์ผ)๋ฅผ ์ฝ์ ๋๊ฐ ์๋ค.
Resource์ ResourceLoader ์ธํฐํ์ด์ค๋ฅผ ํตํด์ ์ด์ ๋ํ ์ ๊ทผ์ ์ ๊ณตํ๋ค.
Resource
์ธํฐํ์ด์ค๋ org.springframework.core.io ํจํค์ง์ ์ ์๋์ด ์๋ค.
๊ฐ ๊ตฌํ์ฒด๋ ๋ค๋ฅธ ์ ํ์ ๋ฆฌ์์ค์ ์ ๊ทผํ๊ธฐ ์ํ ๋ฐฉ๋ฒ์ ์ ๊ณตํ๋ค.
๊ตฌํ์ฒด ์ข ๋ฅ
์์
Resource resource = applicationContext.getResource("classpath:application.yaml");
Resource resource1 = applicationContext.getResource("file:sample.txt");
'๐๏ธ ํ๋ก๊ทธ๋๋จธ์ค ๋ฐ๋ธ์ฝ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
SpringBoot Part2-1 (0) | 2023.06.27 |
---|---|
SpringBoot Part1-5 (0) | 2023.06.26 |
SpringBoot Part1-3 (0) | 2023.06.22 |
SpringBoot Part1-2 (0) | 2023.06.21 |
SpringBoot Part 1-1 (0) | 2023.06.21 |
๋๊ธ