Spring Boot 配置
1. 前言
为了 Spring Boot 能够更好地生成配置元数据文件,我们可以在创建项目时添加 Spring Configuartion Processor
依赖,或者在创建好项目后的 pom.xml
文件中手动添加。添加该依赖后,我们在编写配置时就会有属性提示,大大降低编写错误。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
2. application.properties
2.1 自定义属性
application.properties
配置文件是创建项目后就自带的,如果我们要自定义属性,可以在其中直接配置,配置过程如下:
- 在
application.properties
中添加我们要自定义的配置;
cunyu.id=1024
cunyu.name=村雨遥
cunyu.website=https://cunyu1943.github.io
- 创建实体类来映射我们配置的属性;
package com.cunyu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author : cunyu
* @version : 1.0
* @className : CunyuProperties
* @date : 2020/7/29 13:34
* @description : TODO
*/
@Component
@ConfigurationProperties(prefix = "cunyu")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CunyuProperties {
private int id;
private String name;
private String website;
}
- 定义 Controller 来注入测试;
package com.cunyu.controller;
import com.cunyu.pojo.CunyuProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author : cunyu
* @version : 1.0
* @className : CunyuPropertiesController
* @date : 2020/7/29 13:37
* @description : TODO
*/
@RestController
@RequestMapping("/cunyu")
public class CunyuPropertiesController {
private static final Logger logger = LoggerFactory.getLogger(CunyuPropertiesController.class);
@Autowired
CunyuProperties cunyuProperties;
@GetMapping("/profile")
public String cunyuProfile(){
logger.info("---------------");
logger.info(cunyuProperties.toString());
logger.info("---------------");
return cunyuProperties.toString();
}
}
- 打开网页测试,打开 1,同时观察控制台,显示如下内容则说明属性注入成功;
2.2 多环境配置
实际开发过程中,常常需要多个环境(如 开发、测试、生产等),而不同环境的配置都不一样,此时配置方法如下;
- 创建不同环境对应的配置文件,配置文件名为
application-{profile}.properties
,{profile}
为我们自定义环境,如下:
application-dev.properties
server.servlet.context-path=/dev
application-test.properties
server.servlet.context-path=/test
application-prod.properties
server.servlet.context-path=/prod
- 然后在
application.properties
中加入激活的环境,此时就会激活对应环境的配置;
# {profile} 对应上述的 dev、test、prod
spring.profiles.active={profile}
3. 自定义配置文件
加入我们不想用项目自带的 application.properties
配置环境,那又该如何配置呢?
- 首先创建一个自定义配置文件
my.properties
,文件名可以自定义,但是后缀要保持一致,然后在其中加入我们自定义配置的属性;
my.id=1024
my.name=村雨遥
my.website=https://cunyu1943.github.io
- 定义实体类,用于映射自定义配置文件中的内容;
package com.cunyu.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* @author : cunyu
* @version : 1.0
* @className : MyProperties
* @date : 2020/7/29 14:05
* @description : TODO
*/
@Component
@PropertySource("classpath:my.properties")
@ConfigurationProperties(prefix = "my")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MyProperties {
private int id;
private String name;
private String website;
}
- 定义 Controller 来注入测试
package com.cunyu.controller;
import com.cunyu.pojo.MyProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author : cunyu
* @version : 1.0
* @className : MyPropertiesController
* @date : 2020/7/29 14:07
* @description : TODO
*/
@RestController
@RequestMapping("/my")
public class MyPropertiesController {
private static final Logger logger = LoggerFactory.getLogger(MyPropertiesController.class);
@Autowired
MyProperties myProperties;
@GetMapping("/profile")
public String myProfile() {
logger.info("=============");
logger.info(myProperties.toString());
logger.info("=============");
return myProperties.toString();
}
}
- 打开网页测试,打开
http://localhost:8080/my/profile
,同时观察控制台,显示如下内容则说明属性注入成功;
4. 注意
application.properties
和my.applicaiton.properties
会优先加载application.properties
。- 本文所有代码均已上传 Github[1],若有需要请自取。
参考资料
[1]
Github: https://github.com/cunyu1943/java-learning-code/tree/master/springboot-learning/spring-boot-01
- END -
更多教程请访问码农之家
往期回顾
通过注解的方式整合 MyBatis + Spring Boot