详解SpringBoot注解读取配置文件的方式
作者:倾如故 发布时间:2023-08-05 02:51:16
标签:SpringBoot,注解,读取配置文件
一、@Value读取application.properties配置文件中的值
application.properties配置文件
fileName=configName
PropertiesConfig类文件
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class PropertiesConfig {
//通过@Value注解读取fileName的值
@Value("${fileName}")
private String fileName;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
测试
import com.model.PropertiesConfig;
import org.junit.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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertiesConfigTest {
@Autowired
private PropertiesConfig propertiesConfig;
@Test
public void test(){
System.out.println(propertiesConfig.getFileName());//结果输出:configName
assert "configName".equals(propertiesConfig.getFileName());
}
}
二、@ConfigurationProperties读取多个application.properties配置文件中的值
application.properties文件
database.username=root
database.password=root
DatabaseConfig类文件
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties("database")
public class DatabaseConfig {
private String userName;
private String passWord;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
测试
import com.model.DatabaseConfig;
import org.junit.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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class DatabaseConfigTest {
@Autowired
private DatabaseConfig databaseConfig;
@Test
public void test(){
System.out.println("username = " + databaseConfig.getUserName() +", password = "+databaseConfig.getPassWord());//结果输出:username = root, password = root
assert "root".equals(databaseConfig.getUserName());
assert "root".equals(databaseConfig.getPassWord());
}
}
三、@PropertySource读取任意配置文件
新建property-source.properties配置文件
fileName=configName
database.username=root
database.password=root
PropertySourceConfig类文件
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"classpath:property-source.properties"})
@ConfigurationProperties("database")
public class PropertySourceConfig {
@Value("${fileName}")
private String fileName;
private String userName;
private String passWord;
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
}
测试
import com.model.PropertySourceConfig;
import org.junit.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;
@RunWith(SpringRunner.class)
@SpringBootTest
public class PropertySourceConfigTest {
@Autowired
private PropertySourceConfig propertySourceConfig;
@Test
public void test(){
assert "configName".equals(propertySourceConfig.getFileName());
System.out.println("fileName = " + propertySourceConfig.getFileName());//结果输出:configName
assert "root".equals(propertySourceConfig.getUserName());
System.out.println(propertySourceConfig.getUserName());//结果输出:root
assert "root".equals(propertySourceConfig.getPassWord());
System.out.println(propertySourceConfig.getPassWord());//结果输出:root
}
}
完整代码链接:read-config-file项目地址
来源:https://juejin.cn/post/6927101340178972686
0
投稿
猜你喜欢
- java 实现MD5加密算法的简单实例实现代码:import java.security.NoSuchAlgorithmException;
- ObjectMapper 忽略字段大小写核心代码:ObjectMapper mapper = new ObjectMapper();mapp
- 最近正式入坑Flutter,首先从环境搭建开始,看了网上好多关于Windows环境搭建的资料,基本都是按官方文档写的,看完的感受是,还不如直
- 本文实例为大家分享了opencv实现轮廓高斯滤波平滑的具体代码,供大家参考,具体内容如下一个小测试的题目:在图像上点选,找到与点选处相邻的颜
- 因为mybatis好使,所以几乎需要操作数据库的时候,我都会使用mybatis,而且在一个正式的项目中,同时存在BS和CS的程序,都使用的M
- 声明:下面的实例全部在linux下尝试,window下未尝试。有兴趣者可以试一下。文章针c初学者。c语言的强符号和弱符号是c初学者经常容易犯
- 参考:How to catch an Exception from a threadIs there a way to make Runna
- 逆时针画圆弧,原理:将360度分割成36份,分别标出每10度角度时的坐标点,然后将每个点连接起来。 #include <io
- 简介官方API文档Scaffold的of方法说明有说明调用Scaffold.of方法是在Scallfold的子组件的Build方法中,也就是
- 问题:在用Java程序进行读写含中文的txt文件时,经常会出现读出或写入的内容会出现乱码。原因其实很简单,就是系统的编码和程序的编码采用了不
- 一、方法这里我们用两种方法来实现跑马灯效果,虽然实质上是一种实质就是:1、TextView调出跑马灯效果2、TextView获取焦点&nbs
- 一、广播机制概述通常情况下在学校的每个教室都会装有一个喇叭,这些喇叭是接入到学校广播室的。如果有重要通知,会发送一条广播来告知全校师生。为了
- 前言本文将带您了解在 Flutter 中制作翻转卡片动画的两个完整示例。第一个示例从头开始实现,第二个示例使用第三方包。闲话少说,让我们动手
- 这一篇我们说说Java线程Thread的interrupt中断机制。中断线程线程的thread.interrupt()方法是中断线程,将会设
- 最新开发新项目的时候,要做分享项目,要求分享有微信,微信朋友圈,QQ,QQ空间,新浪微博这五个,所分享内容包括,分享纯图片,纯文字,图文类型
- 一 :问题背景问题:当查询接口较复杂时候,数据的获取都需要[远程调用],必然需要花费更多的时间。 假如查询文章详情页面,需要如下标注的时间才
- 前言我们在学习Windows应用程序开发中,经常会用到消息对话框给用户或者管理员一些的消息提示,它们都是基于对MessageBox类的消息对
- 一、前言(吐槽+煽情+简介) &n
- idea spring Initializr创建项目勾选项目所需要的依赖pom.xml文件会加载勾选的依赖,也可以不勾选后面通过自己常用的p
- 自动登录是我们在软件开发时一个非常常见的功能,例如我们登录 QQ 邮箱:很多网站我们在登录的时候都会看到类似的选项,毕竟总让用户输入用户名密