Properties ↔ YAML Converter
用於轉換Spring Boot的application.properties和application.yml檔案的工具。配置檔案遷移和重構必備!
📄 Properties → YAML
Output will appear here...
💡 Spring Boot Configuration Tips
Properties 장점:
- 간단하고 직관적
- 한 줄에 하나의 설정
- 검색하기 쉬움
YAML 장점:
- 계층 구조가 명확
- 중복 줄임
- 가독성 좋음
주의사항:
- YAML은 들여쓰기에 민감 (스페이스만 사용)
- Properties는 특수문자 이스케이프 필요
- Profile별 파일 분리 권장
Properties vs YAML
Properties格式
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret
server.port=8080
YAML格式
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: secret
server:
port: 8080
何時使用?
Properties → YAML轉換
- 將專案遷移到YAML時
- 想要清楚地看到層級結構時
- 想要減少重複前綴時
- 想要提高可讀性時
YAML → Properties轉換
- 與舊系統的相容性
- 偏好簡單配置
- 更容易搜尋和grep
- IDE自動補全效果更好
實際使用範例
資料庫配置
Properties:
# Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=secret123
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
YAML:
# Database Configuration
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydb
username: root
password: secret123
driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: true
伺服器 & 日誌配置
Properties:
server.port=8080
server.servlet.context-path=/api
server.compression.enabled=true
logging.level.root=INFO
logging.level.com.example=DEBUG
logging.file.name=logs/application.log
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} - %msg%n
YAML:
server:
port: 8080
servlet:
context-path: /api
compression:
enabled: true
logging:
level:
root: INFO
com.example: DEBUG
file:
name: logs/application.log
pattern:
console: '%d{yyyy-MM-dd HH:mm:ss} - %msg%n'
複雜屬性
Properties:
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.consumer.group-id=my-group
spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
YAML:
spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
group-id: my-group
auto-offset-reset: earliest
key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
Properties與YAML比較
✅ Properties優點
- 簡單: 一行一個設定
- 易於搜尋: 使用grep輕鬆查找
- 熟悉: 舊的Java標準
- 特殊字元: 易於轉義
✅ YAML優點
- 可讀性: 層級結構清晰
- 減少重複: 省略公共前綴
- 註解: 更靈活的註解
- 列表: 可以表達陣列
⚠️ Properties缺點
- 重複較多
- 難以理解層級結構
- 複雜配置變得冗長
⚠️ YAML缺點
- 對縮排敏感
- 不能使用定位字元(僅空格)
- 解析錯誤難以除錯
在Spring Boot中的差異
設定檔使用
Properties:
# application.properties (公共)
app.name=MyApp
# application-dev.properties (開發)
spring.datasource.url=jdbc:mysql://localhost:3306/dev_db
# application-prod.properties (生產)
spring.datasource.url=jdbc:mysql://prod-server:3306/prod_db
YAML (在一個檔案中區分設定檔):
app:
name: MyApp
---
spring:
config:
activate:
on-profile: dev
datasource:
url: jdbc:mysql://localhost:3306/dev_db
---
spring:
config:
activate:
on-profile: prod
datasource:
url: jdbc:mysql://prod-server:3306/prod_db
列表/陣列表達
Properties (使用索引):
app.servers[0]=server1.example.com
app.servers[1]=server2.example.com
app.servers[2]=server3.example.com
YAML (原生陣列):
app:
servers:
- server1.example.com
- server2.example.com
- server3.example.com