티스토리 뷰

외부에 있는 property를 읽어와서 적용해야하는 상황이 있다. 직접 파일로 읽어와도 좋고 Typesafe에서 제공하는 Config 같은 라이브러리를 사용해도 좋다. 심플하게 property를 읽어올 수 있는 Typesafe Config의 사용 방법을 간략히 소개한다.

 

  • resource 로 지정된 디렉토리 하위의 property, conf 파일을 읽어온다.
  • property file path/name을 지정하지 않으면 위에서 아래로 이름의 우선순위를 가진다.
ConfigFactory.load();
// first-listed are higher priority
1. application.conf (all resources on classpath with this name)
2. application.json (all resources on classpath with this name)
3. application.properties (all resources on classpath with this name)
4. reference.conf

 

 

  • 일반적으로 property file path/name을 지정한다.
Property
db.pw=this_is_db_password
---------------------------------------------------

Typesafe.config
Config config = ConfigFactory.load("db/connention/db.properties");
String property = config.getString("db.pw")

System.out.println("db pw: " + property);
---------------------------------------------------
output
this_is_db_password

 

property를 추가해야하는 경우 ConfigValue 객체를 생성해서 새로운 Config를 만든다. (config는 Immutable 이다.)

Config withOutSchemaConfig = ConfigFactory.load("db/connention/db.properties");

ConfigValue schemaName = ConfigValueFactory.fromAnyRef("schemaName"));

Config config = withOutSchemaConfig.withValue("db.schema", schemaName);

 

 

 

참고

- https://github.com/lightbend/config

- https://mvnrepository.com/artifact/com.geirsson/metaconfig-typesafe-config

'Programming > 유용한 라이브러리' 카테고리의 다른 글

Jcommander Parameter  (0) 2020.04.24
JsonPath + Template Engine  (0) 2020.04.11
JsonPath  (0) 2020.04.03
MapStruct  (0) 2019.08.30
Guava AsyncEventBus  (0) 2019.02.07
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함