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赋值

SpringBoot通过@Value获取application.yml配置文件的属性值的更多相关文章

  1. SpringBoot static修饰的字段/方法如何获取application.yml配置

    SpringBoot的application.yml一种特殊的应用场景,一般我们获取application.yml的配置文件只要@Value就可以获取到值了,但是如果是static修饰的字段肯定就不能 ...

  2. 使用IDEA开发SpringBoot不加载application.yml配置文件的解决方案

    1.如果启动项目不加载application.yml配置文件,那么请确认下是否应用了Resources为项目资源文件夹 2.如果项目起初是可以正常使用的,突然不知道改了什么,然后进行启动项目的时候不加 ...

  3. Springboot默认加载application.yml原理以及扩展

    Springboot默认加载application.yml原理以及扩展 SpringApplication.run(...)默认会加载classpath下的application.yml或applic ...

  4. SpringBoot-application:application.yml/配置文件详解

    ylbtech-SpringBoot-application:application.yml/配置文件详解 springboot采纳了建立生产就绪spring应用程序的观点. Spring Boot优 ...

  5. Spring Cloud Config-Client 无法获取 Config-Server 在 github 上的配置文件的属性值,竟然是因为

    Spring Cloud Config-Client 无法获取 Config-Server 在 github 上的配置文件的属性值,竟然是因为!!! 2018年07月23日 16:33:25 一颗很菜 ...

  6. Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从而去做一些运行时的逻辑处理

    1.Column注解的的RetentionPolicy的属性值是RUTIME,这样注解处理器可以通过反射,获取到该注解的属性值,从而去做一些运行时的逻辑处理 2. 自定义注解: 使用@interfac ...

  7. SpringBoot启动如何加载application.yml配置文件

    一.前言 在spring时代配置文件的加载都是通过web.xml配置加载的(Servlet3.0之前),可能配置方式有所不同,但是大多数都是通过指定路径的文件名的形式去告诉spring该加载哪个文件: ...

  8. java获取application.properties和application.yml配置文件信息

    public static void main(String[] args) { String result=getProjectConfig("max-file-size"); ...

  9. springboot获取application.yml中的配置信息

    HelloController.java package com.springbootweb.demo.controller; import com.springbootweb.demo.entity ...

随机推荐

  1. 87. Scramble String (Java)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  2. IDEA打开光标是粗黑色,backspace键、insert键点击无效的解决办法

    问题描述:打开IDEA的编译器之后,界面显示的光标变粗,点击backspace键和insert键盘之后无效 解决方法:打开File——Settings——Plugins,在右侧的搜索栏中搜索IdeaV ...

  3. string 驻留机制

    string 驻留机制   string s1 = "abc"; string s2 = "ab"; string s3 = s2 + "c" ...

  4. SSD源码解读——数据读取

    之前,对SSD的论文进行了解读,可以回顾之前的博客:https://www.cnblogs.com/dengshunge/p/11665929.html. 为了加深对SSD的理解,因此对SSD的源码进 ...

  5. 为Qtcreator 编译的程序添加管理员权限

    (1)创建资源文件 myapp.rc 1 24 uac.manifest (2)创建文件uac.manifest <?xml version="1.0" encoding=& ...

  6. 自学Python5.7-面向对象三大基本特征_封装

    自学Python之路-Python基础+模块+面向对象自学Python之路-Python网络编程自学Python之路-Python并发编程+数据库+前端自学Python之路-django 自学Pyth ...

  7. ubuntu下安装tensorflow-gpu版本过程

    我之前已经安装了cpu-only版的tensorflow,所以现在要先把原先的tf卸载 sudo pip uninstall tensorflow sudo pip3 install tensorfl ...

  8. lightinthebox 批量设置分类产品排列方式为List、Grid、Gallery

    lightinthebox 批量设置分类产品排列方式为Grid categories_type = '1'表示List,2表示Grid,3表示Gallery方式 设置单个分类 ; ; ; 设置全部 ' ...

  9. python web开发中跨域问题的解决思路

    线上环境不存在跨域问题,nginx转发 解决思路: 1.什么是跨域 在浏览器窗口中,和某个服务端通过某个 “协议+域名+端口号” 建立了会话的前提下,去使用与这三个属性任意一个不同的源提交了请求,那么 ...

  10. 使用IDEA搭建一个Spring + Spring MVC 的Web项目(零配置文件)

    话不多说,直接上代码: 注解是Spring的一个构建的一个重要手段,减少写配置文件,下面解释一下一些要用到的注解: @Configuration 作用于类上面,声明当前类是一个配置类(相当于一个Spr ...