Open jaeuk520 opened 4 months ago
springboot 3 μμ @ConsturctBinding λΆλ³μ±μ μν @ConstructingBinding κ΄λ ¨ μ΄μ @ConstructingBinding deprecated
@ConfigurationProperties
μ μΈν λ³λμ @ConstructorBinding
μ μ μΈν νμμμ΄ μμ±μ μ£Όμ
μ΄ κ°λ₯νλ€λ λ΄μ©μ
λλ€.λ§μ½ @ConstructorBinding
μ λͺ
μνκ³ μ νλ κ²½μ° ν΄λΉ μμ±μμ μ μΈν μ μμ΅λλ€.
@ConfigurationProperties(prefix = "spring.security.oauth2.client.registration.google")
@Getter
public class GoogleOAuthProperties {
private final String clientId;
private final String clientSecret;
@ConstructorBinding
public GoogleOAuthProperties(String clientId, String clientSecret) {
this.clientId = clientId;
this.clientSecret = clientSecret;
}
}
java.lang.IllegalStateException
κ° λ°μνλλ° buildλ₯Ό gradleλ‘ λ°κΏμ£ΌμΈμ. λͺμκ° μ½μ§νλ€μ@ConfigurationProperties(prefix = "spring.security.oauth2.client.registration.google")
@Getter
@Setter
public class GoogleOAuthProperties {
private String clientId;
private String clientSecret;
}
intellij IDEA buildμλ setterλ μ μμ μΌλ‘ λμνλλ° μμ±μ μ£Όμ μ μλλ€μ
Update
deprecated in@ConstructorBinding
Spring Boot 3.0
@Value
@Value
μ΄λ Έν μ΄μ μ κ°μ κ°λ¦¬ν€κ³ μλplaceholder
λ₯Ό λͺ μν΄μ£Όκ±°λSpEL
λͺ μplaceholder λ°©μ
${app.name}
μ κ°μ΄${}
λ΄λΆμ κ°μ μμΉλ₯Ό μ μ΄μ@Value
μ κ°μ μ£Όμ νλ λ°©μ@Value("${external.api.name}") private String apiName;
@Value("${external.api.key}") private Integer apiKey; }
SpEL λ°©μ
SpEL
μSpring Expression Language
μ μ½μμ΄λ€.@Value
μ κ°μ μ£Όμ ν μ μλ€.placeholder
λ°©μκ³Όλ λ€λ₯΄κ² λ€λ₯ΈSpring Bean
μ μ°Έμ‘°ν΄μ κ°μ μ£Όμ ν μ μλ€.@Value("#{1 eq 1}") private boolean spelBoolean;
@Value("#{externalService.apiName eq 'kakao'}") private String spelNameString;
@Test void spelTest() { assertThat(spelBoolean).isTrue(); assertThat(spelNameString).isEqualTo("true"); } }
λ¬Έμ μ
@Value
λ°©μμλ μνκΉκ²λ λ¬Έμ μ μ΄ μλ€.true
κ°μ΄String
μΌλ‘λBoolean
μΌλ‘λ μ¬μ©λλ κ²μ νμΈν μ μλ€.νμ μμ μ±
boolean
κ°μΌλ‘ μ¬μ©λ¬Έμ μ
value
λ₯ΌString
μΌλ‘λboolean
μΌλ‘λ μ¬μ©ν μ μλ€..ymlμ μ€μ ν΄λ κ°μ μλ‘ λ€λ₯Έ ν΄λμ€μμ
@Value
λ₯Ό μ΄μ©ν΄μ μ°λ°μ μΌλ‘ μ¬μ©νλ€λ©΄?boolean
μΌλ‘ κ°μ Έλ€ μ΄λ€λ©΄ λ€νString
μΌλ‘ λΆλ¬λ€ μ°λ μ€μλ₯Ό λ²νμ§ μμ κ²μ΄λΌλ νμ X@ConfigurationProperties
Properties ν΄λμ€ μ μ
@ConfigurationProperties
μvalue
λ‘ prefixλ₯Ό μ μ΄μ€μΌ νλ€.Setterλ₯Ό λ°λμ μ μν΄μ£Όμ΄μΌ νλ€.
Caused by: java.lang.IllegalStateException: No setter found for property
μ΄ λ°μνλ€.@Value
λ λκ°μ΄ ν μ μμ§ μλ?κ°λ₯νλ€. νμ§λ§ λΆνΈνλ€!
@Value
μ κ³μν΄μ μ€λ³΅λ κ°(external.api)λ₯Ό μ μ΄μ€μΌ νλ λΆνΈν¨μ΄ μκ³ μ€μ²© ν΄λμ€λ₯ΌBean
μΌλ‘ μμ±ν΄μ μμ‘΄μ± μ£Όμ μ ν΄μ€μΌ νλ€.@Value
μμλSnake case
λ‘ μμ±λ κ°λ€μ ν μ¨ νλ νλ¦¬μ§ μκ³ μ μ΄μ€μΌ νμ§λ§@ConfigurationProperties
λ₯Ό μ¬μ©νλ©΄Camel case
λ‘ μμ±ν λ³μλ₯Ό μ°Ύμ μμμ μ£Όμ ν΄μ€λ€.λ¬Έμ μ
λΆλ³μ± (곡ν΅)
λΆλ³μ΄ μλλΌλ λ¬Έμ μ μ΄ μλ€!
final
ν νλλ‘ μΈμ€ν΄μ€ λ³μλ₯Ό μμ±ν μ μκΈ° λλ¬Έμ λΆλ³μ±μ μ μ§ν μκ° μλ€.μ¬μ§μ΄
@ConfigurationProperties
λ κ°λ°μ μ μ₯μμ λΆνμνsetter
κ° κ³΅κ° λμ΄ μμ΄ μ€κ°μ κ°μ΄ λ³κ²½λ μνμ±μ΄ ν¬κ² λ¨μμλ€.@ConstructorBinding
λΆλ³μ± λ¬Έμ λ₯Ό ν΄κ²°νκ³ μ
Spring Boot 2.3
μ΄ν μμ±μ μ£Όμ λ°©μμΌλ‘ λΆλ³μ±μ κ°μ§κ³ Properties νμΌμ λ§λ€ μ μλ λ°©μμ΄ μΆκ°λμλ€.final νλμ λν΄ κ°μ μ£Όμ ν΄μ€λ€!
final
νλ λν μλμΌλ‘ κ°μ μ£Όμ νλ λμμ΄ λλ€.final
ν€μλλ₯Ό λͺ μνμ§ μλλ€λ©΄setter
λ₯Ό μ΄μ©ν΄μ κ°μ binding νλ €νκΈ° λλ¬Έμsetter
κ° μλ€λ exceptionμ΄ λ°μνλ€.μ λ°©μμ μ¬μ©νλ©΄ Properties ν΄λμ€μ μ§μ μ μΌλ‘
@Configuration
μ μ΄μ©ν΄μ μ§μ μ μΌλ‘Spring Bean
μΌλ‘ λ§λ€μ΄μ£Όμ§ μλλ€.λμ PropertiesConfiguration ν΄λμ€μ
@EnableConfigurationProperties
μ μ΄μ©ν΄μ μμ±ν Properties ν΄λμ€μ ν΄λμ€ νμ μ λͺ μν΄μ£Όλ©΄Spring Bean
μΌλ‘ λ±λ‘λλ€.@ConstructorBinding
μ μ΄μ©νμ¬ λΆνμνsetter
λ₯Ό μ¬μ©νμ§ μκ² λλ©΄μ λΆλ³μ±μ μ μ§ν μ μκ² λμλ€.@EnableConfigurationPropertiesμ νκ³μ
@EnableConfigurationProperties
λ₯Ό μ΄μ©ν΄ μ€μ νλ‘νΌν° ν΄λμ€(@ConfigurationProperties)
λ₯Ό μ¬μ©νλ κ²½μ°SpringBoot 2.2
λΆν°λ μ΄λ¬ν λ¬Έμ μ λ€μ ν΄κ²°νκΈ° μν΄@ConfigurationPropertiesScan
μ΄λΌλ μ΄λ Έν μ΄μ μ μ 곡νκ³ μλ€.@ConfigurationPropertiesScan
@ComponentScan
κ³Ό μλΉν μ μ¬νλ€.ν¨ν€μ§λ₯Ό κΈ°λ°μΌλ‘
@ConfigurationPropertiesScan
μ΄ λ±λ‘λ ν΄λμ€λ€μ μ°Ύμ κ°λ€μ μ£Όμ νκ³ λΉμΌλ‘ λ±λ‘ν΄μ€λ€!μ€μΊμ μ€νν ν¨ν€μ§ λλ ν΄λμ€λ₯Ό μ§μ ν΄μ€ μ μλ€.
νμν κ²½μ°
Base Package
λ₯Ό μ§μ ν΄μ€λ€.μμ κ°μ΄ μ€μ μ ν΄μ£Όλ©΄
com.mang.atdd.membership
κ³Ό κ·Έ νμ ν¨ν€μ§λ€μ λν΄@ConfigurationProperties
μ΄ λΆμ ν΄λμ€λ€μ κ°μ μ£Όμ νκ³ λΉμΌλ‘ λ±λ‘ν΄μ€λ€.λ±λ‘ν΄μ£Όμ΄μΌ νλ ν΄λμ€κ° λ§λ€λ©΄
@EnableConfigurationProperties
κ° μλ@ConfigurationPropertiesScan
μ μ΄μ©νλ κ²λ μ’λ€!μ 리
@Value
λ₯Ό μ¬μ©νμ¬ κ°μ μ£Όμ νλ λ°©μκ³Ό@ConfigurationProperties
λ₯Ό μ΄μ©νμ¬ μΈλΆ μ€μ νμΌμ μ‘΄μ¬νλ κ°μ μ£Όμ νλ λ°©λ²μ΄ μλ€.κ°κΈμ μ΄λ©΄ λΆλ³μ±μ μ μ§ν μ μλ
@ConfigurationProperties
μ@ConstructorBinding
μ κ°μ΄ μ¬μ©νλ κ²μ΄ μ’μ 보μ΄μ§λ§ [[Spring Batch]]λ₯Ό μ¬μ©νλ©΄Late Bidning
μ μν΄@Value
λ₯Ό μ¬μ©ν΄μΌ νλ κ²½μ°λ λ°μνλ€.μν©μ λ°λΌ μλ§κ² μ μ ν μ΄λ Έν μ΄μ μ μ νν΄μ μ¬μ©νμ!
Update
Improved @ConstructorBinding Detection
@ConfigurationProperties
,@ConstructorBinding
μ κ°μ΄ μ¬μ©νμλλ°,Spring Boot 3.0
λΆν° μμ±μκ° 1κ°λ§ μλ κ²½μ°μλConstructorBinding
μ μλ΅ν μ μλ€!