Spring Boot项目中读取配置文件的内容
2018-04-02 17:09
507 查看
在spring boot学习1 时,知道spring boot会默认读取配置application.properties。那如果我们直接在application.properties添加自定义的配置项时,如何读取?或者不想把所有的配置都放在application.properties中,而是自定义一个properties文件时,又该如何读取呢?难道还需自己写代码加载读取配置文件吗?
注:下面代码是在spring boot 1.5.2版本下写的。
pom.xml
[html] view plain copy print?<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>
</
4000
dependencies>
application.properties
[plain] view plain copy print?logging.config=classpath:logback.xml logging.path=d:/logs##tomcat set###
server.port=80
#session 超时时间
server.session-timeout=60
###########
hello=hello china!!!
class.schoolName=china school
class.className=second grade three class
class.students[0].name=tom
class.students[1].name=jack
#
hello=hello china!!!
class.schoolName=china school
class.className=second grade three class
class.students[0].name=tom
class.students[1].name=jack
在代码中@Value注入到属性中
[java] view plain copy print?@Controller
@RequestMapping(“/”)
public class HelloController {
public static Logger LOG = LoggerFactory.getLogger(HelloController.class);
@Value(“{hello}"</span><span>) </span></span></li><li class=""><span> <span class="keyword">private</span><span> String hello; </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="annotation">@Value</span><span>(</span><span class="string">"{hello}"</span><span>) </span></span></li><li class=""><span> <span class="keyword">private</span><span> String hello; </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="annotation">@Value</span><span>(</span><span class="string">"{class.schoolName}”)
private String schoolName;
[java] view plain copy print?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;
}
}
我们看到代码中@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 print?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;
}
}
测试下
[java] view plain copy print?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}"</span><span>) </span></span></li><li class="alt"><span> <span class="keyword">private</span><span> String hello; </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="annotation">@Value</span><span>(</span><span class="string">"{hello}"</span><span>) </span></span></li><li class="alt"><span> <span class="keyword">private</span><span> String hello; </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="annotation">@Value</span><span>(</span><span class="string">"{class.schoolName}”)
private String schoolName;
@Autowired
private ClassConfig classConfig;
@Autowired
private StudentConfig studentConfig;
@RequestMapping(value=“/hello”)
@ResponseBody
public 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”;
}
}
[plain] view plain copy print?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
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
=======使用@Value注入获取…..===========
hello=hello china!!! schoolName=china school
======使用@ConfigurationProperties注入获取…..============
schoolName=china school
className=second grade three class
student[0].name=tom
studentConfig…name=xiao hong
源码下载
注:下面代码是在spring boot 1.5.2版本下写的。
pom.xml
[html] view plain copy print?<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>
</
4000
dependencies>
<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
[plain] view plain copy print?logging.config=classpath:logback.xml logging.path=d:/logs##tomcat set###
server.port=80
#session 超时时间
server.session-timeout=60
###########
hello=hello china!!!
class.schoolName=china school
class.className=second grade three class
class.students[0].name=tom
class.students[1].name=jack
logging.config=classpath:logback.xml logging.path=d:/logs
tomcat set
server.port=80session 超时时间
server.session-timeout=60#
hello=hello china!!!
class.schoolName=china school
class.className=second grade three class
class.students[0].name=tom
class.students[1].name=jack
在代码中@Value注入到属性中
[java] view plain copy print?@Controller
@RequestMapping(“/”)
public class HelloController {
public static Logger LOG = LoggerFactory.getLogger(HelloController.class);
@Value(“{hello}"</span><span>) </span></span></li><li class=""><span> <span class="keyword">private</span><span> String hello; </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="annotation">@Value</span><span>(</span><span class="string">"{hello}"</span><span>) </span></span></li><li class=""><span> <span class="keyword">private</span><span> String hello; </span></span></li><li class="alt"><span> </span></li><li class=""><span> <span class="annotation">@Value</span><span>(</span><span class="string">"{class.schoolName}”)
private String schoolName;
@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配置类中[java] view plain copy print?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;
}
}
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”)
[java] view plain copy print?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.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; } }
测试下
[java] view plain copy print?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}"</span><span>) </span></span></li><li class="alt"><span> <span class="keyword">private</span><span> String hello; </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="annotation">@Value</span><span>(</span><span class="string">"{hello}"</span><span>) </span></span></li><li class="alt"><span> <span class="keyword">private</span><span> String hello; </span></span></li><li class=""><span> </span></li><li class="alt"><span> <span class="annotation">@Value</span><span>(</span><span class="string">"{class.schoolName}”)
private String schoolName;
@Autowired
private ClassConfig classConfig;
@Autowired
private StudentConfig studentConfig;
@RequestMapping(value=“/hello”)
@ResponseBody
public 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”;
}
}
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;
@Autowired
private ClassConfig classConfig;
@Autowired
private StudentConfig studentConfig;
@RequestMapping(value="/hello")
@ResponseBody
public 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";
}
}
[plain] view plain copy print?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
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
=======使用@Value注入获取…..===========
hello=hello china!!! schoolName=china school
======使用@ConfigurationProperties注入获取…..============
schoolName=china school
className=second grade three class
student[0].name=tom
studentConfig…name=xiao hong
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 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 =======使用@Value注入获取.....=========== hello=hello china!!! schoolName=china school ======使用@ConfigurationProperties注入获取.....============ schoolName=china school className=second grade three class student[0].name=tom studentConfig...name=xiao hong
源码下载
相关文章推荐
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- 解决spring-boot项目中无法读取yml配置文件属性问题
- Spring boot框架项目,使用maven命令将配置文件打包到jar包外,项目运行读取jar外配置文件
- Springboot 之 自定义配置文件及读取配置文件
- spring-boot读取props和yml配置文件
- spring-boot读取props和yml配置文件
- Spring Boot 读取properties配置文件
- spring boot项目配置文件集合
- springboot如何读取配置文件(application.yml)中的属性值
- eclipse 使用maven 构建springboot 读取多个配置文件
- spring项目打JAR包读取配置文件
- Spring boot 配置文件读取
- Spring-boot中读取config配置文件的两种方式
- spring boot 在不同环境下读取不同配置文件的一种方式
- Maven配置Spring Boot项目打包,依赖包到lib目录、配置文件到conf目录
- 基于maven管理的项目中spring+mybatis配置数据源读取不到properties属性文件的问题
- spring boot 单元测试与读取配置文件(application.yml)中的属性值
- Spring依赖注入的XML配置文件的实现思想(1)——准备工作:如何读取XML的文件的内容
- spring-boot读取props和yml配置文件
- Spring-boot中读取config配置文件的两种方式