跟我学springboot之(二)配置元数据
元数据
Spring Boot jar包含元数据文件,提供所有支持的配置属性的详细信息。这些文件旨在允许IDE开发人员在用户使用application.properties 或application.yml文件时提供上下文帮助和“代码完成” 。
主要的元数据文件是在编译器通过处理所有被@ConfigurationProperties注解的节点来自动生成的。
元数据格式
配置元数据位于jars文件中的META-INF/spring-configuration-metadata.json,它们使用一个具有”groups”或”properties”分类节点的简单JSON格式:
{"groups": [
{
"name": "server",
"type": "org.springframework.boot.autoconfigure.web.ServerProperties",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
}
...
],"properties": [
{
"name": "server.port",
"type": "java.lang.Integer",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
},
{
"name": "server.servlet-path",
"type": "java.lang.String",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
"defaultValue": "/"
}
...
]}
每个”property”是一个配置节点,用户可以使用特定的值指定它。例如,server.port和server.servlet-path可能在application.properties中如以下定义:
server.port=9090
server.servlet-path=/home
“groups”是高级别的节点,它们本身不指定一个值,但为properties提供一个有上下文关联的分组。例如,server.port和server.servlet-path属性是server组的一部分。
注:不需要每个”property”都有一个”group”,一些属性可以以自己的形式存在。
Group属性
groups数组包含的JSON对象可以由以下属性组成:

Property属性
properties数组中包含的JSON对象可由以下属性构成:

deprecation每个properties元素的属性中包含的JSON对象可以包含以下属性:

在Spring Boot 1.3之前,deprecated可以使用单个布尔属性来代替deprecation元素。这仍然以不推荐的方式支持,不应再使用。如果没有理由和替换可用,deprecation应该设置一个空的对象。
hints
hints数组中包含的JSON对象可以包含以下属性:


可重复的元数据节点
在同一个元数据文件中出现多次相同名称的”property”和”group”对象是可以接受的。例如,Spring Boot将spring.datasource属性绑定到Hikari,Tomcat和DBCP类,并且每个都潜在的提供了重复的属性名。这些元数据的消费者需要确保他们支持这样的场景。
提供手动提示
为了改善用户体验并进一步协助用户配置给定的属性,您可以提供额外的元数据:
描述属性的潜在值列表。
关联一个提供者以将一个明确定义的语义附加到一个属性,这样工具就可以根据项目的上下文来发现潜在值的列表。
- 创建Javaconfig文件
package com.lf.datasource; import com.alibaba.druid.pool.DruidDataSource; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.sql.SQLException;
@ConfigurationProperties(prefix = "spring.druid")
@Component
public class DruidSource {
private String dbUrl;
private String username;
private String password;
private String driverClassName;
private int initialSize;
private int minIdle;
private int maxActive;
private int maxWait;
private int timeBetweenEvictionRunsMillis;
private int minEvictableIdleTimeMillis;
private String validationQuery;
private boolean testWhileIdle;
private boolean testOnBorrow;
private boolean testOnReturn;
private boolean poolPreparedStatements;
private int maxPoolPreparedStatementPerConnectionSize;
private String filters;
private String connectionProperties;
..get/set
}
- 添加maven依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true </optional> </dependency>
- 添加spring-configuration-metadata.json
在\resources\META-INF\下添加spring-configuration-metadata.json文件
内容如下:{ "hints": [], "groups": [ { "sourceType": "com.lf.datasource.DruidSource", "name": "spring.druid", "type": "com.lf.datasource.DruidSource" } ], "properties": [ { "sourceType": "com.lf.datasource.DruidSource", "name": "spring.druid.connection-properties", "type": "java.lang.String" }, { "sourceType": "com.lf.datasource.DruidSource", "name": "spring.druid.db-url", "type": "java.lang.String" }, { "sourceType": "com.lf.datasource.DruidSource", "name": "spring.druid.driver-class-name", "type": "java.lang.String" }, { "sourceType": "com.lf.datasource.DruidSource", "name": "spring.druid.filters", "type": "java.lang.String" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": 0, "name": "spring.druid.initial-size", "type": "java.lang.Integer" }, { "name": "spring.druid.initialSize", "description": "Description for spring.datasource.initialSize.", "type": "java.lang.String" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": 0, "name": "spring.druid.max-active", "type": "java.lang.Integer" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": 0, "name": "spring.druid.max-pool-prepared-statement-per-connection-size", "type": "java.lang.Integer" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": 0, "name": "spring.druid.max-wait", "type": "java.lang.Integer" }, { "name": "spring.druid.maxActive", "description": "Description for spring.datasource.maxActive.", "type": "java.lang.String" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": 0, "name": "spring.druid.min-evictable-idle-time-millis", "type": "java.lang.Integer" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": 0, "name": "spring.druid.min-idle", "type": "java.lang.Integer" }, { "name": "spring.druid.minIdle", "description": "Description for spring.datasource.minIdle.", "type": "java.lang.String" }, { "sourceType": "com.lf.datasource.DruidSource", "name": "spring.druid.password", "type": "java.lang.String" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": false, "name": "spring.druid.pool-prepared-statements", "type": "java.lang.Boolean" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": false, "name": "spring.druid.test-on-borrow", "type": "java.lang.Boolean" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": false, "name": "spring.druid.test-on-return", "type": "java.lang.Boolean" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": false, "name": "spring.druid.test-while-idle", "type": "java.lang.Boolean" }, { "sourceType": "com.lf.datasource.DruidSource", "defaultValue": 0, "name": "spring.druid.time-between-eviction-runs-millis", "type": "java.lang.Integer" }, { "sourceType": "com.lf.datasource.DruidSource", "name": "spring.druid.username", "type": "java.lang.String" }, { "sourceType": "com.lf.datasource.DruidSource", "name": "spring.druid.validation-query", "type": "java.lang.String" } ] }
- 编译项目(一定要编译项目)
- 创建application文件配置
如下的属性就可以自动提示,有点ide工具可能不支持,idea可以完美支持``` #连接池的配置信息 ## 初始化大小,最小,最大 spring.druid.initialSize=5 spring.druid.minIdle=5 spring.druid.maxActive=20 ## 配置获取连接等待超时的时间 spring.druid.maxWait=60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 spring.druid.timeBetweenEvictionRunsMillis=60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 spring.druid.minEvictableIdleTimeMillis=300000 spring.druid.validationQuery=SELECT 1 FROM DUAL spring.druid.testWhileIdle=true spring.druid.testOnBorrow=false spring.druid.testOnReturn=false spring.druid.poolPreparedStatements=true spring.druid.maxPoolPreparedStatementPerConnectionSize=20 # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.druid.filters=stat,wall,log4j # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 spring.druid.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
yml配置文件获取方式:
1) 创建config文件,获取值
2)通过注解@value("${属性名}") 获取值
properties文件配置获取方式:
创建bean,添加注解@Configuration @PropertySource(value = "classpath:xxx.properties") @ConfigurationProperties(prefix = "com.test")
案例:
test.properties
com.forezp.name=forezp com.forezp.age=12 com.forezp.sex=male
user.java
package com.forezp.bean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; /** * Created by fangzhipeng on 2017/4/18. */ @Configuration @PropertySource(value = "classpath:test.properties") @ConfigurationProperties(prefix = "com.forezp") public class User { private String name; private int age; private String sex; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
controller
@Autowired User user; @RequestMapping(value = "/user") public String user(){ return user.getName()+user.getAge()+"性别:"+user.getSex(); }
浙公网安备 33010602011771号