Spring boot 读取配置文件properties
By:Roy.LiuLast updated:2017-05-16
Spring boot 读取配置文件, 有如下几种方法
1. 直接利用Spring @Value 注解
2. 利用@PropertySource("classpath:xxx.properties") 与 @Value 注解配合
3. @PropertySource("classpath:xxx.properties") 与 @ConfigurationProperties 注解配合
采用这三种常见的方法,甚至可以直接用配置文件组装复杂的对象都可以
一,直接利用Spring @Value 注解
直接在使用的类中使用,比如:
二. 利用@PropertySource("classpath:xxx.properties") 与 @Value 注解配合
三. @PropertySource("classpath:xxx.properties") 与 @ConfigurationProperties 注解配合
这样做了之后,在其他类中怎么调用呢,其实直接注入就可以了。 记住我们用 @Component 来注解了这个properties 类,所以可以在其他类中用 @Autowired 注入:
如何mapping 一个复杂的对象呢,比如有如下一个配置文件:
注解得到对应的类如下:
spring boot properties 源码下载:
spring boot properties sample download
1. 直接利用Spring @Value 注解
2. 利用@PropertySource("classpath:xxx.properties") 与 @Value 注解配合
3. @PropertySource("classpath:xxx.properties") 与 @ConfigurationProperties 注解配合
采用这三种常见的方法,甚至可以直接用配置文件组装复杂的对象都可以
一,直接利用Spring @Value 注解
直接在使用的类中使用,比如:
@Service
public class MyCommandService {
@Value("${name:unknown}")
private String name;
public String getMessage() {
return getMessage(name);
}
public String getMessage(String name) {
return "Hello " + name;
}
}
二. 利用@PropertySource("classpath:xxx.properties") 与 @Value 注解配合
package com.yihaomen.springboot;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:application.properties")
public class GlobalProperties {
@Value("${name}")
private String name;
@Value("${address}")
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
三. @PropertySource("classpath:xxx.properties") 与 @ConfigurationProperties 注解配合
package com.yihaomen.springboot;
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("classpath:application.properties")
@ConfigurationProperties
public class GlobalProperties {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
这样做了之后,在其他类中怎么调用呢,其实直接注入就可以了。 记住我们用 @Component 来注解了这个properties 类,所以可以在其他类中用 @Autowired 注入:
package com.yihaomen.springboot.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.yihaomen.springboot.GlobalProperties;
@Service
public class MyCommandService {
@Value("${name:unknown}")
private String name;
@Autowired
private GlobalProperties prop;
public String getMessage() {
return getMessage(name);
}
public String getMessage(String name) {
return "Hello " + name + " || address: " + prop.getAddress();
}
}
如何mapping 一个复杂的对象呢,比如有如下一个配置文件:
name=yihaomen-aaa address=wuhan #App app.menus[0].title=Home app.menus[0].name=Home app.menus[0].path=/ app.menus[1].title=Login app.menus[1].name=Login app.menus[1].path=/login app.compiler.timeout=5 app.compiler.output-folder=/temp/ app.error=/error/
注解得到对应的类如下:
@Component
@ConfigurationProperties("app") // 前缀找 app.* , 没有前缀,找根
public class AppProperties {
private String error;
private Listspring boot properties 源码下载:
spring boot properties sample download
From:一号门
Previous:Django 1.10 以上版本 url 配置注意事项

COMMENTS