配置端口
随机数再配置文件的使用
# 随机字符串
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
注意:解密只是控制台给你输出加密前的结果,不会修改配置文件,只有加密才会修改配置文件