Development
@ConfigurationProperties
8/14/2024

์คํ๋ง์์๋ @ConfigurationProperties ์ ๋ ธํ ์ด์ ์ ํตํด ์ธ๋ถ ์ค์ ์ ๊ฐ์ฒด๋ก ๋ง๋ค์ด ์์คํ ๋ด์์ ์ฌ์ฉํ ์ ์๊ฒ ํด์ค๋ค. ์ด๋ฅผ ํตํด ๋จ์ํ ์ธ๋ถ ์ค์ ์ ๋งคํํ๋ ๊ฒ ๋ฟ๋ง ์๋๋ผ ๊ฐ์ฒด์ ์ด์ ์ ์ด๋ฆฌ๋ ๋ฐฉ์์ผ๋ก ํ๋ก๊ทธ๋๋ฐ์ ํ ์ ์๊ฒ ๋์์ค๋ค.
@ConfigurationProperties
์ฌ์ฉํ๊ณ ์ ํ๋ ์์ฑ๋ค์ด ์กด์ฌํ๋ ํด๋์ค๋ฅผ ์์ฑํ ๋ค, ์ ๋ ธํ ์ด์ ์๋ฆฌ๋จผํธ์ ํด๋น ์์ฑ๋ค์ ๊ธฐ๋ณธ ์ฃผ์๋ฅผ ์์ฑํ๋ค. ์ดํ ์ด ํด๋์ค๋ฅผ ๋ฑ๋กํ๋ฉด, ์คํ๋ง์ ๊ธฐ๋ณธ ์ฃผ์์ ๋ณ์๋ช ์ ํ ๋๋ก ์ธ๋ถ ์ค์ ๊ฐ์ ์ฝ์ด์ ๊ฐ์ ์ ์ฅํ๋ค.
๊ธฐ๋ณธ ์ฃผ์ ๋ฐฉ์์ Java Bean Property ๋ฐฉ์์ธ getter, setter๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ์ด์ ํด๋นํ๋ ์ฝ๋๋ ์์ฑํด์ฃผ์ด์ผ ํ๋ค.
@Data
@ConfigurationProperties("db.mysql")
public class MySQLProperties {
private String host;
private Integer port;
private String db;
}์์ ๊ฐ์ด ์์ฑํ ๊ฒฝ์ฐ application.properties ๋ฑ์ผ๋ก ์ ๊ณตํ db.mysql.host, db.mysql.port, db.mysql.db ์ ์ ๋ณด๋ฅผ ์ฝ์ด์ ๊ฐ ํ๋์ ๋งคํํ๋ค. ์ฌ์ค ์ ์ฝ๋๋ง์ผ๋ก ๋์ํ์ง๋ ์๊ณ , ํด๋น ํด๋์ค๊ฐ ๊ตฌ์ฑ์ ๋ณด๋ฅผ ์ฝ์ด์ฌ ์ ์๋๋ก ์ถ๊ฐ์ ์ธ ์ ๋ณด๋ฅผ ์คํ๋ง ์ธก์ ์๋ ค์ผ ํ๋ค.
@Configuration
@EnableConfigurationProperties(MySQLProperties.class)
public class ConfigurationPropertiesConfig {}์ ์ฝ๋๊น์ง ์์ฑ๋ ์ดํ๋ผ๋ฉด, ์คํ๋ง์ ์ปจํ ์ด๋ ์ด๊ธฐํ ์์ ์์ MySQLProperties์ ๋น์ ๋ฑ๋กํ๋ค. ์ด ๊ณผ์ ์์ ํ์ ๊ฒ์ฆ์ ์ํํด, ๋ง์ง ์๋ ๊ฒฝ์ฐ ์ค๋ฅ๋ฅผ ๋ด๋ฑ๊ณ ํ๋ก๊ทธ๋จ์ ์ค์ง์ํจ๋ค.
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to bind properties under 'db.mysql.port' to java.lang.Integer:
Property: db.mysql.port
Value: "port"
Origin: class path resource [application.properties] - 5:15
Reason: failed to convert java.lang.String to java.lang.Integer (caused by java.lang.NumberFormatException: For input string: "port")
Action:
Update your application's configuration์ฌ์ง์ด Intellij๋ Java ์ฝ๋๋ฅผ ์ฝ์ด ํ์ํ ํ๊ฒฝ๋ณ์๋ฅผ ๋ถ์ํ๊ณ , ํ์ ์ด ๋ค๋ฅธ ๊ฒฝ์ฐ ๊ฒฝ๊ณ ๋ฅผ ๋์์ฃผ๊ธฐ๋ ํ๋ค.

