如何在类中应用配置文件

优先级
当前目录子目录的/config > 当前目录 > classpath的/config包 > classpath的根目录
即:越靠近的优先级越高

**指定配置文件
@PropertySource 和 SpringApplication.setDefaultProperties,比如:

SpringApplication application = new SpringApplication(Application.class);
Map<String, Object> defaultMap = new HashMap<String, Object>();
defaultMap.put("name", "Isea-Blog");
//还可以是Properties对象
application.setDefaultProperties(defaultMap);
application.run(args);

**应用属性
@Value(“${xxx}”)和@ConfigurationProperties,比如:
配置文件:

my.name=Isea533
my.port=8080
my.servers[0]=dev.bar.com
my.servers[1]=foo.bar.com

对应对象:

@ConfigurationProperties(prefix="my")
public class Config {
private String name;
private Integer port;
private List<String> servers = new ArrayList<String>(); public String geName(){
return this.name;
} public Integer gePort(){
return this.port;
}
public List<String> getServers() {
return this.servers;
}
}

Spring Boot 会自动将prefix="my"前缀为my的属性注入进来。当然若不指定前缀,则将名字对应的注入,可能冲突。

比如我在文件中配置了一个

massage:
data:
name: qibaoyi

我在类中想要获取他 需要这样去写:

@Value("${message.data.name}")
private String name;

Spring Boot 如何在类中应用配置文件的更多相关文章

  1. Java Spring Boot VS .NetCore (七) 配置文件

    Java Spring Boot VS .NetCore (一)来一个简单的 Hello World Java Spring Boot VS .NetCore (二)实现一个过滤器Filter Jav ...

  2. Spring Boot 监听 Activemq 中的特定 topic ,并将数据通过 RabbitMq 发布出去

    1.Spring Boot 和 ActiveMQ .RabbitMQ 简介 最近因为公司的项目需要用到 Spring Boot , 所以自学了一下, 发现它与 Spring 相比,最大的优点就是减少了 ...

  3. 【转载】Spring boot学习记录(二)-配置文件解析

    前言:本系列文章非本人原创,转自:http://tengj.top/2017/04/24/springboot0/ 正文 Spring Boot使用了一个全局的配置文件application.prop ...

  4. Spring Boot的SpringApplication类详解

    相信使用过Spring Boot的开发人员,都对Spring Boot的核心模块中提供的SpringApplication类不陌生.SpringApplication类的run()方法往往在Sprin ...

  5. 漫谈Spring Security 在Spring Boot 2.x endpoints中的应用(一)

    Spring Boot 2.x极大简化了默认的安全配置,并不是说有很多安全相关的配置,现在你只需要提供一个WebSecurityConfigurerAdapter继承类这样一个简单的操作,Spring ...

  6. Spring Boot SpringApplication启动类(二)

    目录 前言 1.起源 2.SpringApplication 运行阶段 2.1 SpringApplicationRunListeners 结构 2.1.1 SpringApplicationRunL ...

  7. Spring Boot学习记录03_一些属性配置文件

    转自:http://blog.didispace.com/springbootproperties/ 多环境配置(这个地方跟maven的profile配置有点类似) 我们在开发Spring Boot应 ...

  8. Spring Boot事务管理(中)

    在上一篇 Spring Boot事务管理(上)的基础上介绍Spring Boot事务属性和事务回滚规则 . 4 Spring Boot事务属性 什么是事务属性呢?事务属性可以理解成事务的一些基本配置, ...

  9. 【原】spring boot source 1.5 中不支持 diamond 运算符

    最近要开发新的项目,就花了几天时间看了下spring boot的相关资料,然后做了一个demo,不得不说开发效率确实很快,几行注解就完成了事务,aop,数据库等相关配置:但由于先前习惯了spring ...

随机推荐

  1. MySQL-python install

    Redhat 7.6 yum install python-devel mysql-devel gcc -y pip install MySQL-python==1.2.5

  2. List集合-保存和输出宠物信息

    package collection; /** * 宠物类 * @author * */ public class Pet { private String name; private String ...

  3. SQL表操作习题3 11~13题

  4. sqlsever 和oracle的参数

    StringBuilder strSql = new StringBuilder(); strSql.Append("insert into YXZY_TSDQWH("); str ...

  5. My first blog on cnBlogs!

    以后会长期更新自己的心得体会!以此锻炼自己,奋发向前.

  6. SQL Server 2005 系统数据介绍:dm_exec_connections

    原文:SQL Server 2005 系统数据介绍:dm_exec_connections 转载:http://msdn.microsoft.com/zh-cn/library/ms181509(SQ ...

  7. delphi crc校验函数

    function CalCRC16(AData: array of Byte; AStart, AEnd: Integer): string;const  GENP=$8408;  //多项式公式X1 ...

  8. Javascript中的原型链、prototype、__proto__的关系

    javascript  2016-10-06  1120  9 上图是本宝宝用Illustrator制作的可视化信息图,希望能帮你理清Javascript对象与__proto__.prototype和 ...

  9. Flex 布局学习笔记

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. jquery的attr和prop区别之实例

    对于HTML元素本身就带有的固有属性,在处理时,使用prop方法. 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法. html文件 <tr> <th>& ...