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

来源:互联网 发布:知豆电动汽车四座 编辑:程序博客网 时间:2024/06/06 04:56

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

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

pom.xml

 <parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>1.5.2.RELEASE</version></parent>   <dependencies>    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-web</artifactId>    </dependency></dependencies>


@Value

   使用@Value注入写在application.properties中的配置项
application.properties
logging.config=classpath:logback.xmllogging.path=d:/logs##tomcat set###server.port=80#session 超时时间server.session-timeout=60###########hello=hello china!!!class.schoolName=china schoolclass.className=second grade three classclass.students[0].name=tomclass.students[1].name=jack

在代码中@Value注入到属性中
@Controller@RequestMapping("/")public class HelloController {public static Logger LOG = LoggerFactory.getLogger(HelloController.class);@Value("${hello}")private String hello;@Value("${class.schoolName}")private String schoolName;

@ConfigurationProperties

 如果想把某种相关的配置,注入到某个配置类中,比如上面的application.properties中看到class.xx的配置项,想注入到ClassConfig配置类中
package com.fei.springboot.config;import java.util.ArrayList;import java.util.List;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;@Component("classConfig")@ConfigurationProperties(prefix="class")public class ClassConfig {private String schoolName;private String className;private List<StudentConfig> students = new ArrayList<StudentConfig>();public String getSchoolName() {return schoolName;}public void setSchoolName(String schoolName) {this.schoolName = schoolName;}public void setClassName(String className) {this.className = className;}public void setStudents(List<StudentConfig> students) {this.students = students;}public String getClassName() {return className;}public List<StudentConfig> getStudents() {return students;}}
 这样就会把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")
package com.fei.springboot.config;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;@Component("studentConfig")@ConfigurationProperties@PropertySource("classpath:/student.properties")public class StudentConfig {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}

测试下
package com.fei.springboot.controller;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.fei.springboot.config.ClassConfig;import com.fei.springboot.config.StudentConfig;@Controller@RequestMapping("/")public class HelloController {public static Logger LOG = LoggerFactory.getLogger(HelloController.class);@Value("${hello}")private String hello;@Value("${class.schoolName}")private String schoolName;@Autowiredprivate ClassConfig classConfig;@Autowiredprivate StudentConfig studentConfig;@RequestMapping(value="/hello")@ResponseBodypublic String hello(){System.out.println("=======使用@Value注入获取.....===========");System.out.println("hello="+hello+"   schoolName=" + schoolName);System.out.println("======使用@ConfigurationProperties注入获取.....============");System.out.println("schoolName=" + classConfig.getSchoolName());System.out.println("className=" + classConfig.getClassName());System.out.println("student[0].name=" + classConfig.getStudents().get(0).getName());System.out.println("studentConfig...name=" + studentConfig.getName());return "hello";}}
打印结果
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 ms2017-05-17 15:20:49.677 [http-nio-80-exec-1] INFO  o.s.web.servlet.DispatcherServlet - FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms=======使用@Value注入获取.....===========hello=hello china!!!   schoolName=china school======使用@ConfigurationProperties注入获取.....============schoolName=china schoolclassName=second grade three classstudent[0].name=tomstudentConfig...name=xiao hong


 源码下载



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝呛奶怎么办伴呼噜 离婚了欠的债务怎么办 小鸟生长发育过程壳破了怎么办 经常嗓子发炎抵抗力低下怎么办 生完孩子出虚汗怎么办 3岁宝宝抵抗力差怎么办 5岁宝宝免疫力低怎么办 狗狗后腿o型怎么办 狗狗前腿内八字怎么办 小猫腿摔瘸了怎么办会好吗 小狗的腿断了怎么办 狗后腿摔瘸了怎么办 狗狗脖子挠破了怎么办 狗狗脖子挠破化脓怎么办 狗狗大腿骨折了怎么办啊 狗狗洗澡后拉稀怎么办 狗狗好像生病了怎么办 奶狗前腿断了怎么办 狗摔了一下瘸了怎么办 小狗腿突然瘸了怎么办 狗腿突然瘸了怎么办 狗腿受伤了肿了怎么办 狗狗缺钙腿变形怎么办 小狗腿摔骨折了怎么办 狗狗脚掌被压了怎么办 狗狗的脚骨折了怎么办 给猫灌药水呛到怎么办 吃佐匹克隆白天工作量降低怎么办? 手机网页不显示图片怎么办啊 页眉页脚同前节怎么办 小米8后盖缝隙大怎么办 狗子生了一个不动的小狗怎么办 狗狗肚子有脓包怎么办 小孩幼儿园数学不开窍怎么办 老百姓打仗了报警派出所不管怎么办 和人打架报案了怎么办 皇上死后的妃子怎么办 武警改制警卫系的学员怎么办 正团病故后住房怎么办 遇到保姆式领导该怎么办 限购房子卖不了怎么办