SpringBoot中如何详细解读配置文件中的各项参数?

2026-04-19 20:213阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计591个文字,预计阅读时间需要3分钟。

SpringBoot中如何详细解读配置文件中的各项参数?

java

一、@Value读取application.properties配置文件中的值

配置文件:application.properties内容:fileName=configName

类文件:PropertiesConfig.javaimport org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;

@Componentpublic class PropertiesConfig { @Value(${fileName}) private String configName;}

一、@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类文件

SpringBoot中如何详细解读配置文件中的各项参数?

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项目地址

到此这篇关于详解SpringBoot注解读取配置文件的方式的文章就介绍到这了,更多相关SpringBoot注解读取配置文件内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签:方式

本文共计591个文字,预计阅读时间需要3分钟。

SpringBoot中如何详细解读配置文件中的各项参数?

java

一、@Value读取application.properties配置文件中的值

配置文件:application.properties内容:fileName=configName

类文件:PropertiesConfig.javaimport org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;

@Componentpublic class PropertiesConfig { @Value(${fileName}) private String configName;}

一、@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类文件

SpringBoot中如何详细解读配置文件中的各项参数?

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项目地址

到此这篇关于详解SpringBoot注解读取配置文件的方式的文章就介绍到这了,更多相关SpringBoot注解读取配置文件内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

标签:方式