文章目录
- 前言
- 一、读取核心配置文件信息的内容
- 二、xiao属性注入到配置类中不同的两种写法
- 三、读取自定义配置文件信息
- 四、分环境配置
- 4.1 误区
- 五、读取resources目录下的文件
- 六、读取 json 配置文件(绝对路径参数)
前言
Spring-Boot的核心配置文件是,会默认读取该配置文件,当然也可以通过注解自定义配置文件的信息。开发中,经常会有一些常量,变动较少,但是我们不能在java代码中写死,这样每次修改都得去java代码中修改,引用地方较多时我们需要修改很多java文件;所以我们可以集中写在某个配置文件中,这样只用修改配置文件就好。
:
<parent>
<groupId></groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.</version>
</parent>
<dependencies>
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
注意:你在中添加如下配置,不加的话当你使用@ConfigurationProperties注解的时候会报Warning(黄色小感叹号),但是不影响功能实现(强迫症的我看的不爽)。
<dependency>
<groupId></groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
原因:spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了
一、读取核心配置文件信息的内容
核心配置文件是指在resources根目录下的或配置文件,读取这两个配置文件的方法有两种,都比较简单。
核心配置文件内容如下,这里有普通的定义属性,也有内置的函数,如随机数,随机字符串等:
# 自定义属性
=123.45.67.89
# 参数间引用
=This is id:${}
# 32位随机字符串
=${}
# 随机int
=${}
# 随机long
=${}
# 10以内的随机数
.test1=${(10)}
# 10-20的随机数
.test2=${[10,20]}
=china school
[0].name=tom
[1].name=jack
=Hello World SpringBoot
xiao=qiang
方式一:使用@Value方式(常用)
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
@Value("${}")
private String id;
@Value("${}")
private String description;
@Value("${}")
private String value;
@Value("${}")
private int number;
@Value("${}")
private String bignumber;
@Value("${.test1}")
private String test1;
@Value("${.test2}")
private int test2;
@RequestMapping("/index1")
public String index1(){
return "方式一:"+"id->"+id+";description->"+description+";value->"+value
+";number->"+number+";bignumber->"+bignumber+";test1->"+test1+";test2->"+test2;
}
}
注意:在@Value的${}中包含的是核心配置文件中的键名。在Controller类上加@RestController表示将此类中的所有视图都以JSON方式显示,类似于在视图方法上加@ResponseBody。
访问:http://localhost:8080/index1
时得到:方式一:id->123.45.67.89;description->This is id:123.45.67.89;value->856f604db7c4b03da21b29776dc0d3dc;number->1522979010;bignumber->-8020258677837408781;test1->6;test2->13
方式二:把某种相关的配置,注入到某个配置类中,比如上面的 中看到 的配置项,想注入到 ClassConfig 配置类中。还有集合的注入方法
public class StudentConfig {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="class")
public class ClassConfig {
private String schoolName;
private List<StudentConfig> students = new ArrayList<StudentConfig>();
public String getSchoolName() {
return schoolName;
}
public void setSchoolName(String schoolName) {
this.schoolName = schoolName;
}
public void setStudents(List<StudentConfig> students) {
this.students = students;
}
public List<StudentConfig> getStudents() {
return students;
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hui.config.ClassConfig;
@RestController
public class WebController {
@Value("${}")
private String schoolName;
@Autowired
private ClassConfig classConfig;
@RequestMapping("/index2")
public String index1(){
return "方式二:"+"schoolName="+classConfig.getSchoolName()+"或者->"+schoolName+";name1="+classConfig.getStudents().get(0).getName()
+";name2="+classConfig.getStudents().get(1).getName();
}
}
访问:http://localhost:8080/index2
时得到:方式二:schoolName=china school或者->china school;name1=tom;name2=jack
方式三:使用 Environment 方式
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 WebController {
@Autowired
private Environment env;
@RequestMapping("/index3")
public String index3(){
return "方式三:"+env.getProperty("");
}
}
注意:这种方式是依赖注入Evnironment来完成,在创建的成员变量private Environment env上加上 @Autowired
注解即可完成依赖注入,然后使用 (“键名”)即可读取出对应的值。
访问:http://localhost:8080/index3
时得到:方式三:Hello World SpringBoot
二、xiao属性注入到配置类中不同的两种写法
写法一:
import org.springframework.stereotype.Component;
@Component
public class Configa {
@Value("${name}")
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
写法二:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties //和@ConfigurationProperties(prefix="")等效,不可省略否则会找不到为null
//@PropertySource(value = { "" }) //可省略不写,默认读取
public class Configa {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
三、读取自定义配置文件信息
写法一:
为了不破坏核心文件的原生态,但又需要有自定义的配置信息存在,一般情况下会选择自定义配置文件来放这些自定义信息,这里在resources目录下创建配置文件。resources/内容如下:
author.name=Solin
author.age=22
创建管理配置的实体类:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Component
//@ConfigurationProperties(prefix = "author",locations = "classpath:")
@ConfigurationProperties(prefix = "author")
@PropertySource("classpath:/")
//多配置文件引用,若取两个配置文件中有相同属性名的值,则取值为最后一个配置文件中的值
//@PropertySource({"classpath:/","classpath:/"})
public class MyWebConfig{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
注意:
springboot 1.5 版本以前 @ConfigurationProperties
注释中有两个属性:
locations:指定配置文件的所在位置
prefix:指定配置文件中键名称的前缀(我这里配置文件中所有键名都是以author.开头)
springboot 1.5 版本以后 @ConfigurationProperties
没有了 location 属性,使用@PropertySource
来指定配置文件位置
创建测试Controller:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ConfigController {
@Autowired
private MyWebConfig conf;
@RequestMapping("/test")
public @ResponseBody String test() {
return "Name:"+conf.getName()+"---"+"Age:"+conf.getAge();
}
}
注意:由于在Conf类上加了注释 @Component
,所以可以直接在这里使用 @Autowired
来创建其实例对象。
访问:http://localhost:8080/test
时得到:Name:Solin---Age:22
写法二:resources/
内容如下:时尚=美搭
public static String getNameFrom(String codeName) throws IOException {
InputStream in = Code2Name.class.getClassLoader().getResourceAsStream("");
Properties props = new Properties();
props.load(in);
System.out.println("nameFrom:" + codeName + "->" + props.getProperty(codeName));
in.close();
return props.getProperty(codeName);
}
提炼成工具类:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtil {
private static Properties properties;
public static Properties getProperties(String configName) {
properties = new Properties();
InputStream in = null;
try {
in = PropertiesUtil.class.getClassLoader().getResourceAsStream(configName);
properties.load(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != in) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return properties;
}
public static String getProperty(String configName, String key) {
if (properties == null) {
properties = getProperties(configName);
}
return properties.getProperty(key);
}
}
四、分环境配置
在 src/main/resources
下添加类似这样的文件 ,然后在
中添加
=xxx
配置则说明首先读取的是 而不是
,当使用
@Value
时则先去找,如果
没有则再找
,如果两个文件都没有则会报错
Could not resolve placeholder '.test1' in value "${.test1}"
4.1 误区
1.认为分环境配置可以配置多个
我实践后发现只能配一个,如在 中这样配置:
spring.profiles.active=pro
spring.profiles.active=dev
你可能会认为先找 文件,再找
,最后找
。但实际上是
中最下面的生效,即先找
文件,再找
文件,没有则会报错
2.两个配置文件即内容如下:
application.properties name=hehe
hui.properties name=haha
配置类写法:
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties
@PropertySource("classpath:/")
public class Configa {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Controller写法:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hui.config.Configa;
@RestController
public class WebController {
@Autowired
private Configa configa;
@RequestMapping("/index")
public String index1(){
return configa.getName();
}
}
问:最后返回的值是什么,是hehe还是haha?
答:最后返回的是hehe,当 和其他配置文件有相同属性名则
优先级大
五、读取resources目录下的文件
注:这里读取的不是配置文件,而是其他类型的文件,如 txt、csv 等格式的文件
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class DomainConvertUtil {
public static List<String> getChineseName(String word) {
List<String> list = new ArrayList<String>();
try {
InputStream io = Thread.currentThread().getContextClassLoader().getResourceAsStream("");
InputStreamReader isr = new InputStreamReader(io, "utf-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
if (line.contains(word)) {
String[] split = line.split(",");
list.add(split[0]);
list.add(split[1]);
list.add(split[2]);
list.add(split[3]);
return list;
}
}
br.close();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
注意:读取 resources 目录下文件的名称不要用中文,我在 idea 中有次就用中文命名了文件,用主方法 main 调用上面的的 getChineseName 是能读到文件内容的,可是当用 mvn clean compile package
命令打包后放到 Linux 中运行死活读取不到该文件,总是报 空指针的错误,后来把文件名改为英文就不报错了。
参考:
/qq_32786873/article/details/52840745
/weixin_39800144/article/details/78837603
/dream_broken/article/details/72385295
/flygoa/article/details/58075398
六、读取 json 配置文件(绝对路径参数)
maven 依赖:
<dependency>
<groupId></groupId>
<artifactId>fastjson</artifactId>
<version>2.0.25</version>
</dependency>
public static void main(String[] args) {
String jsonPath;
if (args.length == 1) {
jsonPath = args[0];
} else {
System.out.println("请检查参数!!!参数为配置文件json的绝对路径");
return;
}
JSONObject configJson = getJsonConfig(jsonPath);
}
public static JSONObject getJsonConfig(String jsonPath) {
try {
// 读取文件内容到字符串
String content = new String(Files.readAllBytes(Paths.get(jsonPath)));
// 使用将字符串转换为JSONObject
JSONObject configJson = JSON.parseObject(content);
return configJson;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
使用方法:
spark-submit --class com.xiaoqaing.HbaseExportToHdfs --name HbaseExportToHdfs --master yarn --deploy-mode client --driver-memory 1G --driver-cores 1 --executor-memory 2G --executor-cores 3 --conf spark.driver.maxResultSize=3g spark-hbasetostarrocks-1.0-SNAPSHOT-jar-with-dependencies.jar /test/dws_stduent_t.json
如果是放在 resources 文件夹下:
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
ClassPathResource resource = new ClassPathResource("");
InputStream inputStream = resource.getStream();
// 主要是这个工具类
String string = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
// 此处用的是fastjson,如果json文件是集合,也可以用JsonArray转
AaVo resultVo = JSONObject.parseObject(string, AaVo.class);