spring boot 读取配置文件(application yml)中的属性值

                在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值:

1、引入依赖:
[html] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <!-- 支持 @ConfigurationProperties 注解 -->  
  2. <dependency>  
  3.     <groupId>org.springframework.boot</groupId>  
  4.     <artifactId>spring-boot-configuration-processor</artifactId>  
  5.     <optional>true</optional>  
  6. </dependency>  

2、配置文件(application.yml)中配置各个属性的值:

[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. myProps: #自定义的属性和值  
  2.   simpleProp: simplePropValue  
  3.   arrayProps: 1,2,3,4,5  
  4.   listProp1:  
  5.     - name: abc  
  6.       value: abcValue  
  7.     - name: efg  
  8.       value: efgValue  
  9.   listProp2:  
  10.     - config2Value1  
  11.     - config2Vavlue2  
  12.   mapProps:  
  13.     key1: value1  
  14.     key2: value2  

3、创建一个bean来接收配置信息:
[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Component  
  2. @ConfigurationProperties(prefix="myProps"//接收application.yml中的myProps下面的属性  
  3. public class MyProps {  
  4.     private String simpleProp;  
  5.     private String[] arrayProps;  
  6.     private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值  
  7.     private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值  
  8.     private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值  
  9.       
  10.     public String getSimpleProp() {  
  11.         return simpleProp;  
  12.     }  
  13.       
  14.     //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要  
  15.     public void setSimpleProp(String simpleProp) {  
  16.         this.simpleProp = simpleProp;  
  17.     }  
  18.       
  19.     public List<Map<String, String>> getListProp1() {  
  20.         return listProp1;  
  21.     }  
  22.     public List<String> getListProp2() {  
  23.         return listProp2;  
  24.     }  
  25.   
  26.     public String[] getArrayProps() {  
  27.         return arrayProps;  
  28.     }  
  29.   
  30.     public void setArrayProps(String[] arrayProps) {  
  31.         this.arrayProps = arrayProps;  
  32.     }  
  33.   
  34.     public Map<String, String> getMapProps() {  
  35.         return mapProps;  
  36.     }  
  37.   
  38.     public void setMapProps(Map<String, String> mapProps) {  
  39.         this.mapProps = mapProps;  
  40.     }  
  41. }  


启动后,这个bean里面的属性就会自动接收配置的值了。

4、单元测试用例:

[java] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. @Autowired  
  2.     private MyProps myProps;   
  3.       
  4.     @Test  
  5.     public void propsTest() throws JsonProcessingException {  
  6.         System.out.println("simpleProp: " + myProps.getSimpleProp());  
  7.         System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps()));  
  8.         System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1()));  
  9.         System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2()));  
  10.         System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps()));  
  11.     }  


测试结果:

[plain] view plain copy 在CODE上查看代码片 派生到我的代码片
  1. simpleProp: simplePropValue  
  2. arrayProps: ["1","2","3","4","5"]  
  3. listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]  
  4. listProp2: ["config2Value1","config2Vavlue2"]  
  5. mapProps: {"key1":"value1","key2":"value2"}  


           
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Spring Boot 读取 YAML 文件可以使用注解 `@Value` 或者 `@ConfigurationProperties`。 使用 `@Value` 注解: ```java @Value("${property.name}") private String propertyName; ``` 使用 `@ConfigurationProperties` 注解: ```java @ConfigurationProperties(prefix = "property") @Data public class Property { private String name; } ``` 在 application.yml 文件配置相应的属性: ```yml property: name: value ``` 然后在应用程序通过注入 Bean 的方式使用即可。 ### 回答2: Spring Boot是一个用于构建独立的、生产级别的Spring应用程序的框架,它提供了许多便利的功能,其之一是读取yml文件。 首先,我们需要在Spring Boot项目添加YAML文件。YAML(YAML Ain't Markup Language)是一种简洁且易读的数据序列化格式,通常用于配置文件。 在src/main/resources目录下创建一个名为application.yml的文件。在该文件,我们可以使用键对的形式来配置各种属性。 例如,我们可以配置服务器端口号: server: port: 8080 然后,我们需要创建一个类来读取yml配置文件属性。可以使用@ConfigurationProperties注解将该类与yml文件属性进行绑定。 ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "server") public class ServerProperties { private int port; public int getPort() { return port; } public void setPort(int port) { this.port = port; } } ``` 在上述示例,我们使用@ConfigurationProperties注解指定了yml文件的配置项前缀为"server",然后定义了一个整型属性port,通过对应的getter和setter方法可以获取和设置该属性。 最后,我们可以在其他组件或Bean使用该ServerProperties类来获取yml文件属性。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MyComponent { private final ServerProperties serverProperties; @Autowired public MyComponent(ServerProperties serverProperties) { this.serverProperties = serverProperties; } public void printServerPort() { int port = serverProperties.getPort(); System.out.println("Server port: " + port); } } ``` 在上述示例,我们通过构造函数注入了ServerProperties对象,并在printServerPort方法获取yml文件配置的服务器端口号,然后打印出来。 这样,我们就可以通过Spring Boot读取yml文件属性了。Spring Boot会自动将yml文件属性与@Component注解的类进行绑定,从而方便我们使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值