From 502935ff87ed1f76cfb86afdaeb37e9593469949 Mon Sep 17 00:00:00 2001 From: JeffLi1993 Date: Tue, 18 Apr 2017 19:26:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springboot-properties.iml | 61 +++++++++++++++ .../org/spring/springboot/domain/User.java | 76 +++++++++++++++++++ ...meProperties.java => HomeProperties1.java} | 2 +- .../springboot/property/HomeProperties2.java | 54 +++++++++++++ .../src/main/resources/application.yml | 7 ++ .../property/HomePropertiesTest.java | 36 --------- .../springboot/property/PropertiesTest.java | 60 +++++++++++++++ 7 files changed, 259 insertions(+), 37 deletions(-) create mode 100644 springboot-properties/springboot-properties.iml create mode 100644 springboot-properties/src/main/java/org/spring/springboot/domain/User.java rename springboot-properties/src/main/java/org/spring/springboot/property/{HomeProperties.java => HomeProperties1.java} (96%) create mode 100644 springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties2.java delete mode 100644 springboot-properties/src/test/java/org/spring/springboot/property/HomePropertiesTest.java create mode 100644 springboot-properties/src/test/java/org/spring/springboot/property/PropertiesTest.java diff --git a/springboot-properties/springboot-properties.iml b/springboot-properties/springboot-properties.iml new file mode 100644 index 0000000..f72e1c9 --- /dev/null +++ b/springboot-properties/springboot-properties.iml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/springboot-properties/src/main/java/org/spring/springboot/domain/User.java b/springboot-properties/src/main/java/org/spring/springboot/domain/User.java new file mode 100644 index 0000000..089c61a --- /dev/null +++ b/springboot-properties/src/main/java/org/spring/springboot/domain/User.java @@ -0,0 +1,76 @@ +package org.spring.springboot.domain; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * 用户 + * + * Created by bysocket on 18/04/2017. + */ +@Component +@ConfigurationProperties(prefix = "user") +public class User { + + /** + * 用户 ID + */ + private Long id; + + /** + * 年龄 + */ + private int age; + + /** + * 用户名称 + */ + private String desc; + + /** + * 用户 UUID + */ + private String uuid; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public String getUuid() { + return uuid; + } + + public void setUuid(String uuid) { + this.uuid = uuid; + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", age=" + age + + ", desc=" + desc + + ", uuid='" + uuid + '\'' + + '}'; + } +} diff --git a/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties.java b/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties1.java similarity index 96% rename from springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties.java rename to springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties1.java index ed8ae5b..92e7990 100644 --- a/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties.java +++ b/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties1.java @@ -9,7 +9,7 @@ import org.springframework.stereotype.Component; * Created by bysocket on 17/04/2017. */ @Component -public class HomeProperties { +public class HomeProperties1 { /** * 省份 diff --git a/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties2.java b/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties2.java new file mode 100644 index 0000000..40fd7c5 --- /dev/null +++ b/springboot-properties/src/main/java/org/spring/springboot/property/HomeProperties2.java @@ -0,0 +1,54 @@ +package org.spring.springboot.property; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +/** + * 家乡属性 + * + * Created by bysocket on 17/04/2017. + */ +@Component +@ConfigurationProperties(prefix = "home") +public class HomeProperties2 { + + /** + * 省份 + */ + private String province; + + /** + * 城市 + */ + private String city; + + /** + * 描述 + */ + 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/resources/application.yml b/springboot-properties/src/main/resources/application.yml index 13d0e54..133a3d6 100644 --- a/springboot-properties/src/main/resources/application.yml +++ b/springboot-properties/src/main/resources/application.yml @@ -3,3 +3,10 @@ home: province: 浙江省 city: 温岭松门 desc: 我家住在${home.province}的${home.city} + +## 随机属性 +user: + id: ${random.long} + age: ${random.int[1,200]} + desc: 泥瓦匠叫做${random.value} + uuid: ${random.uuid} 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 deleted file mode 100644 index 8a42dfb..0000000 --- a/springboot-properties/src/test/java/org/spring/springboot/property/HomePropertiesTest.java +++ /dev/null @@ -1,36 +0,0 @@ -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/property/PropertiesTest.java b/springboot-properties/src/test/java/org/spring/springboot/property/PropertiesTest.java new file mode 100644 index 0000000..86a1c40 --- /dev/null +++ b/springboot-properties/src/test/java/org/spring/springboot/property/PropertiesTest.java @@ -0,0 +1,60 @@ +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.spring.springboot.domain.User; +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 PropertiesTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesTest.class); + + @Autowired + private HomeProperties1 homeProperties1; + + @Autowired + private HomeProperties2 homeProperties2; + + @Autowired + private User user; + + @Test + public void getHomeProperties1() { + LOGGER.info(homeProperties1.getProvince()); + Assert.assertEquals("浙江省", homeProperties1.getProvince()); + + LOGGER.info(homeProperties1.getCity()); + Assert.assertEquals("温岭松门", homeProperties1.getCity()); + + LOGGER.info(homeProperties1.getDesc()); + } + + @Test + public void getHomeProperties2() { + LOGGER.info(homeProperties2.getProvince()); + Assert.assertEquals("浙江省", homeProperties2.getProvince()); + + LOGGER.info(homeProperties2.getCity()); + Assert.assertEquals("温岭松门", homeProperties2.getCity()); + + LOGGER.info(homeProperties2.getDesc()); + } + + @Test + public void randomTestUser() { + LOGGER.info(user.toString()); + } + +}