详解SpringBoot注解读取配置文件的方式

作者:倾如故 时间:2023-08-05 02:51:16 

一、@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

标签:SpringBoot,注解,读取配置文件
0
投稿

猜你喜欢

  • 深入浅出探索Java分布式锁原理

    2021-12-16 11:58:07
  • Java Gradle项目中的资源正确获取方式

    2022-10-05 09:00:50
  • 出现SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder“.的解决方法

    2021-09-01 12:02:09
  • SpringMVC加载控制与Postmand的使用和Rest风格的引入及RestFul开发全面详解

    2021-10-14 16:45:44
  • Mybatis中注入执行sql查询、更新、新增及建表语句案例代码

    2023-03-07 18:53:55
  • Java 判断字符串中是否包含中文的实例详解

    2023-11-06 13:17:18
  • SpringBoot自动装配原理详解

    2023-07-03 05:49:08
  • Ubuntu 20.04 下安装配置 VScode 的 C/C++ 开发环境(图文教程)

    2021-12-01 16:48:23
  • java的Jackson将json字符串转换成泛型List

    2021-10-06 20:17:49
  • Android 安全加密:Https编程详解

    2023-11-08 06:58:51
  • Android中.9.png图片的使用及制作

    2023-04-05 02:50:36
  • springboot配置Jackson返回统一默认值的实现示例

    2023-12-08 13:55:22
  • Android使用lottie加载json动画的示例代码

    2021-06-20 00:47:01
  • java实现在线聊天系统

    2021-07-30 08:20:54
  • Java使用pulsar-flink-connector读取pulsar catalog元数据代码剖析

    2023-11-05 17:25:41
  • Adapter模式实战之重构鸿洋集团的Android圆形菜单建行

    2021-11-02 09:42:32
  • C#中委托的+=和-=深入研究

    2023-05-31 01:00:15
  • C#判等对象是否相等的方法汇总

    2023-07-13 22:55:07
  • spring-cloud入门之eureka-client(服务注册)

    2023-12-16 22:42:51
  • Android中的全局变量与局部变量使用小结

    2023-12-07 10:11:49
  • asp之家 软件编程 m.aspxhome.com