• java:application.properties的详细使用以及区分环境


    什么是application.properties文件?

    在Java开发中,配置文件是一个重要的部分,它允许我们灵活地调整程序的行为,而不需要修改源代码。在Spring Boot框架中,常用的配置文件是application.properties。本篇博客将介绍如何使用Java和application.properties配置文件。

    application.properties是Spring Boot应用程序的默认属性文件。它包含了应用程序的各种配置信息,如数据库连接、日志级别、应用程序端口等等。这些配置信息可以在程序运行时被读取和使用。

    如何在Java中使用application.properties文件?

    在Java中使用application.properties文件有多种方式,其中最常用的方式是使用Spring Boot的@Value注解。以下是一个简单的例子:

    package com.zhangyu.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloWorldController {
        @Value("${info.app.name}")
        String appName;
    
        @RequestMapping("/hello")
        public String index(){
            return this.appName;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    application.properties

    info.app.name=spring-boot-test
    
    • 1

    如果遇到乱码问题,可以在 setting 中配置
    在这里插入图片描述

    将数据注入到 Bean 中

    有时候属性太多了,一个个绑定到属性字段上太麻烦,官方提倡绑定一个对象的 bean。

    (1)首先我们创建一个名为 My 的 Bean,并将前面的配置数据注入到这个 Bean 中。

    1、@ConfigurationProperties 中的 prefix 属性描述了要加载的配置文件的前缀。
    2、Spring Boot 采用了一种宽松的规则来进行属性绑定:
    假设 Bean 中的属性名为 authorName,那么配置文件中的属性可以是 my.author_name、my.author-name、my.authorName 或者 my.AUTHORNAME

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
     
    @Component
    @ConfigurationProperties(prefix = "my")
    public class My {
        private String name;
        private String age;
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getAge() {
            return age;
        }
     
        public void setAge(String age) {
            this.age = age;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    (2)然后我们在 Controller 中引入这个 Bean 使用即可:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.bind.annotation.GetMapping;
     
    @RestController
    public class HelloController {
        @Autowired
        My my;
     
        @GetMapping("/hello")
        public String hello() {
            return my.getName() + " : " + my.getAge();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    使用自定义的配置文件

    有时候我们不希望把所有配置都放在 application.properties 里面,这时候我们可以另外定义一个。

    假设我们自定义的配置文件是 test.properties,放在 src/main/resources 下面。新建一个 bean 类后,通过如下方式将这个自定义的配置文件数据注入进来:

    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
      
    @Component
    @ConfigurationProperties(prefix = "my")
    @PropertySource("classpath:test.properties")
    public class My {
        private String name;
        private String age;
      
        public String getName() {
            return name;
        }
      
        public void setName(String name) {
            this.name = name;
        }
      
        public String getAge() {
            return age;
        }
      
        public void setAge(String age) {
            this.age = age;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    使用命令行参数进行配置

    (1)在命令行中通过 java -jar 命令启动项目时,可以使用连续的两个减号 – 对 application.properties 中的属性值进行赋值。
    (2)比如下面命令修改 tomcat 端口号为 8081。其等价于在 application.properties 中添加属性 server.port=8081:
    注意:如果 application.properties 中已经有同名属性,那么命令行属性会覆盖 application.properties 的属性。

    java -jar xx.jar --server.port=8081
    
    • 1

    配置文件的优先级

    (1)Spring Boot 项目中的 application.properties 配置文件一共可以出现在如下 4 个位置(优先级逐渐降低):

    • 项目根目录下的 config 文件夹
    • 项目根目录下
    • classpath 下的 config 文件夹
    • classpath 下

    在这里插入图片描述

    加载外部的配置文件

    (1)项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定外部配置文件的位置。

    java -jar xxx.jar --spring.config.location=/Volumes/BOOTCAMP/application.properties
    
    • 1

    (2)当然我们也可以指定外部配置所在的文件夹,启动时会搜索并使用该文件夹下的配置文件:

    java -jar xxx.jar --spring.config.location=/Volumes/BOOTCAMP/
    
    • 1

    (3)我们还可以同时配置多个路径,比如下面样例先加载外部配置文件,如果不存在外部配置文件的话则使用包内默认的配置文件:

    java -jar xxx.jar --spring.config.location=/Volumes/BOOTCAMP/application.properties,classpath:/,classpath:/config/
    
    • 1

    多环境配置

    我们一般都会有多个应用环境,开发环境、测试环境、生产环境,各个环境的配置会略有不同,我可以根据这个创建多份配置文件,由主配置文件来控制读取那个子配置

    1、创建配置文件

    // {profile}是变量用于自定义配置文件名称
    application-{profile}.properties
    
    • 1
    • 2

    application-dev.properties

    info.app.name=spring-boot-dev
    info.app.version=1.0.0
    
    • 1
    • 2

    application-test.properties

    info.app.name=spring-boot-test
    info.app.version=1.0.0
    
    • 1
    • 2

    application-prod.properties

    info.app.name=spring-boot-prod
    info.app.version=1.0.0
    
    • 1
    • 2

    2、在 application.properties 中配置环境

    假设我们在 application.properties 中进行如下配置,则表示使用 application-dev.properties 配置文件启动项目。

    spring.profiles.active=dev
    
    • 1

    3、在项目启动时配置环境

    java -jar xxx.jar --spring.profiles.active=dev
    
    • 1

    配置文件加密

    这个配置文件也用来存储敏感信息,如数据库凭据或其他机密信息。为了保护这些敏感信息,可以使用Spring Boot提供的加密特性对它们进行加密。

    平时工作中,可能会使用配置中心来放敏感配置

    <dependency>  
        <groupId>org.springframework.bootgroupId>  
        <artifactId>spring-boot-starter-data-cryptoartifactId>  
    dependency>
    
    • 1
    • 2
    • 3
    • 4

    用这个,这里我简单讲一下流程
    1、引入

    <dependency>
    	<groupId>com.github.ulisesbocchiogroupId>
    	<artifactId>jasypt-spring-boot-starterartifactId>
    	<version>3.0.4version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、加密

    java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=zhangyu algorithm=PBEWithMD5AndDES
    
    • 1
    • input :明文密码
    • password:要加的盐(可自己设置)
    • algorithm:加密算法,这里使用 PBEWithMD5AndDES

    得到一个密文,放入配置文件中

    datasource.password=ENC(加密后的密文)
    
    • 1

    3、使用

    @RestController
    public class DemoController {@Value("${datasource.password}")
        private String dataSourcePwd;@PostConstruct
        public void init(){
            System.out.println("dataSourcePwd:"+dataSourcePwd);
        }}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    https://zhuanlan.zhihu.com/p/531354051

  • 相关阅读:
    CTF-FTP后门利用【简单易懂】
    树莓派ubuntu上配置miniconda并创建虚拟环境
    微信小程序| 基于ChatGPT+明基屏幕挂灯实现超智能家居物联网小程序
    PNPM 高效入门:安装配置一本通
    Java常用类,这一次帮你总结好
    react实现keepAlive 可手动清除缓存的
    Java 提取HTML文件中的文本内容
    计算机系统(21)----- 信号量实现进程互斥、同步、前驱关系
    系统集成|第九章(笔记)
    Linux 常用压缩格式
  • 原文地址:https://blog.csdn.net/weixin_43972437/article/details/134548319