003.SpringBoot配置文件介绍

配置端口

随机数再配置文件的使用

# 随机字符串
com.didispace.blog.value=${random.value}
# 随机int
com.didispace.blog.number=${random.int}
# 随机long
com.didispace.blog.bignumber=${random.long}
# 10以内的随机数
com.didispace.blog.test1=${random.int(10)}
# 10-20的随机数
com.didispace.blog.test2=${random.int[10,20]}

随机端口

 多环境配置

创建3个环境配置,里面的内容分别写了端口=8000、8001、8002

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

 主配置里面写上使用哪个配置:spring.profiles.active=dev

 配置的读取

常规读取

package com.java.core;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@SpringBootApplication
public class CoreApplication {
    //项目启动时执行的方法
    public static void main(String[] args) {
        //SpringApplication.run(CoreApplication.class, args);
        // 获取 Spring Boot 上下文
        ConfigurableApplicationContext ctx = SpringApplication.run(CoreApplication.class, args);
        //获取配置的端口
        //System.out.println(ctx.getEnvironment().getProperty("server.port"));
        //加密数据自动解密
        System.out.println(ctx.getEnvironment().getProperty("admin.password"));

        //配置类的读取
        //Config cg = (Config) ctx.getBean("config");
        //ctx.close();
    }

}

类的读取

package com.java.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Config {
    @Value("${spring.profiles.active}")     private String active;
    @Value("${server.port}")     private  String port;
}

使用 配置类

package com.java.core;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@EnableWebMvc
@SpringBootApplication
public class CoreApplication {
    //项目启动时执行的方法
    public static void main(String[] args) {
        //SpringApplication.run(CoreApplication.class, args);
        // 获取 Spring Boot 上下文
        ConfigurableApplicationContext ctx = SpringApplication.run(CoreApplication.class, args);
        //获取配置的端口
        //System.out.println(ctx.getEnvironment().getProperty("server.port"));
        //加密数据自动解密
        System.out.println(ctx.getEnvironment().getProperty("admin.password"));

        //配置类的读取
        Config cg = (Config) ctx.getBean("config");
        //ctx.close();
    }

}

读取配置类的注意点

 

配置加密:引入加密组件

<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

再加一个plugin

<plugin>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-maven-plugin</artifactId>
    <version>3.0.3</version>
</plugin>

配置文件,写上加密码

jasypt.encryptor.password=didispace

 使用DEC把要加密的配置内容包起来,如

admin.password=DEC(要加密的内容)

替换一个加密组件,组件下载地址

JCE Unlimited Strength Jurisdiction Policy Files for JDK/JRE 8 Download

因为要登录,可以直接F12获取下载地址

Unauthorized Requesthttps://download.oracle.com/otn-pub/java/jce/8/jce_policy-8.zip解压后把文件拷走

拷贝到自己安装java的jre里面

C:\Program Files\Java\jdk1.8.0_144\jre\lib\security

打开终端,准备执行加密

执行加密命令:mvn jasypt:encrypt -Djasypt.encryptor.password=didispace

 加密成功后的配置文件

解密查看原值:

mvn jasypt:decrypt -Djasypt.encryptor.password=didispace

注意:解密只是控制台给你输出加密前的结果,不会修改配置文件,只有加密才会修改配置文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值