This commit is contained in:
JeffLi1993
2017-04-18 19:26:44 +08:00
committed by liqiangqiang
parent f32f86c9db
commit 502935ff87
7 changed files with 259 additions and 37 deletions

View File

@@ -0,0 +1,61 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:1.5.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:1.5.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:1.5.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.1.9" level="project" />
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.1.9" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jcl-over-slf4j:1.7.22" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.22" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:log4j-over-slf4j:1.7.22" level="project" />
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.17" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:1.5.1.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:8.5.11" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:8.5.11" level="project" />
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:8.5.11" level="project" />
<orderEntry type="library" name="Maven: org.hibernate:hibernate-validator:5.3.4.Final" level="project" />
<orderEntry type="library" name="Maven: javax.validation:validation-api:1.1.0.Final" level="project" />
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.0.Final" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.3.3" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.6" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.6" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.3.6.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:1.5.1.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:1.5.1.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.2.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.2.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.3" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.22" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:2.6.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:1.10.19" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.1" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.4.0" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.3.6.RELEASE" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:4.3.6.RELEASE" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:1.4.2.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:1.5.1.RELEASE" level="project" />
</component>
</module>

View File

@@ -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 + '\'' +
'}';
}
}

View File

@@ -9,7 +9,7 @@ import org.springframework.stereotype.Component;
* Created by bysocket on 17/04/2017. * Created by bysocket on 17/04/2017.
*/ */
@Component @Component
public class HomeProperties { public class HomeProperties1 {
/** /**
* 省份 * 省份

View File

@@ -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;
}
}

View File

@@ -3,3 +3,10 @@ home:
province: 浙江省 province: 浙江省
city: 温岭松门 city: 温岭松门
desc: 我家住在${home.province}的${home.city} desc: 我家住在${home.province}的${home.city}
## 随机属性
user:
id: ${random.long}
age: ${random.int[1,200]}
desc: 泥瓦匠叫做${random.value}
uuid: ${random.uuid}

View File

@@ -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());
}
}

View File

@@ -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;
/**
* 自定义配置文件测试类
* <p>
* 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());
}
}