欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

自动注入配置参数,

来源: javaer 分享于  点击 12101 次 点评:143

自动注入配置参数,


目录
  • 1. 简化版
  • 参考

1. 简化版

  1. @EnableConfigurationProperties(CommonRedisProperties.class) 注解configuration类;
  2. @ConfigurationProperties(prefix = "myserver")注解配置文件类,prefix标明配置文件的前缀;
  3. public RedisTemplate<String, Object> getRedisTemplate(CommonRedisProperties properties, RedisConnectionFactory redisConnectionFactory) ,加到需要使用的参数中即可;
  4. 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

相关栏目:

用户点评