1. 描述
一个spring boot 读取配置文件信息的 demo示例。
环境:
IDE(idea):2021.3
JDK:1.8
maven:3.8.4
spring boot:2.5.6
2. 结果
- 接口返回 读取的配置文件信息
3. demo
3.0 项目结构
3.1 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.byrc</groupId>
<artifactId>byrc-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>demo-properties</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>${project.artifactId}</name>
<description>Demo project for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.2 SpringBootApplication 注解实现启动类
@SpringBootApplication
public class DemoPropertiesApplication {
public static void main(String[] args) {
SpringApplication.run(DemoPropertiesApplication.class, args);
System.out.println("----- http://localhost:8880/demo/ -----");
}
}
3.3 application.yml配置
- 端口和应用路径
- 应用信息和开发信者息(演示读取配置使用)
application.yml
server:
port: 8880
servlet:
context-path: /demo
spring:
profiles:
active: dev
application:
name: ${project.artifactId}
application-dev.yml
application:
name: dev环境 @artifactId@
version: dev环境 @version@
developer:
name: dev环境 byrc
website: dev环境 http://byrced.com
qq: dev环境 35174
phone-number: dev环境 13517466666
3.4 其他代码
@Getter
@Component
public class ApplicationProperty {
@Value(value = "${application.name}")
private String name;
@Value(value = "${application.version}")
private String version;
}
@Data
@ConfigurationProperties(prefix = "developer")
@Component
public class DeveloperProperty {
private String name;
private String website;
private String qq;
private String phoneNumber;
}
@RestController
@RequestMapping("/property")
public class PropertyController {
private final ApplicationProperty applicationProperty;
private final DeveloperProperty developerProperty;
@Autowired
public PropertyController(ApplicationProperty applicationProperty, DeveloperProperty developerProperty) {
this.applicationProperty = applicationProperty;
this.developerProperty = developerProperty;
}
@GetMapping("/property")
public Dict getProperty() {
return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty);
}
}
4. 资料
官网:https://spring.io/projects/spring-boot/
5. 注
- spirng boot 已在父项目引入(父项目pom.xml配置);
- 部分jar包版本已在父项目管理(如果对应不上,一定、肯定、决定是某些修改,没同步更新文档)。
|