Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Spring Boot 配置

Spring Boot 配置

作者头像
村雨遥
发布于 2020-08-27 02:09:17
发布于 2020-08-27 02:09:17
75300
代码可运行
举报
文章被收录于专栏:JavaParkJavaPark
运行总次数:0
代码可运行

目录

  • 1. 前言
  • 2. application.properties
    • 2.1 自定义属性
    • 2.2 多环境配置
  • 3. 自定义配置文件
  • 4. 注意

1. 前言

为了 Spring Boot 能够更好地生成配置元数据文件,我们可以在创建项目时添加 Spring Configuartion Processor 依赖,或者在创建好项目后的 pom.xml 文件中手动添加。添加该依赖后,我们在编写配置时就会有属性提示,大大降低编写错误。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

2. application.properties

2.1 自定义属性

application.properties 配置文件是创建项目后就自带的,如果我们要自定义属性,可以在其中直接配置,配置过程如下:

  1. application.properties 中添加我们要自定义的配置;
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
cunyu.id=1024
cunyu.name=村雨遥
cunyu.website=https://cunyu1943.github.io
  1. 创建实体类来映射我们配置的属性;
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.cunyu.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : CunyuProperties
 * @date : 2020/7/29 13:34
 * @description : TODO
 */

@Component
@ConfigurationProperties(prefix = "cunyu")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CunyuProperties {
    private int id;
    private String name;
    private String website;
}
  1. 定义 Controller 来注入测试;
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.cunyu.controller;

import com.cunyu.pojo.CunyuProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : CunyuPropertiesController
 * @date : 2020/7/29 13:37
 * @description : TODO
 */

@RestController
@RequestMapping("/cunyu")
public class CunyuPropertiesController {
    private static final Logger logger = LoggerFactory.getLogger(CunyuPropertiesController.class);

    @Autowired
    CunyuProperties cunyuProperties;

    @GetMapping("/profile")
    public String cunyuProfile(){
        logger.info("---------------");
        logger.info(cunyuProperties.toString());
        logger.info("---------------");
        return cunyuProperties.toString();
    }
}
  1. 打开网页测试,打开 1,同时观察控制台,显示如下内容则说明属性注入成功;

2.2 多环境配置

实际开发过程中,常常需要多个环境(如 开发、测试、生产等),而不同环境的配置都不一样,此时配置方法如下;

  1. 创建不同环境对应的配置文件,配置文件名为 application-{profile}.properties{profile} 为我们自定义环境,如下:

application-dev.properties

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
server.servlet.context-path=/dev

application-test.properties

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
server.servlet.context-path=/test

application-prod.properties

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
server.servlet.context-path=/prod
  1. 然后在 application.properties 中加入激活的环境,此时就会激活对应环境的配置;
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# {profile} 对应上述的 dev、test、prod
spring.profiles.active={profile}

3. 自定义配置文件

加入我们不想用项目自带的 application.properties 配置环境,那又该如何配置呢?

  1. 首先创建一个自定义配置文件 my.properties,文件名可以自定义,但是后缀要保持一致,然后在其中加入我们自定义配置的属性;
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
my.id=1024
my.name=村雨遥
my.website=https://cunyu1943.github.io
  1. 定义实体类,用于映射自定义配置文件中的内容;
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.cunyu.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : MyProperties
 * @date : 2020/7/29 14:05
 * @description : TODO
 */

@Component
@PropertySource("classpath:my.properties")
@ConfigurationProperties(prefix = "my")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyProperties {
    private int id;
    private String name;
    private String website;
}
  1. 定义 Controller 来注入测试
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
package com.cunyu.controller;

import com.cunyu.pojo.MyProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : cunyu
 * @version : 1.0
 * @className : MyPropertiesController
 * @date : 2020/7/29 14:07
 * @description : TODO
 */

@RestController
@RequestMapping("/my")
public class MyPropertiesController {
    private static final Logger logger = LoggerFactory.getLogger(MyPropertiesController.class);

    @Autowired
    MyProperties myProperties;

    @GetMapping("/profile")
    public String myProfile() {
        logger.info("=============");
        logger.info(myProperties.toString());
        logger.info("=============");

        return myProperties.toString();
    }
}
  1. 打开网页测试,打开 http://localhost:8080/my/profile,同时观察控制台,显示如下内容则说明属性注入成功;

4. 注意

  1. application.propertiesmy.applicaiton.properties 会优先加载 application.properties
  2. 本文所有代码均已上传 Github[1],若有需要请自取。

参考资料

[1]

Github: https://github.com/cunyu1943/java-learning-code/tree/master/springboot-learning/spring-boot-01

- END -

往期回顾

通过注解的方式整合 MyBatis + Spring Boot

通过注解的方式整合 Mybatis + PageHelper 分页显示

