From f32f86c9dba8552d0f141cb77a12f5c9cabfb808 Mon Sep 17 00:00:00 2001 From: JeffLi1993 Date: Mon, 17 Apr 2017 15:07:15 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=20=E5=B1=9E=E6=80=A7=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- springboot-properties/pom.xml | 45 +++++++++++++++ .../org/spring/springboot/Application.java | 20 +++++++ .../springboot/property/HomeProperties.java | 55 +++++++++++++++++++ .../springboot/web/HelloWorldController.java | 18 ++++++ .../src/main/resources/application.yml | 5 ++ .../property/HomePropertiesTest.java | 36 ++++++++++++ .../web/HelloWorldControllerTest.java | 18 ++++++ 7 files changed, 197 insertions(+) create mode 100755 springboot-properties/pom.xml create mode 100644 springboot-properties/src/main/java/org/spring/springboot/Application.java create mode 100644 springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties.java create mode 100644 springboot-properties/src/main/java/org/spring/springboot/web/HelloWorldController.java create mode 100644 springboot-properties/src/main/resources/application.yml create mode 100644 springboot-properties/src/test/java/org/spring/springboot/property/HomePropertiesTest.java create mode 100644 springboot-properties/src/test/java/org/spring/springboot/web/HelloWorldControllerTest.java diff --git a/springboot-properties/pom.xml b/springboot-properties/pom.xml new file mode 100755 index 0000000..c835662 --- /dev/null +++ b/springboot-properties/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + springboot + springboot-properties + 0.0.1-SNAPSHOT + springboot-properties :: Spring boot 配置文件 + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + junit + junit + 4.12 + + + org.springframework.boot + spring-boot-test + 1.4.2.RELEASE + test + + + diff --git a/springboot-properties/src/main/java/org/spring/springboot/Application.java b/springboot-properties/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..5070937 --- /dev/null +++ b/springboot-properties/src/main/java/org/spring/springboot/Application.java @@ -0,0 +1,20 @@ +package org.spring.springboot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Spring Boot 应用启动类 + * + * Created by bysocket on 16/4/26. + */ +// Spring Boot 应用的标识 +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + // 程序启动入口 + // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 + SpringApplication.run(Application.class,args); + } +} diff --git a/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties.java b/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties.java new file mode 100644 index 0000000..ed8ae5b --- /dev/null +++ b/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties.java @@ -0,0 +1,55 @@ +package org.spring.springboot.property; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * 家乡属性 + * + * Created by bysocket on 17/04/2017. + */ +@Component +public class HomeProperties { + + /** + * 省份 + */ + @Value("${home.province}") + private String province; + + /** + * 城市 + */ + @Value("${home.city}") + private String city; + + /** + * 描述 + */ + @Value("${home.desc}") + private String desc; + + public String getProvince() { + return province; + } + + public void setProvince(String province) { + this.province = province; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } +} diff --git a/springboot-properties/src/main/java/org/spring/springboot/web/HelloWorldController.java b/springboot-properties/src/main/java/org/spring/springboot/web/HelloWorldController.java new file mode 100644 index 0000000..44bb15d --- /dev/null +++ b/springboot-properties/src/main/java/org/spring/springboot/web/HelloWorldController.java @@ -0,0 +1,18 @@ +package org.spring.springboot.web; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * Spring Boot HelloWorld 案例 + * + * Created by bysocket on 16/4/26. + */ +@RestController +public class HelloWorldController { + + @RequestMapping("/") + public String sayHello() { + return "Hello,World!"; + } +} diff --git a/springboot-properties/src/main/resources/application.yml b/springboot-properties/src/main/resources/application.yml new file mode 100644 index 0000000..13d0e54 --- /dev/null +++ b/springboot-properties/src/main/resources/application.yml @@ -0,0 +1,5 @@ +## 家乡属性 +home: + province: 浙江省 + city: 温岭松门 + desc: 我家住在${home.province}的${home.city} diff --git a/springboot-properties/src/test/java/org/spring/springboot/property/HomePropertiesTest.java b/springboot-properties/src/test/java/org/spring/springboot/property/HomePropertiesTest.java new file mode 100644 index 0000000..8a42dfb --- /dev/null +++ b/springboot-properties/src/test/java/org/spring/springboot/property/HomePropertiesTest.java @@ -0,0 +1,36 @@ +package org.spring.springboot.property; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * 自定义配置文件测试类 + * + * Created by bysocket on 17/04/2017. + */ +@RunWith(SpringRunner.class) +@SpringBootTest +public class HomePropertiesTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(HomePropertiesTest.class); + + @Autowired + private HomeProperties homeProperties; + + @Test + public void getHomeProperties() { + LOGGER.info(homeProperties.getProvince()); + Assert.assertEquals("浙江省",homeProperties.getProvince()); + + LOGGER.info(homeProperties.getCity()); + Assert.assertEquals("温岭松门",homeProperties.getCity()); + + LOGGER.info(homeProperties.getDesc()); + } +} diff --git a/springboot-properties/src/test/java/org/spring/springboot/web/HelloWorldControllerTest.java b/springboot-properties/src/test/java/org/spring/springboot/web/HelloWorldControllerTest.java new file mode 100644 index 0000000..559364b --- /dev/null +++ b/springboot-properties/src/test/java/org/spring/springboot/web/HelloWorldControllerTest.java @@ -0,0 +1,18 @@ +package org.spring.springboot.web; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * Spring Boot HelloWorldController 测试 - {@link HelloWorldController} + * + * Created by bysocket on 16/4/26. + */ +public class HelloWorldControllerTest { + + @Test + public void testSayHello() { + assertEquals("Hello,World!",new HelloWorldController().sayHello()); + } +}