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. MySql Connector/c++8中JSON处理Demo

    #include <iostream> #include <vector> #include <mysqlx/xdevapi.h> using std::cout; ...

  2. ABAP术语-Lock Mode

    Lock Mode 原文:http://www.cnblogs.com/qiangsheng/archive/2008/02/29/1085732.html Status that determine ...

  3. Linux计算某一列的和

    ll | awk '{print $5}' | egrep -v "^$"| paste -sd+|bc 简单说明: ll:拿到当前目录下所有的文件大小 awk:拿到第几列 egr ...

  4. python排序算法的整理

    #冒泡排序,主要思想:从第一个元素开始,向尾部开始比较大小换位置 def bubble_sort(alist): for j in range(len(alist)-1,0,-1): for i in ...

  5. nginx问题之nginx: could not build server_names_hash, you should increase server_names_hash_bucket_size解决方案

    昨天在nginx上部署了一个网站后,发现访问不了,再去访问之前部署的网站,发现都访问不了了,去看下下nginx,发现nginx服务停止了,没有在运行,重启了下服务,发现还是一样,就去看了下nginx的 ...

  6. 企业Shell面试题及企业运维实战案例(三)

    1.企业Shell面试题1:批量生成随机字符文件名案例 使用for循环在/oldboy目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串oldboy,名称示例如下: ...

  7. DevOps - 项目私库 - Nexus Repository

    相关链接 Sonatype官网:https://www.sonatype.com Products: Nexus Repository OSS2.x & 3.x Documentation:  ...

  8. Zabbix源码安装部署

    zabbix源码部署安装 参考文档:https://www.zabbix.com/documentation/4.0/manual/installation/install ​ https://www ...

  9. ThinkPHP框架介绍

    什么是框架 php框架是许多代码的集合,这些代码的程序结构的代码(并不是业务代码)代码中有许多的函数,类,功能类包 不使用框架开发的缺陷 代码编写不规范 牵一发而动全身 不能很好满足客户各方面的需求 ...

  10. node Cookie

    代码: const express = require('express'); const cookieParser = require('cookie-parser'); const app = e ...