-
Design Pattern
- Singleton Design Pattern
- Factory Design Pattern
- Factory Method Design Pattern
- Abstract Factory Design Pattern
- Builder Design Pattern
- Prototype Design Pattern
- Object Pool Design Pattern
- Chain of Responsibility Design Pattern
- Command Design Pattern
- Interpreter Design Pattern
- Iterator Design Pattern
- Mediator Design Pattern
- Memento Design Pattern
- Observer Design Pattern
- Observer Design Pattern - Xử Lý Exception
- Strategy Design Pattern
- Template Method Design Pattern
- Visitor Design Pattern
- Null Object Design Pattern
- Adapter Design Pattern
- Bridge Design Pattern
- Composite Design Pattern
- Decorator Design Pattern
- Flyweight Design Pattern
- Proxy Design Pattern
- S.O.L.I.D
- Clean code
- Lập trình socket
- Java Core
- Multi-Thread
- Spring
- Java Web
- Memory Caching
- Message Queue
- DevOps
- Xây dựng một nền tảng
- MongoDB
- MySQL timestamp
- Properties vs yaml
- Kotlin
- Lập Trình Machine Learning với PyTorch
- Mã Nguồn Mở
- Ezy HTTP
- Free Chat
- Một số kinh nghiệm với Git
- Review cho đồng nghiệp!
- Setup Dev Environment
- Hello World
- Create a Server Project
- Handle Client Requests
- Using ezyfox-server-csharp-client
- Using ezyfox-es6-client
- Client React.js Interaction
- Build And Deploy In Local

.properties hay yaml?
Trong thế giới lập trình của chúng ta, các file cấu hình là một trong những thành phần vô cùng quan trọng, nó giúp chúng ta lưu trữ các biến môi trường như cấu hình database, cấu hình hệ thống và nó giúp chúng ta dễ dàng tuỳ chỉnh môi trường mà không phải build lại source code. Với java thì hiện nay có 2 loại file cấu hình hay được sử dụng nhất đó là .properties
và yaml
.properties
.properties
file là một tiêu chuẩn file cấu hình được sử dụng chính trong #java, nó có dạng:
key=value
Ví dụ:
database.mongo.uri=mongodb://root@123456:127.0.0.1:27017/test
database.mongo.database=test
Tất cả dữ liệu sẽ được coi là string, và nếu cần chuyển sang các dạng dữ liệu khác chúng ta có thể tự code hoặc sử dụng thư viện.
Cách đọc 1 file .properties
Java đã cung cấp cho chúng ta sẵn lớp java.util.Properties
với hàm load
để đọc nội dung file.properties từ một InputStream
như sau:
Properties properties = new Properties();
properties.load(getClass().getResourceAsStream("/application.properties"));
YAML
Còn yaml thì sao? #yaml là một loại file cấu hình được sử dụng phổ biến trong nhiều năm trở lại đây vì tính đơn giản của nó, nó có định dạng:
key: value
foo:
bar: Foo Bar
hello:
world: Hello World
Ví dụ:
database:
mongo:
uri: mongodb://root@123456:127.0.0.1:27017/test
database: test
Bạn có thể thấy nó đơn giản hơn #.properties hơn rất nhiều, tuy nhiên, hiện tại java chưa hỗ trợ thư viện chuẩn để đọc file yaml, bạn phải sử dụng thư viện bên thứ 3 SnakeYAML
Đọc file yaml
Cũng tương tự như đọc file .properties
đọc file yaml
cũng đơn giản thôi:
Yaml yaml = new Yaml();
InputStream inputStream = getClass().getResourceAsStream("/application.yaml");
Map<String, Object> properties = yaml.load(inputStream);
Kết luận
Mặc dù phải dùng thư viện bên thứ 3 nhưng rõ ràng là yaml
đơn giản và rõ ràng hơn .properties
rất nhiều, chính vì vậy ngày nay mọi người hay sử dụng file yaml
là chính. Và bạn hãy sử dụng yaml nếu bạn thích nó nhé
Mapping đối tượng
Trong thực tế, chúng ta sẽ thường có nhu cầu map file cấu hình với đối tượng, vậy phải làm thế nào? Nếu bạn đang dùng #spring thì nó đã hỗ trợ sẵn rồi, còn nếu bạn có nhu cầu phát triển độc lập, hãy dùng thư viện properties-file do mình phát triển nhé:
ApplicationConfig config = new PropertiesMapper()
.file("application.yaml") // hoặc application.properties
.map(ApplicationConfig.class);
Tham khảo
Properties
YAML
Mapping đối tượng
-
Design Pattern
- Singleton Design Pattern
- Factory Design Pattern
- Factory Method Design Pattern
- Abstract Factory Design Pattern
- Builder Design Pattern
- Prototype Design Pattern
- Object Pool Design Pattern
- Chain of Responsibility Design Pattern
- Command Design Pattern
- Interpreter Design Pattern
- Iterator Design Pattern
- Mediator Design Pattern
- Memento Design Pattern
- Observer Design Pattern
- Observer Design Pattern - Xử Lý Exception
- Strategy Design Pattern
- Template Method Design Pattern
- Visitor Design Pattern
- Null Object Design Pattern
- Adapter Design Pattern
- Bridge Design Pattern
- Composite Design Pattern
- Decorator Design Pattern
- Flyweight Design Pattern
- Proxy Design Pattern
- S.O.L.I.D
- Clean code
- Lập trình socket
- Java Core
- Multi-Thread
- Spring
- Java Web
- Memory Caching
- Message Queue
- DevOps
- Xây dựng một nền tảng
- MongoDB
- MySQL timestamp
- Properties vs yaml
- Kotlin
- Lập Trình Machine Learning với PyTorch
- Mã Nguồn Mở
- Ezy HTTP
- Free Chat
- Một số kinh nghiệm với Git
- Review cho đồng nghiệp!
- Setup Dev Environment
- Hello World
- Create a Server Project
- Handle Client Requests
- Using ezyfox-server-csharp-client
- Using ezyfox-es6-client
- Client React.js Interaction
- Build And Deploy In Local