SpringBoot通过@Value获取application.yml配置文件的属性值

时间:2018-09-01 19:55:55   收藏:0   阅读:2388

application.yml实例:

spring:
    redis:
      database: 0
      host: 127.0.0.1

获取方法:

/**
 * @Auther:WangZiBin
 * @Description:
 * @Modified By:
 */
@Configuration
public class JedisConfig{

    private Logger jedisConfigLogger= LoggerFactory.getLogger(JedisConfig.class);

    @Value("${spring.redis.host:#{null}}")
    private String host;

    @Value("${spring.redis.port:#{null}}")
    private Integer port;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }


}

注意@Configuration注解是必须的,@Component同样适用

@Value("${spring.redis.port:#{null}}")

其中

:#{null}

作用为在取不到对应配置值时,采用默认值null赋值

原文:https://www.cnblogs.com/zhangzhiqin/p/9571270.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!