@@ -35,6 +35,7 @@ spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuato | |||||
<modules> | <modules> | ||||
<module>../spring-boot-demo-helloworld</module> | <module>../spring-boot-demo-helloworld</module> | ||||
<module>../spring-boot-demo-properties</module> | |||||
<module>../spring-boot-demo-actuator</module> | <module>../spring-boot-demo-actuator</module> | ||||
<module>../spring-boot-demo-logback</module> | <module>../spring-boot-demo-logback</module> | ||||
<module>../spring-boot-demo-mybatis</module> | <module>../spring-boot-demo-mybatis</module> | ||||
@@ -108,6 +109,7 @@ spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuato | |||||
| Module 名称 | Module 介绍 | | | Module 名称 | Module 介绍 | | ||||
| ---------------------------------------- | ---------------------------------------- | | | ---------------------------------------- | ---------------------------------------- | | ||||
| [spring-boot-demo-helloworld](./spring-boot-demo-helloworld) | spring-boot 的一个 helloworld | | | [spring-boot-demo-helloworld](./spring-boot-demo-helloworld) | spring-boot 的一个 helloworld | | ||||
| [spring-boot-demo-properties](./spring-boot-demo-properties) | spring-boot 读取配置文件中的内容 | | |||||
| [spring-boot-demo-actuator](./spring-boot-demo-actuator) | spring-boot 集成 spring-boot-starter-actuator 用于监控 spring-boot 的启动和运行状态 | | | [spring-boot-demo-actuator](./spring-boot-demo-actuator) | spring-boot 集成 spring-boot-starter-actuator 用于监控 spring-boot 的启动和运行状态 | | ||||
| [spring-boot-demo-logback](./spring-boot-demo-logback) | spring-boot 集成 logback 日志 | | | [spring-boot-demo-logback](./spring-boot-demo-logback) | spring-boot 集成 logback 日志 | | ||||
| [spring-boot-demo-mybatis](./spring-boot-demo-mybatis) | spring-boot 集成 [mybatis-spring-boot-starter](https://github.com/mybatis/spring-boot-starter)、[mybatis-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) | | | [spring-boot-demo-mybatis](./spring-boot-demo-mybatis) | spring-boot 集成 [mybatis-spring-boot-starter](https://github.com/mybatis/spring-boot-starter)、[mybatis-spring-boot-starter](https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter) | | ||||
@@ -13,6 +13,7 @@ | |||||
<modules> | <modules> | ||||
<module>../spring-boot-demo-helloworld</module> | <module>../spring-boot-demo-helloworld</module> | ||||
<module>../spring-boot-demo-properties</module> | |||||
<module>../spring-boot-demo-actuator</module> | <module>../spring-boot-demo-actuator</module> | ||||
<module>../spring-boot-demo-logback</module> | <module>../spring-boot-demo-logback</module> | ||||
<module>../spring-boot-demo-mybatis</module> | <module>../spring-boot-demo-mybatis</module> | ||||
@@ -0,0 +1,48 @@ | |||||
# spring-boot-demo-properties | |||||
依赖 [spring-boot-demo-helloworld](../spring-boot-demo-parent) | |||||
### application.yml | |||||
```yml | |||||
server: | |||||
port: 8080 | |||||
context-path: /demo | |||||
application: | |||||
name: spring-boot-demo-properties | |||||
version: 0.0.1-SNAPSHOT | |||||
author: | |||||
name: xkcoding | |||||
website: xkcoding.com | |||||
qq: 237497819 | |||||
phone-number: 18601224166 | |||||
``` | |||||
### 读取配置文件的两种方式 | |||||
#### ApplicationConfig.java(第一种方式) | |||||
```java | |||||
@Component | |||||
@Data | |||||
public class ApplicationConfig { | |||||
@Value("${application.name}") | |||||
private String name; | |||||
@Value("${application.version}") | |||||
private String version; | |||||
} | |||||
``` | |||||
#### AuthorConfig.java(第二种方式) | |||||
```java | |||||
@Data | |||||
@ConfigurationProperties(prefix = "application.author") | |||||
@Component | |||||
public class AuthorConfig { | |||||
private String name; | |||||
private String website; | |||||
private String qq; | |||||
private String phoneNumber; | |||||
} | |||||
``` |
@@ -0,0 +1,24 @@ | |||||
<?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> | |||||
<artifactId>spring-boot-demo-properties</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<packaging>war</packaging> | |||||
<name>spring-boot-demo-properties</name> | |||||
<description>Demo project for Spring Boot</description> | |||||
<parent> | |||||
<groupId>com.xkcoding</groupId> | |||||
<artifactId>spring-boot-demo-parent</artifactId> | |||||
<version>0.0.1-SNAPSHOT</version> | |||||
<relativePath>../spring-boot-demo-parent/pom.xml</relativePath> | |||||
</parent> | |||||
<build> | |||||
<finalName>spring-boot-demo-properties</finalName> | |||||
</build> | |||||
</project> |
@@ -0,0 +1,12 @@ | |||||
package com.xkcoding.springbootdemoproperties; | |||||
import org.springframework.boot.SpringApplication; | |||||
import org.springframework.boot.autoconfigure.SpringBootApplication; | |||||
@SpringBootApplication | |||||
public class SpringBootDemoPropertiesApplication { | |||||
public static void main(String[] args) { | |||||
SpringApplication.run(SpringBootDemoPropertiesApplication.class, args); | |||||
} | |||||
} |
@@ -0,0 +1,14 @@ | |||||
package com.xkcoding.springbootdemoproperties.config; | |||||
import lombok.Data; | |||||
import org.springframework.beans.factory.annotation.Value; | |||||
import org.springframework.stereotype.Component; | |||||
@Component | |||||
@Data | |||||
public class ApplicationConfig { | |||||
@Value("${application.name}") | |||||
private String name; | |||||
@Value("${application.version}") | |||||
private String version; | |||||
} |
@@ -0,0 +1,15 @@ | |||||
package com.xkcoding.springbootdemoproperties.config; | |||||
import lombok.Data; | |||||
import org.springframework.boot.context.properties.ConfigurationProperties; | |||||
import org.springframework.stereotype.Component; | |||||
@Data | |||||
@ConfigurationProperties(prefix = "application.author") | |||||
@Component | |||||
public class AuthorConfig { | |||||
private String name; | |||||
private String website; | |||||
private String qq; | |||||
private String phoneNumber; | |||||
} |
@@ -0,0 +1,29 @@ | |||||
package com.xkcoding.springbootdemoproperties.controller; | |||||
import com.google.common.collect.Maps; | |||||
import com.xkcoding.springbootdemoproperties.config.ApplicationConfig; | |||||
import com.xkcoding.springbootdemoproperties.config.AuthorConfig; | |||||
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; | |||||
import java.util.HashMap; | |||||
import java.util.Map; | |||||
@RestController | |||||
@RequestMapping("/config") | |||||
public class ConfigController { | |||||
@Autowired | |||||
private ApplicationConfig applicationConfig; | |||||
@Autowired | |||||
private AuthorConfig authorConfig; | |||||
@GetMapping({"", "/", "/index"}) | |||||
public Map index() { | |||||
HashMap<Object, Object> ret = Maps.newHashMap(); | |||||
ret.put("applicationConfig", applicationConfig); | |||||
ret.put("authorConfig", authorConfig); | |||||
return ret; | |||||
} | |||||
} |
@@ -0,0 +1,11 @@ | |||||
server: | |||||
port: 8080 | |||||
context-path: /demo | |||||
application: | |||||
name: spring-boot-demo-properties | |||||
version: 0.0.1-SNAPSHOT | |||||
author: | |||||
name: xkcoding | |||||
website: xkcoding.com | |||||
qq: 237497819 | |||||
phone-number: 18601224166 |
@@ -0,0 +1,16 @@ | |||||
package com.xkcoding.springbootdemoproperties; | |||||
import org.junit.Test; | |||||
import org.junit.runner.RunWith; | |||||
import org.springframework.boot.test.context.SpringBootTest; | |||||
import org.springframework.test.context.junit4.SpringRunner; | |||||
@RunWith(SpringRunner.class) | |||||
@SpringBootTest | |||||
public class SpringBootDemoPropertiesApplicationTests { | |||||
@Test | |||||
public void contextLoads() { | |||||
} | |||||
} |