Spring Boot 01 加载配置文件和获取key/value值
2017-11-01 21:04
1291 查看
项目目录:
JDBCConfig.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:config/jdbc.properties")
public class JDBCConfig {
@Value("${username}")
private String username;
@Value("${url}")
private String url;
@Value("${password}")
private String password;
@Value("${driverclassname}")
private String driverClassName;
@Override
public String toString() {
return "JDBCConfig [username=" + username + ", url=" + url
+ ", password=" + password + ", driverClassName="
+ driverClassName + "]";
}
}
jdbc.porperties
username=root password=123465 url=jdbc:mysql://127.0.0.1:3306/test_mysql driverclassname=com.mysql.jdbs.DriverDSConfig.java
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix="ds")
@PropertySource("classpath:ds.properties")
public class DsConfig {
@Override
public String toString() {
return "DsConfig [username=" + username + ", password=" + password
+ ", url=" + url + ", driverClassName=" + driverClassName + "]";
}
private String username;
private String password;
private String url;
private String driverClassName;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
}
ds.properties
ds.username=root ds.password=123456 ds.url=jdbc:mysql://127.0.0.1:3306/test_mysql ds.driverClassName=com.mysql.jdbc.Driver
User.java
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component @PropertySource("file:E:/SpringBoot/application1.properties") public class User { @Value("${local.port}") private String port; @Autowired private Environment env; @Override public String toString() { return "User [port=" + port + ", env=" + env.getProperty("local.ip") + "]"; } }文件路径:
application1.properties
local.ip=192.168.1.31 local.port=8080
1、Spring Boot 默认配置文件的位置和名字
(1)、默认放置配置文件的地方是:classpath: 和classpath:config
(2)、默认名字是:application.properties
2、加载配置文件的方式
(1)、使用@PropertiesSource或@PrepertiesSources注解JDBCConfig.java。(看JDBCConfig.java)
(2)、使用@ConfigurationProperties注解。(注:1.4版本的可以使用该注解的locations属性加载配置文件,1.5版本不支持该属性)
(3 )、使用main方法的args参数:--spring.config.location=classpath:/propertiesName.properties
或--spring.config.name=propertiesName(注:该参数可以省略后缀名,位置在classpath:或classpath:/config目录下
(4)、可以使用file:"路径"获取磁盘的配置文件(看User.java)
3、获取配置文件的key/value值
1、@Value("${key}")
2、@ConfigurationProperties注解(看DsConfig.java,这个注解是用set方法注入的)
3、获取Environment对象,使用getProperty方法
4、获取Enviroment对象
1.
ConfigurableApplicationContext context = SpringApplication.run(App.class, args); context.getEnviroment()2、跟普通的bean一样获取。
5、配置文件注入List或数组
使用@ConfigurationPerties注解。
ListConfig.java
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; @Component @PropertySource("classpath:list.properties") @ConfigurationProperties(prefix="esc") public class ListConfig { private List<String> host= new ArrayList<String>(); private String []ip=new String[100]; public String[] getIp() { return ip; } @Override public String toString() { return "ListConfig [host=" + host + ", ip=" + Arrays.toString(ip) + "]"; } public void setIp(String[] ip) { this.ip = ip; } public List<String> getHost() { return host; } public void setHost(List<String> host) { this.host = host; } }list.properties
esc.host[0]=1 esc.host[2]=2 esc.host[1]=5 esc.host[3]=3 esc.host[4]=4 esc.ip[0]=a esc.ip[1]=b esc.ip[2]=c esc.ip[3]=d
相关文章推荐
- [Spring boot]配置文件properties加载
- 监听器如何获取Spring配置文件(加载生成Spring容器)
- spring-boot 加载本地静态资源文件路径配置
- spring boot通过jar包启动时,配置文件的加载顺序
- spring-boot 加载本地静态资源文件路径配置
- spring-boot 加载本地静态资源文件路径配置
- Spring Boot学习--从配置文件中获取随机数
- Spring Boot加载配置文件
- spring boot加载第三方jar包的配置文件
- 监听器如何获取Spring配置文件(加载生成Spring容器)
- SpringBoot学习:获取yml和properties配置文件的内容
- spring-boot 加载本地静态资源文件路径配置
- 详解Spring Boot加载properties和yml配置文件
- springboot配置文件加载不到的问题
- spring-boot 加载本地静态资源文件路径配置
- spring-boot 加载本地静态资源文件路径配置
- springboot配置文件加载不到的问题
- spring-boot使用注解获取配置文件中的值
- springboot获取配置文件中的属性(包含自定义的文件)
- 使用SpringBoot加载配置文件