在实际开发当中我们对于项目配置的获取是必不可少的,举例子来学习SpringBoot是如何快速获取配置文件中的数据与切换读取不同的配置文件内容
先创建一个springBoot基本配置文件 (application.properties)
1.修改我们启动的端口号
2.访问连接在加上一层内容
(
以前的访问路径:http://localhost:8080/hello
现在的访问路径:http://localhost:8081/springBootTest/hello
)
server.port=8081
server.context-path=/springBootTest
修改好后启动项目查看我们修改了配置文件的内容是否生效,访问成功说明我们修改的配置文件生效
我们使用到下面这几种注解让我们完成获取配置文件数据
添加两个数据后的(application.properties)文件
server.port=8081
server.context-path=/springBootTest
cupSize=B
age=18
随后在 HelloController类中通过 @Value() 这个注解 获取到配置文件中的数据并输出
package com.zhang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 第一个springboot 应用
* @author Administrator
*
*/
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
/* @Value("${context}")
private String context;*/
//@Autowired
//private GirlProper girlProper;//把配置文件映射到实体对象中
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say(){
return "cupSize:"+cupSize+" age:"+age;
//return "Hello Spring Boot!";
}
}
修改好之后重新启动项目访问看看我们输出的数据,输出成功说明正确的访问到了配置文件中的数据
接下来在试试当在配置文件中获取其他配置的数据做完自己数据后还能否获取到数据
添加数据后的(application.properties)文件
server.port=8081
server.context-path=/springBootTest
cupSize=B
age=18
context="age:${age} cupSize: ${cupSize}"
随后在 HelloController类中通过 @Value() 这个注解 获取到配置文件中的数据并输出
package com.zhang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 第一个springboot 应用
* @author Administrator
*
*/
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@Value("${context}")
private String context;
//@Autowired
//private GirlProper girlProper;//把配置文件映射到实体对象中
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say(){
//return "Hello Spring Boot!";
//return "cupSize:"+cupSize+" age:"+age;
return context;
}
}
修改好之后重新启动项目访问看看我们输出的数据,输出成功说明正确的访问到了配置文件中的数据
一个个数据的获取还是很麻烦的,接下来尝试一下用获取配置文件当中对象这样更符合我们实际开发使用
添加数据后的(application.properties)文件
server.port=8081
server.context-path=/springBootTest
cupSize=B
age=18
context="age:${age} cupSize: ${cupSize}"
girl.cupSize=B
girl.age=18
创建一个实体类用户装载配置文件当中的属性数据 GirlProper类
package com.zhang;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 女孩实体类
* @author Administrator
*
*/
@Component
@ConfigurationProperties(prefix = "girl")//把配置文件映射到实体对象中
public class GirlProper {
private String cupSize;
private Integer age;
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
HelloController 类中我们注入 映射好的 GirlProper类 获取配置文件中的属性数据
package com.zhang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 第一个springboot 应用
* @author Administrator
*
*/
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@Value("${context}")
private String context;
@Autowired
private GirlProper girlProper;//把配置文件映射到实体对象中
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say(){
//return "Hello Spring Boot!";
//return "cupSize:"+cupSize+" age:"+age;
//return context;
return girlProper.getCupSize()+"------"+girlProper.getAge();
}
}
修改好之后重新启动项目访问看看我们输出的数据,输出成功说明正确的访问到了配置文件中的数据
现实开发当中,我们有开发环境与生产环境所以用的配置文件也是有所却别,通过下面的例子来实现springBoot如何简单切换我们要使用的配置文件,这个就大大简化了我们的配置,不在配置上花费太多的时间,这个也是springBoot的一个优势
先创建两个配置文件
application.properties springBoot配置文件
application-dev.properties 开发环境配置文件
application-prod.properties 生产环境配置文件
先测试配置开发环境配置文件 (要应用那个配置文件,只需要配置上文件名称就可以了)
application.properties springBoot配置文件
spring.profiles.active=dev
application-dev.properties 开发环境配置文件
server.port=8080
cupSize=B
age=18
context="age:${age} cupSize: ${cupSize}"
girl.cupSize=B
girl.age=18
HelloController 类
package com.zhang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 第一个springboot 应用
* @author Administrator
*
*/
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@Value("${context}")
private String context;
@Autowired
private GirlProper girlProper;//把配置文件映射到实体对象中
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say(){
//return "Hello Spring Boot!";
//return "cupSize:"+cupSize+" age:"+age;
//return context;
return girlProper.getCupSize()+"------"+girlProper.getAge();
}
}
(开发环境测试结果)
修改好之后重新启动项目访问看看我们输出的数据,输出成功说明正确的访问到了配置文件中的数据
测试配置生产环境配置文件 (要应用那个配置文件,只需要配置上文件名称就可以了)
application.properties springBoot配置文件
spring.profiles.active=prod
application-prod.properties 开发环境配置文件
server.port=8081
cupSize=F
age=20
context="age:${age} cupSize: ${cupSize}"
girl.cupSize=F
girl.age=20
HelloController 类
package com.zhang;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* 第一个springboot 应用
* @author Administrator
*
*/
@RestController
public class HelloController {
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@Value("${context}")
private String context;
@Autowired
private GirlProper girlProper;//把配置文件映射到实体对象中
@RequestMapping(value = "/hello", method = RequestMethod.GET)
public String say(){
//return "Hello Spring Boot!";
//return "cupSize:"+cupSize+" age:"+age;
//return context;
return girlProper.getCupSize()+"------"+girlProper.getAge();
}
}
(生产环境测试结果)
修改好之后重新启动项目访问看看我们输出的数据,输出成功说明正确的访问到了配置文件中的数据