网上down了一份源码。启动后报错,通过报错信息定位到这个地方:

之前对这个@Value的实现方式我也没了解过,所以乘机对springboot关于这一块的源码研究了一下。可以参考当时我的一篇分析记录《@value》。对@value的原理又了一定的了解之后,发现问题并没有处在解析这一块,而是springboot并没有读取我们的配置文件。配置文件有两个:

那么为什么没有读去呢?然后我又分析了一下源码,发现默认情况下,只会读去默认的配置文件路径名称。分别是:

file:./config/application.properties
file:./config/application.xml
file:./config/application.yml
file:./config/application.yaml
file:./application.properties
file:./application.xml
file:./application.yml
file:./application.yaml
classpath:/config/application.properties
classpath:/config/application.xml
classpath:/config/application.yml
classpath:/config/application.yaml
classpath:/application.properties
classpath:/application.xml
classpath:/application.yml
classpath:/application.yaml
file:./config/application-default.properties
file:./config/application-default.properties
file:./config/application.properties
file:./config/application-default.xml
file:./config/application-default.xml
file:./config/application.xml
file:./config/application-default.yml
file:./config/application-default.yml
file:./config/application.yml
file:./config/application-default.yaml
file:./config/application-default.yaml
file:./config/application.yaml
file:./application-default.properties
file:./application-default.properties
file:./application.properties
file:./application-default.xml
file:./application-default.xml
file:./application.xml
file:./application-default.yml
file:./application-default.yml
file:./application.yml
file:./application-default.yaml
classpath:/config/application-default.properties
classpath:/config/application-default.properties
classpath:/config/application.properties
classpath:/config/application-default.xml
classpath:/config/application-default.xml
classpath:/config/application.xml
classpath:/config/application-default.yml
classpath:/config/application-default.yml
classpath:/config/application.yml
classpath:/config/application-default.yaml
classpath:/config/application-default.yaml
classpath:/config/application.yaml
classpath:/application-default.properties
classpath:/application-default.properties
classpath:/application-default.xml
classpath:/application-default.xml
classpath:/application.xml
classpath:/application-default.yml
classpath:/application-default.yaml
classpath:/application.yaml
 并没有我们的配置文件,也就是说默认情况下是读取不到我们的配置文件的。
那么怎样才能让springboot在启动的时候读去我们指定的配置文件呢?有三种方法http://blog.csdn.net/qq_21387171/article/details/53876721:

(1)    使用@PropertySource注解

直接在启动类上使用@PropertySource注解来加载不同的配置文件,具体如下所示:

(2)    修改spring.profiles.active属性

需要在application.properties文件中通过spring.profiles.active属性来设置,其值对应{profile}值,如:spring.profiles.active=dev就会加载application-dev.properties配置文件内容,具体如下所示:

(3)    执行命令行

如上面介绍所讲的一样,我们可以使用命令行,比如执行java -jar xxx.jar --spring.profiles.active=test,可以观察到服务端口被设置为8082,也就是测试环境的配置(test)。

一次@value取值失败的原因的更多相关文章

  1. react 在IE9下input标签使用e.target.value取值失败

    在react项目开发中,input标签使用onChange方法获取输入值改变state: <input type="text" id="redeemNum" ...

  2. 使用spring中4.2.6版本使用@Value取值失败,结果为${xxx}的情况

    环境:spring 4.2.6 使用全注解来获取值的时候发生@Value为写好的表达式 简单的代码: 实体类: public class RichMan {    @Value(value=" ...

  3. php上传$_FILES 无法取值

    无法取值的主要原因在form. 代码 <form method="post" action="****" name="theForm" ...

  4. 页面传递的都是string ; 每个标签要有name的原因是为了取值 因为传递给后台是键值对的形式

    页面传递的都是string ; 每个标签要有name的原因是为了取值  因为传递给后台是键值对的形式

  5. Spring @Value取值为null或@Autowired注入失败

    @Value 用于注入.properties文件中定义的内容 @Autowired 用于装配bean 用法都很简单,很直接,但是稍不注意就会出错.下面就来说说我遇到的问题. 前两天在项目中遇到了一个问 ...

  6. loadrunner动态从mysql取值

    loadrunner动态从mysql取值 [需要下载跟数据库服务器一致的dll,32位或64位] loadrunner中有参数化从数据库中取值,但是只是静态的,对于一些要实时取值的数据就game ov ...

  7. cin 对象取值过程详解

    突然又空,鉴于对cin对象的去值机制还不是很了解,就探究一番,并且记下来,方便以后复习. #include <iostream> int main(void) { using namesp ...

  8. sqlserver 导入数据出现 无法创建 OLE DB 取值函数。请查看列元数据是否有效

    我用的是Sql Server 的导入导出功能来实现的,但是有些数据可以导进去,有些就不行.总是出现一些错误! 执行之前 (错误)消息错误 0xc0202005: 数据流任务: 在数据源中找不到列“Un ...

  9. mybatis映射文件参数处理 #{}取值与${}取值的区别

    #{}:是以预编译的映射,将参数设置到sql语句中,和jdbc的preraredStatement一样,使用占位符,防止sql注入. ${}:取出的值会直接拼装在sql中,会有安全问题. 大多数情况下 ...

随机推荐

  1. 1.tornado实现高并发爬虫

    from pyquery import PyQuery as pq from tornado import ioloop, gen, httpclient, queues from urllib.pa ...

  2. 使用pipeline管道执行redis命令

    pipeline管道可以减少后端与redis的连接次数,从而实现了优化. 原理如下: 使用方法: 未使用pipeline前: strict_redis = get_redis_connection(' ...

  3. Delphi 中 函数参数中的 const 修饰符的本质以及注意事项

    来自:http://blog.csdn.net/farrellcn/article/details/9096787 ------------------------------------------ ...

  4. 《Java编程思想》笔记 第三章 操作符

    1.操作符种类: 运算顺序1-7 一元操作符(单目操作符)  - 负号, + 正号,--递减,++递增 算术操作符 + - *  /  % 移位操作符  <<左移(低位补0),>&g ...

  5. Go语言,用原子函数atomic避免资源竞争

    下一步应该是互斥锁了. package main import ( "fmt" "runtime" "sync" "sync/at ...

  6. Codeforces 651 C. Watchmen-曼哈顿距离和欧几里得距离

    C. Watchmen   time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  7. Codeforces 723 A. The New Year: Meeting Friends

    A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...

  8. C# 用程序读写另一个控制台程序

    一. using System; namespace ConsoleApp1 { class Program { static void Main(string[] args) { Console.W ...

  9. 模板—数据结构—LCT

    模板—数据结构—LCT Code: #include <cstdio> #include <algorithm> using namespace std; #define N ...

  10. 安装mongodb插件

    1.安装mngodb模块 wget http://pecl.php.net/get/mongodb-1.2.4.tgz tar zxf mongodb-1.2.4.tgz cd mongodb-1.2 ...