Spring Boot 通过 XML 的方式整合 MyBatis

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-08-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 村雨遥 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
Spring Boot 配置详解
为了 Spring Boot 能够更好地生成配置元数据文件,我们可以在创建项目时添加 Spring Configuartion Processor 依赖,或者在创建好项目后的 pom.xml 文件中手动添加。添加该依赖后,我们在编写配置时就会有属性提示,大大降低编写错误。
村雨遥
2022/06/15
7230
Spring Boot 配置详解
Spring Boot 整合 Thymeleaf
第 2.3 中,设置了跳转的视图为 index,所以我们需要在 src/main/resources/templates 中创建 index.html;
村雨遥
2020/08/04
5480
Spring Boot 整合 Thymeleaf
Spring Boot 通过注解的方式整合 MyBatis
目前而言,国内大家使用最多的持久层框架可能还是 MyBatis 吧,那既然如此,更强大的 Spring Boot 遇上炽手可热的 MyBatis,又会擦出什么样的火花呢?
村雨遥
2022/06/15
3440
Spring Boot 通过注解的方式整合 MyBatis
Spring Boot 快速入门系列(I) —— 属性配置篇
通过上一篇(Spring Boot 快速入门系列(先导篇) —— 从 Hello World 开始)的介绍,相信大家对 Spring Boot 构建轻量级项目已经有了初步的了解和认识。
IT技术小咖
2019/07/09
5260
Spring Boot 快速入门系列(I) —— 属性配置篇
SpringBoot三大开发工具,你都用过么?
idea修改完代码后再按下 ctrl + f9 使其重新编译一下,即完成了热部署功能
Java技术江湖
2022/02/17
5210
SpringBoot三大开发工具,你都用过么?
spring-boot 速成(4) 自定义配置
spring-boot 提供了很多默认的配置项,但是开发过程中,总会有一些业务自己的配置项,下面示例了,如何添加一个自定义的配置:
菩提树下的杨过
2018/09/20
5420
spring-boot 速成(4) 自定义配置
Spring Boot 使用 Swagger3 生成 API 接口文档
在之前的文章中,我们已经讲了如何利用 Spring Boot 来集成 Swagger2,详情可戳:Spring Boot 集成 Swagger2,构建强大的 API 文档[1]。但其实 Swagger2 中主流的 2.9.2 自 2018 年发布后就已经好久没更新了,而在时隔两年之后的 2020 年,Swagger3 终于发布了。
村雨遥
2022/01/19
28K1
Spring Boot 使用 Swagger3 生成 API 接口文档
SpringBoot 通过注解的方式整合 Mybatis + PageHelper 分页显示
本篇博客主要利用 SpringBoot 通过注解的方式整合 Mybatis 同时利用 PageHelper 对结果分页,所有涉及的代码已经上传 Github mybatis-pagehelper[1]。下面是整个整合过程,接下来开始整合:
村雨遥
2020/08/13
1.5K0
Spring学习笔记
```java package com.kob.backend.controller.pk;
h3110_w0r1d
2024/02/19
1690
Spring学习笔记
Spring Boot 整合 Thymeleaf 实例
在新建项目时添加,在 Templeate Engines 中勾选 Thymeleaf;
村雨遥
2022/06/15
5120
Spring Boot 整合 Thymeleaf 实例
SpringBoot读取配置文件的6种方式,包括:通过Environment、@PropertySource、@ConfigurationProperties、
Environment是用来读取应用程序运行时的环境变量的类,可以通过key-value的方式读取application.properties和系统环境变量,命令行输入参数,系统属性等.
刘大猫
2024/11/05
1.7K0
一起来学SpringBoot | 第二篇:SpringBoot配置详解
上一篇介绍了 SpringBoot 由来及构建方式,通过第一章的教程我们对 SpringBoot 不在感到陌生,可以发现 SpringBoot 虽然干掉了 XML 但未做到 零配置,它体现出了一种 约定优于配置,也称作按约定编程,是一种软件设计范式,旨在减少软件开发人员需做决定的数量,获得简单的好处,而又不失灵活性。 一般情况下默认的配置足够满足日常开发所需,但在特殊的情况下,我们往往需要用到自定义属性配置、自定义文件配置、多环境配置、外部命令引导等一系列功能。不用担心,这些 SpringBoot 都替我们考虑好了,我们只需要遵循它的规则配置即可
battcn
2018/08/03
4820
缓存 - Spring Boot 整合 Caffeine 不完全指北
@Cacheable:表示该方法支持缓存。当调用被注解的方法时,如果对应的键已经存在缓存,则不再执行方法体,而从缓存中直接返回。当方法返回null时,将不进行缓存操作。
小小工匠
2023/07/24
2.4K0
缓存 - Spring Boot 整合 Caffeine 不完全指北
Spring Boot 中文参考指南
本节对Spring Boot进行介绍 以及如何安装,我们将引导您构建第一个Spring Boot 应用,同时讨论一些核心准则。
阿提说说
2023/02/27
4.9K0
Spring Boot 中文参考指南
3步轻松搞定Spring Boot缓存
本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序的数据缓存功能。在Spring Boot应用程序中,我们可以通过Spring Caching来快速搞定数据缓存。
程序员追风
2019/09/06
5300
3步轻松搞定Spring Boot缓存
Spring Boot2.x-03Spring Boot基础
Spring Boot-Spring Tool Suit + Gradle 构建第一个Spring Boot 项目01
小小工匠
2021/08/17
3360
【Spring Boot】010-Spring Boot整合Mybatis
链接:https://pan.baidu.com/s/17YUn1yEA-U4Ju-a20EjuFg 提取码:zibo
訾博ZiBo
2025/01/06
470
【Spring Boot】010-Spring Boot整合Mybatis
SpringBoot原理?属性配置?在这里
找到pom.xml中的spring-boot-starter-parent,Crtl+点击跳转到了spring-boot-starter-parent的pom.xml,xml配置如下(部分摘录):
程序员的时光001
2020/07/30
4970
SpringBoot原理?属性配置?在这里
Spring Boot 整合 JDBC Template 实例
在 Spring Boot 中,对于数据访问层,无论是关系型数据库(SQL)还是非关系型数据库(No
村雨遥
2022/06/15
2520
Spring Boot 整合 JDBC Template 实例
Spring Boot 通过 XML 的方式整合 MyBatis
本篇博客主要利用 SpringBoot 通过 XML 的方式整合 Mybatis,所有涉及的代码已经上传 Github mybatis-xml[1]。下面是整个整合过程,接下来开始整合:
村雨遥
2020/08/04
9030
Spring Boot 通过 XML 的方式整合 MyBatis
相关推荐
Spring Boot 配置详解
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验