在main目录下新建resources目录,并设置为Root Resources,新建文件application.yaml
server:
ip: 10.1.0.6
hostname: hadoop-bd1
admin: zhangsan
yaml是替代传统的xx.properties文件,是一种方便的定义层次配置数据的格式
package com.mp.prj.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Server01 {
@Value("10.1.0.1")
private String ip;
@Value("hadoop-bd1")
private String hostname;
@Value("1")
private int admin;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public int getAdmin() {
return admin;
}
public void setAdmin(int admin) {
this.admin = admin;
}
@Override
public String toString() {
return "Server01{" +
"ip='" + ip + '\'' +
", hostname='" + hostname + '\'' +
", admin=" + admin +
'}';
}
}
使用@SpringBootTest 单元测试
package com.mp.test;
import com.mp.prj.AppTest;
import com.mp.prj.domain.Server01;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/*
* @SpringBootTest:用于springboot项目微服务单元测试,需要指定classes 启动类
@RunWith :需要指定
* */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppTest.class)
public class IocTest02 {
@Autowired
Server01 server01;
@Test
public void test02(){
System.out.println(server01);
}
}
运行结果:
Server01{ip='10.1.0.1', hostname='hadoop-bd1', admin=1}
package com.mp.prj.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Server02 {
private String ip;
private String hostname;
@Value("#{1+0}")
private int admin;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public int getAdmin() {
return admin;
}
public void setAdmin(int admin) {
this.admin = admin;
}
@Override
public String toString() {
return "Server01{" +
"ip='" + ip + '\'' +
", hostname='" + hostname + '\'' +
", admin=" + admin +
'}';
}
}
同样单元测试,也是可以捕获到信息
package com.mp.prj.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/*
* 这里使用@Configuration 注解,作用于类上,表明这个类为一个配置类,其实里面也包含有@Component注解
* */
@Configuration
public class Server03 {
@Value("${server.ip}")
private String ip;
@Value("${server.hostname}")
private String hostname;
@Value("${server.admin}")
private int admin;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public int getAdmin() {
return admin;
}
public void setAdmin(int admin) {
this.admin = admin;
}
@Override
public String toString() {
return "Server01{" +
"ip='" + ip + '\'' +
", hostname='" + hostname + '\'' +
", admin=" + admin +
'}';
}
}
测试运行结果:
Server01{ip='10.1.0.6', hostname='hadoop-bd1', admin=1}
(1)针对属性值很多的情况下,我们每一个属性都要去写@Value 配置,那岂不是很麻烦,所以我们这里可以指定配置前缀
(2)通过@ConfigurationProperties 我们取指定配置文件的前缀
(3)字段名默认和配置文件中处前缀后一致的情况下,是可以省略掉@Value注解的
package com.mp.prj.domain;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
/*
* 这里使用@Configuration 注解,作用于类上,表明这个类为一个配置类,其实里面也包含有@Component注解
* */
@Configuration
@ConfigurationProperties(prefix = "server") //指定配置文件的前缀
public class Server04 {
private String ip;
private String hostname;
private int admin;
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
public int getAdmin() {
return admin;
}
public void setAdmin(int admin) {
this.admin = admin;
}
@Override
public String toString() {
return "Server01{" +
"ip='" + ip + '\'' +
", hostname='" + hostname + '\'' +
", admin=" + admin +
'}';
}
}
同样,我们是可以得到结果的
(1)很多情况下,我们会将开发和生成环境等多种配置放在一个yaml配置文件中,要求支持多环境切换
(2)通过spring.profiles 来指定该文档应用于哪个环境
(3) 配置文件中使用— 分割配置文件
spring:
profiles:
active: pro
---
spring:
profiles: dev
server:
ip: 10.1.0.6
hostname: hadoop-bd1
admin: 1
---
spring:
profiles: pro
server:
ip: 192.168.1.128
hostname: hadoop-bd100
admin: 0
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
//启动dev配置文件
app.setAdditionalProfiles("dev"); // dev 或prod
app.run(args);
}
}