From d32ceb01a8e6766c6cb054dea27967ba6979f926 Mon Sep 17 00:00:00 2001 From: "yangkai.shen" <237497819@qq.com> Date: Tue, 14 Nov 2017 14:58:43 +0800 Subject: [PATCH] =?UTF-8?q?spring=20boot=20=E8=AF=BB=E5=8F=96=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E4=BF=A1=E6=81=AF=E7=9A=84=E4=B8=A4?= =?UTF-8?q?=E7=A7=8D=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + spring-boot-demo-parent/pom.xml | 1 + spring-boot-demo-properties/README.md | 48 +++++++++++++++++++ spring-boot-demo-properties/pom.xml | 24 ++++++++++ .../SpringBootDemoPropertiesApplication.java | 12 +++++ .../config/ApplicationConfig.java | 14 ++++++ .../config/AuthorConfig.java | 15 ++++++ .../controller/ConfigController.java | 29 +++++++++++ .../src/main/resources/application.yml | 11 +++++ ...ingBootDemoPropertiesApplicationTests.java | 16 +++++++ 10 files changed, 172 insertions(+) create mode 100644 spring-boot-demo-properties/README.md create mode 100644 spring-boot-demo-properties/pom.xml create mode 100644 spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/SpringBootDemoPropertiesApplication.java create mode 100644 spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/config/ApplicationConfig.java create mode 100644 spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/config/AuthorConfig.java create mode 100644 spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/controller/ConfigController.java create mode 100644 spring-boot-demo-properties/src/main/resources/application.yml create mode 100644 spring-boot-demo-properties/src/test/java/com/xkcoding/springbootdemoproperties/SpringBootDemoPropertiesApplicationTests.java diff --git a/README.md b/README.md index fd16365..e792674 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuato ../spring-boot-demo-helloworld + ../spring-boot-demo-properties ../spring-boot-demo-actuator ../spring-boot-demo-logback ../spring-boot-demo-mybatis @@ -108,6 +109,7 @@ spring boot demo 一个用来学习 spring boot 的项目,已经集成 actuato | Module 名称 | Module 介绍 | | ---------------------------------------- | ---------------------------------------- | | [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-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) | diff --git a/spring-boot-demo-parent/pom.xml b/spring-boot-demo-parent/pom.xml index c09a9c0..af21188 100644 --- a/spring-boot-demo-parent/pom.xml +++ b/spring-boot-demo-parent/pom.xml @@ -13,6 +13,7 @@ ../spring-boot-demo-helloworld + ../spring-boot-demo-properties ../spring-boot-demo-actuator ../spring-boot-demo-logback ../spring-boot-demo-mybatis diff --git a/spring-boot-demo-properties/README.md b/spring-boot-demo-properties/README.md new file mode 100644 index 0000000..b1937ce --- /dev/null +++ b/spring-boot-demo-properties/README.md @@ -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; +} +``` \ No newline at end of file diff --git a/spring-boot-demo-properties/pom.xml b/spring-boot-demo-properties/pom.xml new file mode 100644 index 0000000..f071103 --- /dev/null +++ b/spring-boot-demo-properties/pom.xml @@ -0,0 +1,24 @@ + + + 4.0.0 + + spring-boot-demo-properties + 0.0.1-SNAPSHOT + war + + spring-boot-demo-properties + Demo project for Spring Boot + + + com.xkcoding + spring-boot-demo-parent + 0.0.1-SNAPSHOT + ../spring-boot-demo-parent/pom.xml + + + + spring-boot-demo-properties + + + diff --git a/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/SpringBootDemoPropertiesApplication.java b/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/SpringBootDemoPropertiesApplication.java new file mode 100644 index 0000000..5dc8077 --- /dev/null +++ b/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/SpringBootDemoPropertiesApplication.java @@ -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); + } +} diff --git a/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/config/ApplicationConfig.java b/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/config/ApplicationConfig.java new file mode 100644 index 0000000..f2eb5fe --- /dev/null +++ b/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/config/ApplicationConfig.java @@ -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; +} diff --git a/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/config/AuthorConfig.java b/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/config/AuthorConfig.java new file mode 100644 index 0000000..18ea05b --- /dev/null +++ b/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/config/AuthorConfig.java @@ -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; +} diff --git a/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/controller/ConfigController.java b/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/controller/ConfigController.java new file mode 100644 index 0000000..ec1a84a --- /dev/null +++ b/spring-boot-demo-properties/src/main/java/com/xkcoding/springbootdemoproperties/controller/ConfigController.java @@ -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 ret = Maps.newHashMap(); + ret.put("applicationConfig", applicationConfig); + ret.put("authorConfig", authorConfig); + return ret; + } +} diff --git a/spring-boot-demo-properties/src/main/resources/application.yml b/spring-boot-demo-properties/src/main/resources/application.yml new file mode 100644 index 0000000..82dcd36 --- /dev/null +++ b/spring-boot-demo-properties/src/main/resources/application.yml @@ -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 \ No newline at end of file diff --git a/spring-boot-demo-properties/src/test/java/com/xkcoding/springbootdemoproperties/SpringBootDemoPropertiesApplicationTests.java b/spring-boot-demo-properties/src/test/java/com/xkcoding/springbootdemoproperties/SpringBootDemoPropertiesApplicationTests.java new file mode 100644 index 0000000..7ed478f --- /dev/null +++ b/spring-boot-demo-properties/src/test/java/com/xkcoding/springbootdemoproperties/SpringBootDemoPropertiesApplicationTests.java @@ -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() { + } + +}