spring boot学习2之properties配置文件读取

目录(?)[+]

      在spring boot学习1 时,知道spring boot会默认读取配置application.properties。那如果我们直接在application.properties添加自定义的配置项时,如何读取?或者不想把所有的配置都放在application.properties中,而是自定义一个properties文件时,又该如何读取呢?难道还需自己写代码加载读取配置文件吗?

注:下面代码是在spring boot 1.5.2版本下写的。

pom.xml

[html]  view plain  copy
  1. <parent>  
  2.     <groupId>org.springframework.boot</groupId>  
  3.     <artifactId>spring-boot-starter-parent</artifactId>  
  4.     <version>1.5.2.RELEASE</version>  
  5. </parent>     
  6. <dependencies>  
  7.     <dependency>  
  8.         <groupId>org.springframework.boot</groupId>  
  9.         <artifactId>spring-boot-starter-web</artifactId>  
  10.     </dependency>  
  11.     </dependencies>  


@Value

   使用@Value注入写在application.properties中的配置项
application.properties
[plain]  view plain  copy
  1. logging.config=classpath:logback.xml  
  2. logging.path=d:/logs  
  3.   
  4.   
  5. ##tomcat set###  
  6. server.port=80  
  7. #session 超时时间  
  8. server.session-timeout=60  
  9. ###########  
  10.   
  11. hello=hello china!!!  
  12.   
  13. class.schoolName=china school  
  14. class.className=second grade three class  
  15. class.students[0].name=tom  
  16. class.students[1].name=jack  

在代码中@Value注入到属性中
[java]  view plain  copy
  1. @Controller  
  2. @RequestMapping("/")  
  3. public class HelloController {  
  4.   
  5.     public static Logger LOG = LoggerFactory.getLogger(HelloController.class);  
  6.       
  7.     @Value("${hello}")  
  8.     private String hello;  
  9.       
  10.     @Value("${class.schoolName}")  
  11.     private String schoolName;  

@ConfigurationProperties

 如果想把某种相关的配置,注入到某个配置类中,比如上面的application.properties中看到class.xx的配置项,想注入到ClassConfig配置类中
[java]  view plain  copy
  1. package com.fei.springboot.config;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import org.springframework.boot.context.properties.ConfigurationProperties;  
  7. import org.springframework.stereotype.Component;  
  8.   
  9. @Component("classConfig")  
  10. @ConfigurationProperties(prefix="class")  
  11. public class ClassConfig {  
  12.   
  13.     private String schoolName;  
  14.       
  15.     private String className;  
  16.       
  17.     private List<StudentConfig> students = new ArrayList<StudentConfig>();  
  18.   
  19.     public String getSchoolName() {  
  20.         return schoolName;  
  21.     }  
  22.   
  23.     public void setSchoolName(String schoolName) {  
  24.         this.schoolName = schoolName;  
  25.     }  
  26.   
  27.     public void setClassName(String className) {  
  28.         this.className = className;  
  29.     }  
  30.   
  31.     public void setStudents(List<StudentConfig> students) {  
  32.         this.students = students;  
  33.     }  
  34.   
  35.     public String getClassName() {  
  36.         return className;  
  37.     }  
  38.   
  39.     public List<StudentConfig> getStudents() {  
  40.         return students;  
  41.     }  
  42.       
  43.       
  44. }  
 这样就会把application.properties中以class.开头的配置项给注入进来
我们看到代码中@ConfigurationProperties并没有指明properties,那默认的就是application.properties。那是否有地方指明properties的路径呢?在版本1.5.1之前可以@ConfigurationProperties(prefix="class",location="classpath:/customer.properties"),但是1.5.2版本没有location了,需要用@PropertySource("classpath:/student.properties")
[java]  view plain  copy
  1. package com.fei.springboot.config;  
  2.   
  3. import org.springframework.boot.context.properties.ConfigurationProperties;  
  4. import org.springframework.context.annotation.PropertySource;  
  5. import org.springframework.stereotype.Component;  
  6.   
  7. @Component("studentConfig")  
  8. @ConfigurationProperties  
  9. @PropertySource("classpath:/student.properties")  
  10. public class StudentConfig {  
  11.   
  12.     private String name;  
  13.       
  14.   
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.       
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.       
  23. }  

测试下
[java]  view plain  copy
  1. package com.fei.springboot.controller;  
  2.   
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5. import org.springframework.beans.factory.annotation.Autowired;  
  6. import org.springframework.beans.factory.annotation.Value;  
  7. import org.springframework.stereotype.Controller;  
  8. import org.springframework.web.bind.annotation.RequestMapping;  
  9. import org.springframework.web.bind.annotation.ResponseBody;  
  10.   
  11. import com.fei.springboot.config.ClassConfig;  
  12. import com.fei.springboot.config.StudentConfig;  
  13.   
  14. @Controller  
  15. @RequestMapping("/")  
  16. public class HelloController {  
  17.   
  18.     public static Logger LOG = LoggerFactory.getLogger(HelloController.class);  
  19.       
  20.     @Value("${hello}")  
  21.     private String hello;  
  22.       
  23.     @Value("${class.schoolName}")  
  24.     private String schoolName;  
  25.       
  26.     @Autowired  
  27.     private ClassConfig classConfig;  
  28.       
  29.     @Autowired  
  30.     private StudentConfig studentConfig;  
  31.       
  32.     @RequestMapping(value="/hello")  
  33.     @ResponseBody  
  34.     public String hello(){  
  35.         System.out.println("=======使用@Value注入获取.....===========");  
  36.         System.out.println("hello="+hello+"   schoolName=" + schoolName);  
  37.         System.out.println("======使用@ConfigurationProperties注入获取.....============");  
  38.         System.out.println("schoolName=" + classConfig.getSchoolName());  
  39.         System.out.println("className=" + classConfig.getClassName());  
  40.         System.out.println("student[0].name=" + classConfig.getStudents().get(0).getName());  
  41.           
  42.         System.out.println("studentConfig...name=" + studentConfig.getName());  
  43.           
  44.         return "hello";  
  45.     }  
  46. }  
打印结果
[plain]  view plain  copy
  1. 2017-05-17 15:20:49.677  INFO 72996 --- [p-nio-80-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms  
  2. 2017-05-17 15:20:49.677 [http-nio-80-exec-1] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms  
  3. =======使用@Value注入获取.....===========  
  4. hello=hello china!!!   schoolName=china school  
  5. ======使用@ConfigurationProperties注入获取.....============  
  6. schoolName=china school  
  7. className=second grade three class  
  8. student[0].name=tom  
  9. studentConfig...name=xiao hong  


 源码下载



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值