Skip to content

Commit f32f86c

Browse files
JeffLi1993liqiangqiang
authored andcommitted
Spring Boot 属性配置文件
1 parent ed91d43 commit f32f86c

File tree

7 files changed

+197
-0
lines changed

7 files changed

+197
-0
lines changed

springboot-properties/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>springboot</groupId>
7+
<artifactId>springboot-properties</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<name>springboot-properties :: Spring boot 配置文件</name>
10+
11+
<!-- Spring Boot 启动父依赖 -->
12+
<parent>
13+
<groupId>org.springframework.boot</groupId>
14+
<artifactId>spring-boot-starter-parent</artifactId>
15+
<version>1.5.1.RELEASE</version>
16+
</parent>
17+
18+
<dependencies>
19+
<!-- Spring Boot Web 依赖 -->
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
25+
<!-- Spring Boot Test 依赖 -->
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-test</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
32+
<!-- Junit -->
33+
<dependency>
34+
<groupId>junit</groupId>
35+
<artifactId>junit</artifactId>
36+
<version>4.12</version>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-test</artifactId>
41+
<version>1.4.2.RELEASE</version>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.spring.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* Spring Boot 应用启动类
8+
*
9+
* Created by bysocket on 16/4/26.
10+
*/
11+
// Spring Boot 应用的标识
12+
@SpringBootApplication
13+
public class Application {
14+
15+
public static void main(String[] args) {
16+
// 程序启动入口
17+
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
18+
SpringApplication.run(Application.class,args);
19+
}
20+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.spring.springboot.property;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
/**
7+
* 家乡属性
8+
*
9+
* Created by bysocket on 17/04/2017.
10+
*/
11+
@Component
12+
public class HomeProperties {
13+
14+
/**
15+
* 省份
16+
*/
17+
@Value("${home.province}")
18+
private String province;
19+
20+
/**
21+
* 城市
22+
*/
23+
@Value("${home.city}")
24+
private String city;
25+
26+
/**
27+
* 描述
28+
*/
29+
@Value("${home.desc}")
30+
private String desc;
31+
32+
public String getProvince() {
33+
return province;
34+
}
35+
36+
public void setProvince(String province) {
37+
this.province = province;
38+
}
39+
40+
public String getCity() {
41+
return city;
42+
}
43+
44+
public void setCity(String city) {
45+
this.city = city;
46+
}
47+
48+
public String getDesc() {
49+
return desc;
50+
}
51+
52+
public void setDesc(String desc) {
53+
this.desc = desc;
54+
}
55+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.spring.springboot.web;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
/**
7+
* Spring Boot HelloWorld 案例
8+
*
9+
* Created by bysocket on 16/4/26.
10+
*/
11+
@RestController
12+
public class HelloWorldController {
13+
14+
@RequestMapping("/")
15+
public String sayHello() {
16+
return "Hello,World!";
17+
}
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## 家乡属性
2+
home:
3+
province: 浙江省
4+
city: 温岭松门
5+
desc: 我家住在${home.province}的${home.city}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.spring.springboot.property;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.test.context.SpringBootTest;
10+
import org.springframework.test.context.junit4.SpringRunner;
11+
12+
/**
13+
* 自定义配置文件测试类
14+
*
15+
* Created by bysocket on 17/04/2017.
16+
*/
17+
@RunWith(SpringRunner.class)
18+
@SpringBootTest
19+
public class HomePropertiesTest {
20+
21+
private static final Logger LOGGER = LoggerFactory.getLogger(HomePropertiesTest.class);
22+
23+
@Autowired
24+
private HomeProperties homeProperties;
25+
26+
@Test
27+
public void getHomeProperties() {
28+
LOGGER.info(homeProperties.getProvince());
29+
Assert.assertEquals("浙江省",homeProperties.getProvince());
30+
31+
LOGGER.info(homeProperties.getCity());
32+
Assert.assertEquals("温岭松门",homeProperties.getCity());
33+
34+
LOGGER.info(homeProperties.getDesc());
35+
}
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.spring.springboot.web;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* Spring Boot HelloWorldController 测试 - {@link HelloWorldController}
9+
*
10+
* Created by bysocket on 16/4/26.
11+
*/
12+
public class HelloWorldControllerTest {
13+
14+
@Test
15+
public void testSayHello() {
16+
assertEquals("Hello,World!",new HelloWorldController().sayHello());
17+
}
18+
}

0 commit comments

Comments
 (0)