springboot可以提供了多种方式配置properties。

一、Java System.setProperty(k, v)

System.setProperty("myname", "Java_System_name");

二、在classpath目录下创建配置文件 application.properties

文件内容格式是 KV格式

myname=classpath_name

三、支持嵌套注解

application.properties

db=db
jdbc.username=root
jdbc.password=root

注解主类

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; @Component
@ConfigurationProperties
public class MysqlConfig { private String db; private Jdbc jdbc; public String getDb() {
return db;
} public void setDb(String db) {
this.db = db;
} public Jdbc getJdbc() {
return jdbc;
} public void setJdbc(Jdbc jdbc) {
this.jdbc = jdbc;
} }

附类

public class Jdbc {
private String username; private String password; public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}
}

springboot 会自动解析jdbc开头的属性,和注解类jdbc映射

四、创建yml文件配置

首先, pom需要依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
my:
server:
- hehe
- haha

配置类注解 : 使用

@ConfigurationProperties

prefix : 获取yml文件的my配置项
@Component("serverConfig")
@ConfigurationProperties(prefix = "my")
public class ServerConfig { private List<String> server = new ArrayList<String>(); public List<String> getServer() {
return server;
} public void setServer(List<String> server) {
this.server = server;
} }

测试 :

@RestController : spring路由请求,直接将结果返回给请求者
@EnableAutoConfiguration : springboot启动入口
@ComponentScan : 扫描注解
@RestController
@EnableAutoConfiguration
@ComponentScan
public class Application { @Autowired
private ServerConfig serverConfig; @RequestMapping("/")
public String index() { getServerConfig(); return "hello, spring boot" ;
} public void getServerConfig() {
Gson gson = new Gson(); System.out.println(gson.toJson(serverConfig.getServer()));
} public static void main(String[] args) { SpringApplication.run(Application.class, args); }
}

结果 :

["hehe","haha"]

参考文献

springboot配置

springboot中文文档

springboot 学习笔记(二)--- properties 配置的更多相关文章

  1. SpringBoot学习笔记<二>注解

    此篇为项目作结之笔记,关于注解. 项目启动入口@SpringBootApplication[必选]  @ServletComponentScan[可选] 注解后: Servlet.Filter.Lis ...

  2. Nginx学习笔记二基本配置

    1.Nginx的配置文件默认在Nginx程序安装目录的conf二级目录下,主配置文件为nginx.conf.假设您的Nginx安装 在/usr/local/webserver/nginx/目录下,那么 ...

  3. SpringBoot学习笔记二之Spring整合Mybatis

    原文链接: https://www.toutiao.com/i6803235766274097678/ 在learn-admin-component子工程中加入搭建环境所需要的具体依赖(因为比较长配置 ...

  4. 《深入理解Spring Cloud与微服务构建》学习笔记(二十)~配置中心Spring Cloud Config

    本例重新创建项目,构建一个空的mavan工程. 一.Config Server 从本地读取配置文件 新建一个moudle config_server ,pom添加依赖   <dependency ...

  5. php-resque学习笔记二(配置)

    1:前提 系统:CentOS PHP版本:PHP7     安装目录:/usr/local/php php-resque          安装目录:/home/software/php-resque ...

  6. PyCharm学习笔记(二) 调试配置

    选择PyCharm编译器 注意工程默认使用的解释器可能是Pycharm自带的,而不是单独安装的.

  7. Springboot学习笔记(六)-配置化注入

    前言 前面写过一个Springboot学习笔记(一)-线程池的简化及使用,发现有个缺陷,打个比方,我这个线程池写在一个公用服务中,各项参数都定死了,现在有两个服务要调用它,一个服务的线程数通常很多,而 ...

  8. SpringBoot学习笔记(1):配置Mybatis

    SpringBoot学习笔记(1):配置Mybatis 反思:如果自己写的笔记自己都看不懂,那就不要拿出来丢人现眼! IDEA插件 Free MyBatis Plugin插件可以让我们的MyBatis ...

  9. springboot 学习笔记(二)

    springboot 学习笔记(二) 快速创建一个springboot工程,并引入所需要的依赖 1.利用Spring initializr 来创建一个springboot项目,登陆http://sta ...

  10. SpringBoot学习笔记(3):静态资源处理

    SpringBoot学习笔记(3):静态资源处理 在web开发中,静态资源的访问是必不可少的,如:Html.图片.js.css 等资源的访问. Spring Boot 对静态资源访问提供了很好的支持, ...

随机推荐

  1. Linux性能监控工具 gtop

    给大家介绍一款性能监控工具,个人对比界面比top美观,常用指标比较清晰毕竟top上的指标不是每个人都能熟悉,也不是所有指标参数都需要看,对于新手也不便查找,好了说的再多先上图大家参观一下. 1.安装需 ...

  2. Redis4.0新特性

    redis 4.0 新特性 Redis 4.0在2017年7月发布为GA.包含几个重大改进:更好的复制(PSYNC2),线程DEL / FLUSH,混合RDB + AOF格式,活动内存碎片整理,内存使 ...

  3. CASE WHEN 小结

    1.简单的一个case when 例子: CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END 2. case when 在一整个表为空强行让其显示出一个值,在其后 ...

  4. xpath技术解析xm文件(php)

    1.结合php dom技术的学习,得出一个结论:php dom技术可以跨层取出节点,但是不能保持层次关系,使用xpath可以很好地解决问题. *** xpath技术的核心思想:迅速简洁的定位你需要查找 ...

  5. Java源码解析——集合框架(一)——ArrayList

    ArrayList源码分析 ArrayList就是动态数组,是Array的复杂版本,它提供了动态的增加和减少元素.灵活的设置数组的大小. 一.类声明 public class ArrayList< ...

  6. YII2.0学习一 Advanced 模板安装

    下载github上的完事安装包(本机环境使用Composer安装非常慢) https://github.com/yiisoft/yii2-app-advanced 解压到文件目录 wwwroot/sh ...

  7. x-pack本地安装方式

    一.首先下载本地安装包,我使用的ELK是5.6.1版本: https://artifacts.elastic.co/downloads 二.进入到elasticsearch/bin(所有节点)和kib ...

  8. Hue联合(hdfs yarn hive) 后续......................

    1.启动hdfs,yarn start-all.sh 2.启动hive $ bin/hive $ bin/hive --service metastore & $ bin/hive --ser ...

  9. go学习笔记-基础类型

    基础类型 布尔值 布尔值的类型为bool,值是true或false,默认为false. //示例代码 var isActive bool // 全局变量声明 var enabled, disabled ...

  10. Xshell 清除历史记录方法

    使用电脑久了,就会清理电脑,将一些历史记录清除,使得电脑可以运行的更快,Xshell也是同样的道理.本集小编就教大家如何清除xshell的历史记录. 如何清除历史记录: 1.打开xshell,然后点击 ...