본문 바로가기

Project

Spring boot, react 이용한 블로그 개발 (3) - ConfigurationProperties 프로퍼티 설정

이번 포스트에서는 application.yml에 프로퍼티 정보를 저장하고 스프링의 ConfigurationProperties 어노테이션을 이용하여 자바 클래스에 바인딩하는 것을 적용할 것이다. 이를 통해 프로퍼티 정보를 명시적으로 클래스를 통해 중앙 관리하여 다른 곳에서 프로퍼티 정보를 쉽게 가져다 쓸 수 있을 것이다.


1. application.yml 작성


- application.yml에는 사용할 프로퍼티 정보를 yaml 형식으로 작성하게 된다. 아래 코드에 작성된 형식의 루트에는 application과 spring이 있는데 application 이하에는 코드 작성시 명시적으로 프로퍼티를 주입시킬 대상을 나열하였고 spring 아래에는 spring과 관련된 라이브러리에서 사용하는 프로퍼티를 작성하였다. 현재 application 이하에는 datsource 속성과 추후에 적용시킬 JWT에서 사용할 속성들을 작성한 것을 확인할 수 있다. 

application:
datasource: # in addition to the standard spring.datasource properties
cachePrepStmts: true
prepStmtCacheSize: 250
prepStmtCacheSqlLimit: 2048
useServerPrepStmts: true
security:
jwt:
secret: 5867a8374e7c0f6284b177b48faf89e1c79d72d8
token-validity-in-seconds: 86400 # 24 hour
token-validity-in-seconds-for-remember-me: 2592000
spring:
application:
name: Blog
profiles:
active: dev
devtools:
restart:
enabled: true
livereload:
enabled: true
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:test;DB_CLOSE_DELAY=-1
username: sa
password:
#driver-class-name: org.h2.jdbcx.JdbcDataSource
#initialize: true
h2:
console:
enabled: true
jpa:
database-platform: org.hibernate.dialect.H2Dialect
database: H2
show-sql: false
format_sql: true
properties:
hibernate.id.new_generator_mappings: true
hibernate.cache.use_second_level_cache: false
hibernate.cache.use_query_cache: false
hibernate.generate_statistics: true
hibernate:
ddl-auto: create-drop
server:
port: 8081


2. 클래스 작성


- yaml 파일에 작성한 속성 중 application 이하의 속성들을 ApplicationProperties 클래스에 바인딩을 한다. ConfigurationProperties 어노테이션을 이용할 때는 prefix 속성을 이용하여 yaml 파일에 바인딩할 속성을 지정할 수 있다. 그리고 클래스에는 yaml 파일에 작성한 중첩된 구조대로 똑같이 클래스 작성하면 스프링 시작 시 데이터가 바인딩 된다.

@Configuration
@ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)
public class ApplicationProperties {

private final Datasource datasource = new Datasource();

private final Security security = new Security();

public Datasource getDatasource() {
return datasource;
}

public static class Datasource {

private boolean cachePrepStmts = true;

private int prepStmtCacheSize = 250;

private int prepStmtCacheSqlLimit = 2048;

private boolean useServerPrepStmts = true;

public boolean isCachePrepStmts() {
return cachePrepStmts;
}

public void setCachePrepStmts(boolean cachePrepStmts) {
this.cachePrepStmts = cachePrepStmts;
}

public int getPrepStmtCacheSize() {
return prepStmtCacheSize;
}

public void setPrepStmtCacheSize(int prepStmtCacheSize) {
this.prepStmtCacheSize = prepStmtCacheSize;
}

public int getPrepStmtCacheSqlLimit() {
return prepStmtCacheSqlLimit;
}

public void setPrepStmtCacheSqlLimit(int prepStmtCacheSqlLimit) {
this.prepStmtCacheSqlLimit = prepStmtCacheSqlLimit;
}

public boolean isUseServerPrepStmts() {
return useServerPrepStmts;
}

public void setUseServerPrepStmts(boolean useServerPrepStmts) {
this.useServerPrepStmts = useServerPrepStmts;
}
}

public Security getSecurity() {
return security;
}

public static class Security {

private final Jwt jwt = new Jwt();

public Jwt getJwt() {
return jwt;
}

public static class Jwt {

private String secret;

private long tokenValidityInSeconds = 1800;

private long tokenValidityInSecondsForRememberMe = 2592000;

public String getSecret() {
return secret;
}

public void setSecret(String secret) {
this.secret = secret;
}

public long getTokenValidityInSeconds() {
return tokenValidityInSeconds;
}

public void setTokenValidityInSeconds(long tokenValidityInSeconds) {
this.tokenValidityInSeconds = tokenValidityInSeconds;
}

public long getTokenValidityInSecondsForRememberMe() {
return tokenValidityInSecondsForRememberMe;
}

public void setTokenValidityInSecondsForRememberMe(long tokenValidityInSecondsForRememberMe) {
this.tokenValidityInSecondsForRememberMe = tokenValidityInSecondsForRememberMe;
}
}
}
}



NEXT 

- 다음 포스트에서는 위의 Repository 설정에 기반하여 관련된 컨트롤러 및 서비스를 만들어 볼 것이다.


프로젝트 github

https://github.com/keumtae-kim/spring-boot-react-blog