From fa5b08a6abab81930309e896d729ea28946e5429 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=8C=BFDD?= Date: Thu, 5 May 2016 14:00:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=E8=AF=A6?= =?UTF-8?q?=E8=A7=A3=EF=BC=9A=E8=87=AA=E5=AE=9A=E4=B9=89=E5=B1=9E=E6=80=A7?= =?UTF-8?q?=E3=80=81=E9=9A=8F=E6=9C=BA=E6=95=B0=E3=80=81=E5=A4=9A=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E9=85=8D=E7=BD=AE=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Chapter2-1-1/pom.xml | 55 +++++++++++ .../main/java/com/didispace/Application.java | 22 +++++ .../com/didispace/service/BlogProperties.java | 96 +++++++++++++++++++ .../com/didispace/web/HelloController.java | 21 ++++ .../main/resources/application-dev.properties | 2 + .../resources/application-prod.properties | 2 + .../resources/application-test.properties | 2 + .../src/main/resources/application.properties | 17 ++++ .../java/com/didispace/ApplicationTests.java | 48 ++++++++++ 9 files changed, 265 insertions(+) create mode 100644 Chapter2-1-1/pom.xml create mode 100755 Chapter2-1-1/src/main/java/com/didispace/Application.java create mode 100644 Chapter2-1-1/src/main/java/com/didispace/service/BlogProperties.java create mode 100755 Chapter2-1-1/src/main/java/com/didispace/web/HelloController.java create mode 100755 Chapter2-1-1/src/main/resources/application-dev.properties create mode 100755 Chapter2-1-1/src/main/resources/application-prod.properties create mode 100755 Chapter2-1-1/src/main/resources/application-test.properties create mode 100755 Chapter2-1-1/src/main/resources/application.properties create mode 100755 Chapter2-1-1/src/test/java/com/didispace/ApplicationTests.java diff --git a/Chapter2-1-1/pom.xml b/Chapter2-1-1/pom.xml new file mode 100644 index 0000000..c85c405 --- /dev/null +++ b/Chapter2-1-1/pom.xml @@ -0,0 +1,55 @@ + + + 4.0.0 + + com.didispace + Chapter2-1-1 + 1.0.0 + jar + + Chapter2-1-1 + + + + org.springframework.boot + spring-boot-starter-parent + 1.3.2.RELEASE + + + + + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + \ No newline at end of file diff --git a/Chapter2-1-1/src/main/java/com/didispace/Application.java b/Chapter2-1-1/src/main/java/com/didispace/Application.java new file mode 100755 index 0000000..f7a9e16 --- /dev/null +++ b/Chapter2-1-1/src/main/java/com/didispace/Application.java @@ -0,0 +1,22 @@ +package com.didispace; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + + SpringApplication.run(Application.class, args); + + } + +} diff --git a/Chapter2-1-1/src/main/java/com/didispace/service/BlogProperties.java b/Chapter2-1-1/src/main/java/com/didispace/service/BlogProperties.java new file mode 100644 index 0000000..63de02a --- /dev/null +++ b/Chapter2-1-1/src/main/java/com/didispace/service/BlogProperties.java @@ -0,0 +1,96 @@ +package com.didispace.service; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +/** + * @author 程序猿DD + * @version 1.0.0 + * @date 16/5/5 下午12:16. + * @blog http://blog.didispace.com + */ +@Component +public class BlogProperties { + + @Value("${com.didispace.blog.name}") + private String name; + @Value("${com.didispace.blog.title}") + private String title; + @Value("${com.didispace.blog.desc}") + private String desc; + + @Value("${com.didispace.blog.value}") + private String value; + @Value("${com.didispace.blog.number}") + private Integer number; + @Value("${com.didispace.blog.bignumber}") + private Long bignumber; + @Value("${com.didispace.blog.test1}") + private Integer test1; + @Value("${com.didispace.blog.test2}") + private Integer test2; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public Integer getNumber() { + return number; + } + + public void setNumber(Integer number) { + this.number = number; + } + + public Long getBignumber() { + return bignumber; + } + + public void setBignumber(Long bignumber) { + this.bignumber = bignumber; + } + + public Integer getTest1() { + return test1; + } + + public void setTest1(Integer test1) { + this.test1 = test1; + } + + public Integer getTest2() { + return test2; + } + + public void setTest2(Integer test2) { + this.test2 = test2; + } +} diff --git a/Chapter2-1-1/src/main/java/com/didispace/web/HelloController.java b/Chapter2-1-1/src/main/java/com/didispace/web/HelloController.java new file mode 100755 index 0000000..58c767e --- /dev/null +++ b/Chapter2-1-1/src/main/java/com/didispace/web/HelloController.java @@ -0,0 +1,21 @@ +package com.didispace.web; + +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RestController +public class HelloController { + + @RequestMapping("/hello") + public String index() { + return "Hello World"; + } + +} \ No newline at end of file diff --git a/Chapter2-1-1/src/main/resources/application-dev.properties b/Chapter2-1-1/src/main/resources/application-dev.properties new file mode 100755 index 0000000..bbeee1f --- /dev/null +++ b/Chapter2-1-1/src/main/resources/application-dev.properties @@ -0,0 +1,2 @@ +# 服务端口 +server.port=1111 \ No newline at end of file diff --git a/Chapter2-1-1/src/main/resources/application-prod.properties b/Chapter2-1-1/src/main/resources/application-prod.properties new file mode 100755 index 0000000..2b3fdee --- /dev/null +++ b/Chapter2-1-1/src/main/resources/application-prod.properties @@ -0,0 +1,2 @@ +# 服务端口 +server.port=3333 \ No newline at end of file diff --git a/Chapter2-1-1/src/main/resources/application-test.properties b/Chapter2-1-1/src/main/resources/application-test.properties new file mode 100755 index 0000000..717d776 --- /dev/null +++ b/Chapter2-1-1/src/main/resources/application-test.properties @@ -0,0 +1,2 @@ +# 服务端口 +server.port=2222 \ No newline at end of file diff --git a/Chapter2-1-1/src/main/resources/application.properties b/Chapter2-1-1/src/main/resources/application.properties new file mode 100755 index 0000000..b2582fb --- /dev/null +++ b/Chapter2-1-1/src/main/resources/application.properties @@ -0,0 +1,17 @@ +com.didispace.blog.name=程序猿DD +com.didispace.blog.title=Spring Boot教程 +com.didispace.blog.desc=${com.didispace.blog.name}正在努力写《${com.didispace.blog.title}》 + +# 随机字符串 +com.didispace.blog.value=${random.value} +# 随机int +com.didispace.blog.number=${random.int} +# 随机long +com.didispace.blog.bignumber=${random.long} +# 10以内的随机数 +com.didispace.blog.test1=${random.int(10)} +# 10-20的随机数 +com.didispace.blog.test2=${random.int[10,20]} + +# 多环境配置文件激活属性 +spring.profiles.active=dev \ No newline at end of file diff --git a/Chapter2-1-1/src/test/java/com/didispace/ApplicationTests.java b/Chapter2-1-1/src/test/java/com/didispace/ApplicationTests.java new file mode 100755 index 0000000..4b4b6ed --- /dev/null +++ b/Chapter2-1-1/src/test/java/com/didispace/ApplicationTests.java @@ -0,0 +1,48 @@ +package com.didispace; + +import com.didispace.service.BlogProperties; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.mock.web.MockServletContext; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +/** + * + * @author 程序猿DD + * @version 1.0.0 + * @blog http://blog.didispace.com + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(Application.class) +public class ApplicationTests { + + private static final Log log = LogFactory.getLog(ApplicationTests.class); + + @Autowired + private BlogProperties blogProperties; + + + @Test + public void getHello() throws Exception { + Assert.assertEquals(blogProperties.getName(), "程序猿DD"); + Assert.assertEquals(blogProperties.getTitle(), "Spring Boot教程"); + Assert.assertEquals(blogProperties.getDesc(), "程序猿DD正在努力写《Spring Boot教程》"); + + log.info("随机数测试输出:"); + log.info("随机字符串 : " + blogProperties.getValue()); + log.info("随机int : " + blogProperties.getNumber()); + log.info("随机long : " + blogProperties.getBignumber()); + log.info("随机10以下 : " + blogProperties.getTest1()); + log.info("随机10-20 : " + blogProperties.getTest2()); + + } + +}