自动注入配置参数,
分享于 点击 12101 次 点评:143
自动注入配置参数,
目录- 1. 简化版
- 参考
1. 简化版
@EnableConfigurationProperties(CommonRedisProperties.class)
注解configuration
类;@ConfigurationProperties(prefix = "myserver")
注解配置文件类,prefix标明配置文件的前缀;public RedisTemplate<String, Object> getRedisTemplate(CommonRedisProperties properties, RedisConnectionFactory redisConnectionFactory)
,加到需要使用的参数中即可;META-INF
目录下添加additional-spring-configuration-metadata.json
文件,格式如下
{
"groups": [
{
"name": "server",
"type": "com.huawei.workbenchcommon.redis.CommonRedisProperties",
"sourceType": "com.huawei.workbenchcommon.redis.CommonRedisProperties"
}
],
"properties": [
{
"name": "myserver.database",
"type": "java.lang.String",
"sourceType": "org.springframework.boot.autoconfigure.web.ServerProperties"
}
]
}
参考
[1] springboot doc configuration metadata
用户点评