SpringBoot加载配置文件的几种方式
首先回忆一下在没有使用SpringBoot之前也就是传统的spring项目中是如何读取配置文件,通过I/O流读取指定路径的配置文件,然后再去获取指定的配置信息。
传统项目读取配置方式
- 读取xml配置文件
public String readFromXml(String xmlPath, String property) {
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read(new File(xmlPath));
} catch (DocumentException e) {
e.printStackTrace();
}
Iterator<Element> iterator = doc.getRootElement().elementIterator();
while (iterator.hasNext()){
Element element = iterator.next();
if (element.getQName().getName().equals(property)){
return element.getTextTrim();
}
}
return null;
}
- 读取.properties配置文件
public String readFromProperty(String filePath, String property) {
Properties prop = new Properties();
try {
prop.load(new FileInputStream(filePath));
String value = prop.getProperty(property);
if (value != null) {
return value;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
SpringBoot读取配置方式
如何使用SpringBoot读取配置文件,从使用Spring慢慢演变,但是本质原理是一样的,只是SpringBoot简化配置,通过注解简化开发,接下来介绍一些常用注解。
@ImportResource注解
- 这个注解用来导入Spring的配置文件,是配置文件中的内容注入到配置类中,参数是一个数组,可以注入多个配置文件
- 代码演示:
在SpringBoot项目的resources目录下创建一个xml配置文件beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="configBean" class="com.example.test.config.ConfigBean">
<property name="dbType" value="Oracle"/>
<property name="driverClassName" value="jdbc.driver.Oracle.OracleDriver"/>
<property name="host" value="127.0.0.1"/>
<property name="userName" value="oracle"/>
<property name="password" value="oracle"/>
</bean>
</beans>
创建配置类ConfigBean
package com.example.test.config;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
/**
* @author Vincente
* @date 2020/07/12-12:29
* @desc 配置类
**/
@Setter
@Getter
@ToString
public class ConfigBean {
private String dbType;
private String driverClassName;
private String host;
private String userName;
private String password;
}
添加@ImportResource注解,在SpringBoot项目的启动类添加
package com.example.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
@SpringBootApplication
@ImportResource(locations = {"classpath:beans.xml"})
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
}
测试代码
package com.example.test;
import com.example.test.config.ConfigBean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@SpringBootTest
@RunWith(SpringRunner.class)
class TestApplicationTests {
@Autowired
private ConfigBean configBean;
@Test
void testConfigBean(){
System.out.println(configBean);
}
}
输出结果
ConfigBean(dbType=Oracle, driverClassName=jdbc.driver.Oracle.OracleDriver, host=127.0.0.1, userName=oracle, password=oracle)
- 小结 @ImportResource注解可以用来加载一个外部xml文件,注入到项目完成配置,但是这样引入xml并没有达到SpringBoot简化配置的目的。
@Configuration和@Bean注解
- @Configuration和@Bean注解并不能读取配置文件中的信息,但是这两个类本身用来定义配置类
@Configuration用来代替xml文件,添加在一个类上面
@Bean用来代替bean标签,声明在方法上,方法的返回值返回一个对象到Spring的IoC容器中,方法名称相当于bean标签中的ID
- 代码样例
声明一个bean
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Vincente
* @date 2020/07/12-13:28
* @desc
**/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplateConfig restTemplate(){
return new RestTemplate();
}
}
测试代码
package com.example.test;
import com.example.test.config.RestTemplateConfig;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
class TestApplicationTests {
@Resource
private RestTemplateConfig restTemplate;
@Test
void testConfigBean(){
System.out.println(restTemplate);
}
}
输出结果
com.example.test.config.RestTemplateConfig@de7e193
@Import注解
- @Import注解是用来导入配置类或者一些需要前置加载的类,带有@Configuration的配置类(4.2 版本之前只可以导入配置类,4.2版本之后 也可以导入 普通类)
- 代码样例
结合上面的代码做修改,不全部贴出
将RestTemplateConfigestTemplateConfig类中的@Configuration注解去掉,在ConfigBean中导入
@Setter
@Getter
@ToString
@Import(RestTemplateConfig.class)
public class ConfigBean {
private String dbType;
private String driverClassName;
private String host;
private String userName;
private String password;
}
测试代码
package com.example.test;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
class TestApplicationTests {
@Resource
ApplicationContext ctx;
@Test
void testConfigBean(){
System.out.println(ctx.getBean("restTemplate"));
}
}
输出结果
com.example.test.config.RestTemplateConfig@6cd15072
- 小结 可以看到在IoC容器中已经导入了RestTemplateConfig(普通)类,这个注解类似于之前applicationContext.xml中的import标签
@ConfigurationProperties和@Value
- @ConfigurationProperties和@Value这两个注解算是在SpringBoot中用的比较多的注解了,可以在项目的配置文件application.yml和application.properties中直接读取配置,但是在用法上二者也是有一定的区别
- 代码样例
创建配置文件application.yml
db-config:
db-type: Oracle
driver-class-name: jdbc.driver.Ooracle.OracleDriver
host: 127.0.0.1
user-name: Oracle
password: Oracle
server:
port: 8080
创建配置类ConfigBean
package com.example.test.config;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* @author Vincente
* @date 2020/07/12-12:29
* @desc 配置类
**/
@Setter
@Getter
@ToString
@ConfigurationProperties(prefix = "db-config")
public class ConfigBean {
private String dbType;
private String driverClassName;
private String host;
private String userName;
private String password;
}
测试代码
package com.example.test;
import com.example.test.config.ConfigBean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
class TestApplicationTests {
@Resource
ConfigBean configBean;
@Value("${server.port}")
private String port;
@Test
void testConfigBean(){
System.out.println(configBean);
System.out.println(port);
}
}
输出结果
ConfigBean(dbType=Oracle, driverClassName=jdbc.driver.Ooracle.OracleDriver, host=127.0.0.1, userName=Oracle, password=Oracle)
8080
- 总结 二者的一些区别
| 特性 | @ConfigurationProperties | @Value |
|---|---|---|
| SpEL表达式 | 不支持 | 支持 |
| 属性松散绑定 | 支持 | 不支持 |
| JSR303数据校验 | 支持 | 不支持 |
JSR303校验演示:
添加校验注解
package com.example.test.config;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Null;
/**
* @author Vincente
* @date 2020/07/12-12:29
* @desc 配置类
**/
@Setter
@Getter
@ToString
@ConfigurationProperties(prefix = "db-config")
@Validated
public class ConfigBean {
@Null
private String dbType;
private String driverClassName;
private String host;
private String userName;
private String password;
}
输出结果
Description:
Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'db-config' to com.example.test.config.ConfigBean failed:
Property: db-config.dbType
Value: Oracle
Origin: class path resource [application.yml]:2:12
Reason: 必须为null
@PropertySource注解
- @ConfigurationProperties和@Value这两个注解默认从项目的主配置文件中读取配置,当项目配置较多全部从一个地方读取会显得臃肿,可以将配置文件按照模块拆分读取到不同的配置类中,可以使用@PropertySource配合@Value读取其他配置文件
- 代码样例
创建配置文件db-config.yml
/**
* @author Vincente
* @date 2020/07/12-14:19
* @desc
**/
db-config:
db-type: Oracle
driver-class-name: jdbc.driver.Ooracle.OracleDriver
host: 127.0.0.1
user-name: Oracle
password: Oracle
创建配置类ConfigBean
package com.example.test.config;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @author Vincente
* @date 2020/07/12-12:29
* @desc 配置类
**/
@Setter
@Getter
@ToString
@PropertySource("classpath:db-config.yml")
@Component
public class ConfigBean {
@Value("${db-type}")
private String dbType;
@Value("${driver-class-name}")
private String driverClassName;
@Value("${host}")
private String host;
@Value("${user-name}")
private String userName;
@Value("${password}")
private String password;
}
测试代码
package com.example.test;
import com.example.test.config.ConfigBean;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@SpringBootTest
@RunWith(SpringRunner.class)
class TestApplicationTests {
@Resource
ConfigBean configBean;
@Test
void testConfigBean(){
System.out.println(configBean);
}
}
输出结果
ConfigBean(dbType=Oracle, driverClassName=jdbc.driver.Ooracle.OracleDriver, host=127.0.0.1, userName=Vincente, password=Oracle)
- 小结
@PropertySource 用于获取类路径下的db-config.yml配置文件,@Value用于获取yml中的配置信息,@Component注解用来将配置类交给Spring容器管理
总结
SpringBoot中提供了注解代替配置文件的方式来获取项目中的配置,大大简化了开发,以上总结了常用的读取配置的方法,简单来说就是两种文件(yml和properties)几大注解(@Value,@PropertySource,@Configuration,@ConfigurationProperties,@Import,@Bean);首先要了解每个注解的使用场景后,其次根据项目实际情况来具体的使用
SpringBoot加载配置文件的几种方式的更多相关文章
- java加载配置文件的三种方式
比如我们要加载db.properties文件 如图: 比如我们要加载source目录下的db.properties文件.就有以下几种方式 第一种是文件io流: public static void l ...
- Spring 创建 IOC 容器时加载配置文件的几种方式
一.ClassPathXmlApplicationContext 类路径加载 1. 使用 classpath 路径,classpath 前缀加不加都可以. ApplicationContext act ...
- mybatis 加载配置文件的两种方式
package com.atguigu.day03_mybaits.test; import java.io.IOException;import java.io.InputStream;import ...
- JavaScript判断图片是否加载完成的三种方式
JavaScript判断图片是否加载完成的三种方式 有时需要获取图片的尺寸,这需要在图片加载完成以后才可以.有三种方式实现,下面一一介绍. 一.load事件 1 2 3 4 5 6 7 8 9 10 ...
- WebView加载页面的两种方式——网络页面和本地页面
WebView加载页面的两种方式 一.加载网络页面 加载网络页面,是最简单的一种方式,只需要传入http的URL就可以,实现WebView加载网络页面 代码如下图: 二.加载本地页面 1.加载asse ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...
- [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)
Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...
- Vue加载组件、动态加载组件的几种方式
https://cn.vuejs.org/v2/guide/components.html https://cn.vuejs.org/v2/guide/components-dynamic-async ...
- VUE 动态加载组件的四种方式
动态加载组件的四种方式: 1.使用import导入组件,可以获取到组件 var name = 'system'; var myComponent =() => import('../compon ...
随机推荐
- centos8上配置openresty/nginx可访问php
一,创建一个测试站的目录 [root@yjweb data]# mkdir dev [root@yjweb data]# cd dev [root@yjweb dev]# mkdir think_ww ...
- linux 压缩命令 zip
1.zip命令 例如:zip -r mysql.zip mysql 该句命令的含义是:将mysql文件夹压缩成mysql.zip zip -r abcdef.zip abc def.txt 这句命令的 ...
- 第二十章 nginx常见问题
一.Nginx常见问题 一.nginx多server优先级 在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中的每个server_name进行匹配, ...
- PS编辑工具
3.1PS污点修复 (1)快捷键:J. (2)中括号可以改变笔触的大小,前中括号减小笔触,后中括号增加笔触. (3)可以用选区把需要修复的地方框选上,再进行修复,这样不会影响到未选区域. 3.2PS修 ...
- 理解 Android Binder 机制(一):驱动篇
Binder的实现是比较复杂的,想要完全弄明白是怎么一回事,并不是一件容易的事情. 这里面牵涉到好几个层次,每一层都有一些模块和机制需要理解.这部分内容预计会分为三篇文章来讲解.本文是第一篇,首先会对 ...
- 理解DES算法
首先 了解对称密码加密技术:采用单钥密码系统的加密方法,同一个密钥可以同时用作信息的加密和解密,这种加密方法称为对称加密,也称为单密钥加密.但是有时候密钥不用完全相同 只要相似也可以.因为用一个密钥可 ...
- react-native 签名
完成项目时,我们需要将项目打包成一个apk,方便测试以及发布版本. 这时,需要把js代码和图片资源都放进apk中, 并且发布版本还需要签名,今天把这一系列操作记录下来. 一.生成离线bundle包 离 ...
- Luogu P4172 [WC2006]水管局长
题意 给定一个 \(n\) 个点 \(m\) 条边的图和 \(q\) 次操作,每次操作分为以下两种: 1 u v:查询 \(u\) 到 \(v\) 的一条路径使得边权最大的边的权值最小. 2 u v: ...
- 基于gin的golang web开发:模型验证
Gin除了模型绑定还提供了模型验证功能.你可以给字段指定特定的规则标签,如果一个字段用binding:"required"标签修饰,在绑定时该字段的值为空,那么将返回一个错误.开发 ...
- Js模块化开发的理解
Js模块化开发的理解 模块化是一个语言发展的必经之路,其能够帮助开发者拆分和组织代码,随着前端技术的发展,前端编写的代码量也越来越大,就需要对代码有很好的管理,而模块化能够帮助开发者解决命名冲突.管理 ...