TravelMate-KU / TravelMate-Server

0 stars 1 forks source link

πŸ”¨ Spring Boot μ—μ„œ properties κ°’ μ£Όμž…λ°›κΈ° #10

Open jaeuk520 opened 4 months ago

jaeuk520 commented 4 months ago

Update

@Value

placeholder 방식

SpEL 방식

문제점

νƒ€μž… μ•ˆμ •μ„±

external:
  value: true
@SpringBootTest
public class ValueProblemTest {
    @Value("${external.value}")
    private String stringValue;

    @Value("${external.value}")
    private boolean booelanValue;

    @Test
    void problemTest() {
        assertThat(stringValue).isEqualTo("true");
        assertThat(booelanValue).isEqualTo(true);
    }
}
// Test Success!

@ConfigurationProperties

external:
  record-year: 2020
  api:
    name: kakao
    key: 123123

Properties 클래슀 μ •μ˜

@Getter
@Setter
@Configuration
@ConfigurationProperties("external")
public class TypeSafeProperties {
    private String recordYear;
    private Api api;

    @Getter
    @Setter
    public static class Api {
        private String name;
        private Integer key;
    }
}

@Value도 λ˜‘κ°™μ΄ ν•  수 μžˆμ§€ μ•Šλ‚˜?

문제점

λΆˆλ³€μ„± (곡톡)

@ConstructorBinding

@Getter
@RequiredArgsConstructor
@ConstructorBinding
@ConfigurationProperties("external")
public final class ConstructorProperties {
    private final String recordYear;
    private final Api api;

    @Getter
    @RequiredArgsConstructor
    public static final class Api {
        private final String name;
        private final Integer key;
    }
}
@Configuration
@EnableConfigurationProperties(value = {ConstructorProperties.class})
public class PropertiesConfiguration {
}

@EnableConfigurationProperties의 ν•œκ³„μ 

@ConfigurationPropertiesScan

@Configuration
@ConfigurationPropertiesScan("com.mang.atdd.membership")
public class SpringBootApplication {
}

정리

Update

Improved @ConstructorBinding Detection

david-parkk commented 4 months ago

springboot 3 μ—μ„œ @ConsturctBinding λΆˆλ³€μ„±μ„ μœ„ν•œ @ConstructingBinding κ΄€λ ¨ 이슈 @ConstructingBinding deprecated

david-parkk commented 4 months ago
david-parkk commented 4 months ago
@ConfigurationProperties(prefix = "spring.security.oauth2.client.registration.google")
@Getter
@Setter
public class GoogleOAuthProperties {
    private String clientId;
    private String clientSecret;
}

intellij IDEA buildμ—λŠ” setterλŠ” μ •μƒμ μœΌλ‘œ λ™μž‘ν•˜λŠ”λ° μƒμ„±μž μ£Όμž…μ€ μ•ˆλ˜λ„€μš”