yaml文件用法
yaml文件在SpringBoot中可取代application.properties文件。
#ymal文件對空格很敏感,采取鍵值對方式連接對應關系 #ymal還可以直接給實體類賦值 #student: #name: zhouwei #age: 23 #這是三個對象#student: # name: zhouwei # age: 23 #這是一個對象,student.name,student.age#student: # name: zhouwei # age: 23 #這是一個對象,student.name.ageserver:port: 8081# 存對象 student:name: zhouweiage: 23# 存對象的另一種寫法 student2: {name: zhouwei2,age: 23}#數組 pets:- cat- dogpets2: {cat,dog}配置yml文件后通過configuration綁定
spring:datasource:username: rootpassword: 123456url: jdbc:mysql://localhost:3306/treehole?useUnicode=true&characterEncoding=utf-8driver-class-name: com.mysql.cj.jdbc.Drivertype: com.alibaba.druid.pool.DruidDataSource#配置監控統計攔截的filters: stat-監控統計 log4j-記錄日志 wall-防御sql注入filters: stat,wall,log4j通過@Bean和@ConfigurationProperties可以實現綁定
package com.example.jdbctest.config;import com.alibaba.druid.pool.DruidDataSource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;@Configuration public class DruidConfig {@Bean@ConfigurationProperties(prefix = "spring.datasource")public DataSource druidDataSource(){return new DruidDataSource();}}總結
- 上一篇: 生态系统长期观测数据产品体系
- 下一篇: 需求、需求工程与需求工程师 — 3. 需