首页 > 其他分享 >SpringBoot教程(二)springboot的配置文件

SpringBoot教程(二)springboot的配置文件

时间:2023-09-13 11:34:43浏览次数:37  
标签:SpringBoot 配置文件 springframework application org import properties springboot


一.springboot配置文件的类型

application.properties application.yml

项目结构,因为不可以同时使用这两种文件 启动时任选一个放到resources下即可 

SpringBoot教程(二)springboot的配置文件_springboot yml文件

二.properties配置文件的使用

SpringBoot教程(二)springboot的配置文件_springboot教程_02

SpringBoot教程(二)springboot的配置文件_springboot配置文件_03

package com.lpinfo.shop.lpinfoshop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class appController {

    @Value("${server.port}")
    String runPort;

    @Autowired
    Environment env;

    @RequestMapping("/queryAppInfo")
    public String queryAppInfo(){
        return runPort+"["+env.getProperty("server.tomcat.uri-encoding");
    }
}

启动访问结果

SpringBoot教程(二)springboot的配置文件_springboot_04

三.多环境配置文件

在现实的开发环境中,我们需要不同的配置环境;格式为application-{profile}.properties,其中{profile}对应你的环境标识,比如:

  • application-test.properties:测试环境
  • application-dev.properties:开发环境
  • application-prod.properties:生产环境

spring.profiles.active=dev/test/prod  就使用哪个环境的配置文件

SpringBoot教程(二)springboot的配置文件_springboot yml文件_05

 application-dev.properties 对应开发环境  (2)application-test.properties 对应测试环境  (3)application-pro.properties 对应生产环境

 对于哪个配置会生效,需要在application.properties中通过spring.profiles.active属性来设置,其值对应{profile}值,例如:

  spring.profiles.active=dev 就会加载开发环境配置的信息。

四.项目地址

https://pan.baidu.com/s/18QAe1ezpQwrMRJrbIRJmkg

 

标签:SpringBoot,配置文件,springframework,application,org,import,properties,springboot
From: https://blog.51cto.com/ratelcloud/7453280

相关文章

  • springboot发布部署web jar包
    1.在idea中生成jar包文件 2.我这个项目使用的是JavaJDK20,所以要在官网下载这个版本在服务器上安装。https://www.oracle.com/java/technologies/downloads/   有些系统需要重启下服务器才会生效。 3.把第一步生成的 demo-0.0.1-SNAPSHOT.jar文件复制到服务器......
  • Springboot @ConfigurationProperties对象 静态方法调用无效
    一.问题1.springboot使用 @ConfigurationProperties注入对象普通方法调用可以 在静态方法中调用的时候读取不到参数二.文件packagecom.lpinfo.framework.config;@Data@Component@PropertySource("classpath:/oss.properties")@ConfigurationProperties(prefix="oss")......
  • SpringBoot+Mybatis三级查询
    一、概述现有一个需求:查询视频详情。对应三张表,视频表、章节列表、集列表。一个视频对应多个章节,一个章节对应多集视频。请根据视频id查询视频详情,并把视频的章节列表,章节中的集列表都带出来。SpringBoot和MyBatis-plus说明:<!--根模块继承了SpringBoot,子模块也跟着继......
  • springboot日志配置
    配置文件使用xml配置日志保存(并不需要pom配置slf4j依赖,starter里面已经配置了依赖了)在项目的resources目录下创建一个【logback-spring.xml】日志配置文件名称只要是一logback开头就行,测试使用log.xml并不会生成日志。合法名称:logback.xml、logback-spring.xml备注:要配置l......
  • SpringBoot项目启动报错:An incompatible version [1.1.22] of the Apache Tomcat Nati
    问题解释:“安装了不兼容的ApacheTomcat原生库版本[1.1.22],而Tomcat需要版本[1.2.14]”解决方法:①打开网页 http://archive.apache.org/dist/tomcat/tomcat-connectors/native/②        ③        ④     ......
  • springboot中的文件上传与下载
    首先回忆一下springmvc中的文件上传1)引入文件上传相关jar包,commons-io、commons-fileupload2)文件上传表单提交方式必须为post3)要求表单的enctype属性必须为:multipart/form-data4)后台接收文件时,使用multipartFile变量与前端name属性值保持一致5)在springmvc的配置文件中必须......
  • springboot单元测试
    参考文章:SpringBoot单元测试详解_springboottest单元测试_三分恶的博客-CSDN博客JUnit4(三)高级之assertThat和Matchers(匹配器)_FXBStudy的博客-CSDN博客 一:junit测试当你的单元测试代码不需要用到SpringBoot功能,而只是一个简单的测试时,你可以直接编写你的Junit测试......
  • 详解SpringBoot下文件上传与下载的实现
    SpringBoot后台如何实现文件上传下载?最近做的一个项目涉及到文件上传与下载。前端上传采用百度webUploader插件。有关该插件的使用方法还在研究中,日后整理再记录。本文主要介绍SpringBoot后台对文件上传与下载的处理。单文件上传/单文件上传@RequestMapping(value="/uploa......
  • springBoot spring6 无法加载 thymeleaf的,在html页面中无法智能感知 th:这些
    网上所有的坑我都试过了,还是无法解决问题,@ControllerpublicclassSellController{@RequestMapping("/test01")/*@ResponseBody*/publicStringindex(){return"test01";}}”test01“下面永远是波浪线,无论把网上查出来的解决问题的方案我......
  • docker部署springboot+vue项目环境安装及部署流程
    后端项目打jar包修改及配置项1、修改配置文件application-prod.yml中的mysql配置和redis配置2、切换Maven为生产模式3、Maven打包4、拿到打包后的jar 包5、拿到jar包之后在Linux中使用nohupjava-jarjeecg-system-start-3.5.0.jar>catalina.out2>&1&命令即可启动项......