@ConfigurationPropertiesScan
@EnableConfigurationProperties๋ ๋ฑ๋กํ๊ณ ์ ํ๋ ํ๊ฒฝ๋ณ์๋ฅผ ํ๋ํ๋ ์์ฑํด์ค์ผ ํ๋ค๋ ๋ฒ๊ฑฐ๋ก์์ด ์๋๋ฐ, ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์๋ @ConfigurationPropertiesScan์ ์ฌ์ฉํ ์ ์๋ค. ์ ๋ ธํ ์ด์ ์์ฑ์ธ basePackage๋ฅผ ์ค์ ํ์ง ์๋๋ค๋ฉด @ConfigurationPropertiesScan์ด ๋ถ์ด์๋ ์ค์ ํด๋์ค์ ๊ฐ์ ํจํค์ง๊ฑฐ๋ ํ์ ํจํค์ง๋ฅผ ๋์์ผ๋ก @ConfigurationProperties๊ฐ ๋ถ์ ํด๋์ค๋ฅผ ์ฝ์ด ์ค์ ์ ๋ณด ๋น์ ๋ฑ๋กํ๋ค.
@Configuration
//@EnableConfigurationProperties(MySQLProperties.class)
@ConfigurationPropertiesScan
public class ConfigurationPropertiesConfig {
}์์ฑ์ ๋ฐฉ์ ๋ฑ๋ก
ํ์ง๋ง getter setter๊ฐ ์กด์ฌํ๋ ๊ฐ์ฒด๋ ๊ฐ์ฒด์ ์๋ช ์ฃผ๊ธฐ ๋ด์์ ๊ฐ์ด ๋ณํ๋ ๊ฐ๋ฅ์ฑ์ ๋ณ๋๋ค. ์ด๋ฌํ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ๊ธฐ ์ํด์ ์ค์ ์ ๋ณด๋ฅผ ์์ฑ์๋ฅผ ํตํด ์ฃผ์ ์ํฌ ์ ์๋ค. ์คํ๋ง์ ์์ฑ์๊ฐ ํ๋๋ผ๋ฉด ํด๋น ์์ฑ์๋ฅผ ํตํด ์ค์ ์ ๋ณด๋ฅผ ์ฃผ์ ํด์ค๋ค.
@Getter
@ConfigurationProperties(prefix = "db.mysql")
public class MySQLProperties {
private final String host;
private final Integer port;
private final String db;
public MySQLProperties(String host, Integer port, String db) {
this.host = host;
this.port = port;
this.db = db;
}
}๊ตฌ์ฑ์ ๋ณด ๊ฒ์ฆ
์ ๊ธฐ์ ๋ง์ผ๋ก ๊ฐ๋จํ ํ์ ๊ฒ์ฆ์ ๋์ง๋ง ๊ฐ ์์ฒด๋ฅผ ์ ๋ฐํ๊ฒ ๊ฒ์ฆํ ์๋ ์๋ค. ์คํ๋ง์ Java Bean Validator๋ฅผ ํตํด ๊ตฌ์ฑ์ ๋ณด๋ฅผ ๊ฒ์ฆํ ์ ์๊ฒ ๋์์ค๋ค.
@ConfigurationProperties(prefix = "db.mysql")
@Validated
public class MySQLProperties {
private final String host;
private final Integer port;
private final String db;
@Min(1)
@Max(100)
private final Integer poolSize;
public MySQLProperties(String host, Integer port, String db, Integer poolSize) {
this.host = host;
this.port = port;
this.db = db;
this.poolSize = poolSize;
}
}์์ ๊ฐ์ด ํด๋์ค ๋ ๋ฒจ์ @Validated ์ ๋ ธํ ์ด์ ๊ณผ ํ๋์ Validator ์ ๋ ธํ ์ด์ ์ ๋ถ์ฐฉํด๋์ผ๋ฉด ์ด๊ธฐํ ์์ ์ ๊ฒ์ฆ์ ์ํํ๊ณ , ์คํจํ ๊ฒฝ์ฐ ์๋ฌ ๋ฌธ๊ตฌ์ ํจ๊ป ํ๋ก๊ทธ๋จ์ ์ข ๋ฃ์ํจ๋ค.
***************************
APPLICATION FAILED TO START
***************************
Description:
Binding to target com.example.demo.MySQLProperties failed:
Property: db.mysql.poolSize
Value: "1000"
Origin: class path resource [application.properties] - 6:20
Reason: 100 ์ดํ์ฌ์ผ ํฉ๋๋ค
Action:
Update your application's configuration