mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Spring Boot 属性配置文件
This commit is contained in:
45
springboot-properties/pom.xml
Executable file
45
springboot-properties/pom.xml
Executable file
@@ -0,0 +1,45 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>springboot</groupId>
|
||||||
|
<artifactId>springboot-properties</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>springboot-properties :: Spring boot 配置文件</name>
|
||||||
|
|
||||||
|
<!-- Spring Boot 启动父依赖 -->
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-parent</artifactId>
|
||||||
|
<version>1.5.1.RELEASE</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- Spring Boot Web 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Spring Boot Test 依赖 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Junit -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.12</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-test</artifactId>
|
||||||
|
<version>1.4.2.RELEASE</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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!";
|
||||||
|
}
|
||||||
|
}
|
||||||
5
springboot-properties/src/main/resources/application.yml
Normal file
5
springboot-properties/src/main/resources/application.yml
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
## 家乡属性
|
||||||
|
home:
|
||||||
|
province: 浙江省
|
||||||
|
city: 温岭松门
|
||||||
|
desc: 我家住在${home.province}的${home.city}
|
||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user