c++ properties_Spring Boot指定外部application.properties配置文件启动(实践)

1.修改pom.xml,过滤src/main/resources目录下.properties文件

e938d794607a403576446e1b0905026a.png

pom.xml

pom.xml代码:

 XXXXorg.springframework.boot                spring-boot-maven-plugin            src/main/resources**/*.properties

2.在resources目录下创建META_INF/spring.factories文件(注意META_INF文件夹也是要创建的)

99f450a433949a0268117f96ff497a4e.png

spring.factories

spring.factories内容:

org.springframework.boot.env.EnvironmentPostProcessor=com.config.LocalSettingsEnvironmentPostProcessor

3.新建LocalSettingsEnvironmentPostProcessor.java加载外部属性文件application.properties

6c9aa00a53b035fd9e5ad358633230a2.png

LocalSettingsEnvironmentPostProcessor

LocalSettingsEnvironmentPostProcessor.java代码如下:

package com.config;import org.springframework.boot.SpringApplication;import org.springframework.boot.env.EnvironmentPostProcessor;import org.springframework.core.env.ConfigurableEnvironment;import org.springframework.core.env.MutablePropertySources;import org.springframework.core.env.PropertiesPropertySource;import org.springframework.core.io.FileSystemResource;import org.springframework.core.io.support.PropertiesLoaderUtils;import java.io.File;import java.io.IOException;import java.util.Properties;/** * @author 光州程序猿 * @version 1.0 */public class LocalSettingsEnvironmentPostProcessor implements EnvironmentPostProcessor {    private static final String LOCATION = "C:甥敳獲Desktopapplication.properties";    @Override    public void postProcessEnvironment(ConfigurableEnvironment configurableEnvironment, SpringApplication springApplication) {        File file = new File(LOCATION);        try {            MutablePropertySources propertySources = configurableEnvironment.getPropertySources();            Properties properties = loadProperties(file);            propertySources.addFirst(new PropertiesPropertySource("Config", properties));        } catch (Exception e) {            e.printStackTrace();        }    }    private Properties loadProperties(File f) {        FileSystemResource resource = new FileSystemResource(f);        try {            return PropertiesLoaderUtils.loadProperties(resource);        } catch (IOException ex) {            throw new IllegalStateException("Failed to load local settings from " + f.getAbsolutePath(), ex);        }    }}

启动SpringBoot项目,指定外部application.properties启动成功。

b264729781826f2f5322d0595227667a.png

启动结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 VS Code 中配置 51 单片机环境,需要进行以下步骤: 1. 安装 C/C++ 扩展 在 VS Code 中搜索并安装 C/C++ 扩展,以便在代码编写时获得语法高亮、智能提示等功能。 2. 配置 c_cpp_properties.json 文件 在项目根目录下创建一个名为 c_cpp_properties.json 的文件,并按照以下格式进行配置: ``` { "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "C:/Keil_v5/ARM/INC" ], "defines": [], "compilerPath": "C:/Keil_v5/ARM/ARMCC/bin/armcc.exe", "cStandard": "c11", "cppStandard": "c++17", "intelliSenseMode": "${default}" } ], "version": 4 } ``` 其中,配置项的含义如下: - configurations:配置项数组。 - name:配置项名称。 - includePath:头文件搜索路径。 - defines:预处理器定义。 - compilerPath:编译器路径。 - cStandard:C 语言标准。 - cppStandard:C++ 标准。 - intelliSenseMode:IntelliSense 模式。 3. 配置 launch.json 文件 在项目根目录下创建一个名为 launch.json 的文件,并按照以下格式进行配置: ``` { "version": "0.2.0", "configurations": [ { "name": "Debug 51 MCU", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/project_name", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "C:/Keil_v5/ARM/ARMCC/bin/arm-none-eabi-gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] } ``` 其中,配置项的含义如下: - name:配置项名称。 - type:调试器类型。 - request:请求类型。 - program:可执行文件路径。 - args:命令行参数。 - stopAtEntry:是否在程序入口处停止。 - cwd:工作目录。 - environment:环境变量。 - externalConsole:是否在外部控制台中运行。 - MIMode:GDB 后端模式。 - miDebuggerPath:GDB 路径。 - setupCommands:执行的命令。 配置完成后,就可以在 VS Code 中进行 51 单片机代码的编写、调试等操作了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值