详解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


猜你喜欢
- 上次老师跟大家分享了 cookie、session和token,今天给大家分享一下Java 8中的Stream API。Stream简介1、
- 命名空间:Windows.Data.Json在Windows Runtime中,可以使用Json类对获取的Json字符串进行操作,相比Dat
- 前言SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。它是一个零配置的数据库,这意味着与其
- 服务降级服务压力剧增的时候,根据当前的业务情况及流量对一些服务和页面有策略的降级,以此缓解服务器的压力,以保证核心任务的进行。同时保证部分甚
- 方法一,修改gradle.properties文件,增加一句gradle.user.home=D\:\\Android\\.gradle但这
- 准备三个框架结合的lib包Spring3结合Struts2的步骤如下:1:开启Struts2结合Spring3,在struts.xml中添加
- XmlTextWriter类允许你将XML写到一个文件中去。这个类包含了很多方法和属性,使用这些属性和方法可以使你更容易地处理XML。为了使
- 一直在使用Mybatis这个ORM框架,都是使用mybatis里的一些常用功能。今天在项目开发中有个业务是需要限制各个用户对某些表里的字段查
- 一、首先我们先创建一个Maven项目把我们需要的包先准备好 1.打开pom.xml文件引入依赖,以下是整个pom.xml文件<
- Java 继承与多态的深入理解1、 什么是继承,继承的特点?子类继承父类的特征和行为,使得子类具有父类的各种属性和方法。或子类从
- 由于Android项目开源所致,市面上出现了N多安卓软件市场。为了让我们开发的软件有更多的用户使用,我们需要向N多市场发布,软件升级后,我们
- 本文实例讲述了Java基于swing实现的弹球游戏代码。分享给大家供大家参考。主要功能代码如下:package Game;import ja
- 1. 概述将对象组合成树形结构以表示“部分-整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。2. 解决的问题当希望忽
- 前言Android开发中经常使用findViewById来获取控件然后进行一些列操作,当控件太多的时候代码就非常臃肿,今天就来学习一个新的开
- 本文实例为大家分享了C#实现文字转语音的具体代码,供大家参考,具体内容如下客户提出要求,将文字内容转为语音,因为内网环境,没办法采用联网,在
- 前言ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素实现了
- 前排提示,我在这个工具类加了@Component注解,如果在springboot的项目使用,记得通过@Autowired注入使用。impor
- 前言之前在SpringBoot项目中一直使用的是SpringFox提供的Swagger库,上了下官网发现已经有接近两年没出新版本了!前几天升
- 一、电子邮件详解假设自己的电子邮件是me@163.com,对方的邮件是you@163.com我们编写好文件填写好对方文件,点击发送,这些电子
- 一、抽象类1.抽象类1.1抽象类的定义在Java面向对象当中,所有的对象都是用过类进行描绘的,但是并不是所有的类都是用来描绘对象的,如果一个