Properties

μ„œλΉ„μŠ€ ν™˜κ²½μ— 따라 μ„œλ‘œ λ‹€λ₯Έ ν™˜κ²½ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜κ³ μž ν•  λ•Œ(dev, prod) μ‚¬μš©ν•˜λŠ” κΈ°λŠ₯μ—λŠ” Profileκ³Ό Propertyκ°€ μžˆλ‹€.

μ •μ˜

μŠ€ν”„λ§μ€ μ—¬λŸ¬ ν™˜κ²½ λ³€μˆ˜λ₯Ό ν¬ν•¨ν•œ μ™ΈλΆ€μ˜ νŒŒμΌμ„ μ΄μš©ν•˜μ—¬ μŠ€ν”„λ§ λΉˆμ„ μ„€μ •ν•˜λŠ” 방법을 μ œκ³΅ν•œλ‹€. 이 μ™ΈλΆ€μ˜ νŒŒμΌμ„ ν”„λ‘œνΌν‹° 파일이라고 ν•˜λ©°, properties ν™•μž₯자λ₯Ό κ°–λŠ”λ‹€.

μ‚¬μš©λ²•

ν”„λ‘œνΌν‹° νŒŒμΌμ€ λ‹€μŒκ³Ό 같은 ν˜•μ‹μœΌλ‘œ μž‘μ„±λœλ‹€.

ν”„λ‘œνΌν‹°λͺ…=κ°’
ν”„λ‘œνΌν‹°λͺ…=κ°’
...

이 ν”„λ‘œνΌν‹° 파일 λ‚΄μ˜ λ³€μˆ˜λ“€μ€ λ‹€μŒμ˜ 과정을 거쳐 μ‚¬μš©ν•  수 μžˆλ‹€.

  1. PropertySourcesPlaceholderConfigurer 빈 등둝

  2. @Value μ• λ…Έν…Œμ΄μ…˜μ„ 톡해 ν”„λ‘œνΌν‹° κ°’ μ‚¬μš©

1. PropertySourcesPlaceholderConfigurer 빈 등둝

@Configuration
public class Config {

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer configurer =
					new PropertySourcesPlaceholderConfigurer();
        configurer.setLocations(
                new ClassPathResource("application.properties"),
                new ClassPathResource("database.properties")
        );
        return configurer;
    }

}

PropertySourcesPlaceholderConfigurerλΉˆμ„ λ“±λ‘ν•˜λŠ” λ©”μ„œλ“œλŠ” λ°˜λ“œμ‹œ static으둜 μ„ μ–Έλ˜μ–΄μ•Ό ν•œλ‹€.

2. @Value μ• λ…Έν…Œμ΄μ…˜μ„ 톡해 ν”„λ‘œνΌν‹° κ°’ μ‚¬μš©

λ‹€μŒκ³Ό 같이 μ‚¬μš©ν•  수 μžˆλ‹€.

@Value μ–΄λ…Έν…Œμ΄μ…˜μ˜ νŒŒλΌλ―Έν„°λ‘œ ν”„λ‘œνΌν‹°μ˜ 이름을 ${} μ•ˆμ— λ„£μ–΄ μ „λ‹¬ν•˜λ©΄ ν•΄λ‹Ή μ–΄λ…Έν…Œμ΄μ…˜μ΄ μ‚¬μš©λœ λ³€μˆ˜μ— ν”„λ‘œνΌν‹°μ˜ 값이 ν• λ‹Ήλœλ‹€.

Last updated