Spring Boot 配置文件

This commit is contained in:
JeffLi1993
2017-04-20 13:50:09 +08:00
committed by liqiangqiang
parent b592e7948f
commit 4222985d43
9 changed files with 56 additions and 38 deletions

View File

@@ -1,20 +1,32 @@
package org.spring.springboot;
import org.spring.springboot.property.HomeProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot 应用启动类
*
* <p>
* Created by bysocket on 16/4/26.
*/
// Spring Boot 应用的标识
@SpringBootApplication
public class Application {
public class Application implements CommandLineRunner {
@Autowired
private HomeProperties homeProperties;
public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(Application.class,args);
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
System.out.println("\n" + homeProperties.toString());
System.out.println();
}
}

View File

@@ -11,7 +11,7 @@ import org.springframework.stereotype.Component;
*/
@Component
@ConfigurationProperties(prefix = "home")
public class HomeProperties2 {
public class HomeProperties {
/**
* 省份
@@ -51,4 +51,13 @@ public class HomeProperties2 {
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "HomeProperties{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", desc='" + desc + '\'' +
'}';
}
}

View File

@@ -1,17 +1,14 @@
package org.spring.springboot.domain;
package org.spring.springboot.property;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 用户
*
* Created by bysocket on 18/04/2017.
* Created by bysocket on 20/04/2017.
*/
@Component
@ConfigurationProperties(prefix = "user")
public class User {
public class UserProperties {
/**
* 用户 ID
*/
@@ -64,12 +61,13 @@ public class User {
this.uuid = uuid;
}
@Override
public String toString() {
return "User{" +
return "UserProperties{" +
"id=" + id +
", age=" + age +
", desc=" + desc +
", desc='" + desc + '\'' +
", uuid='" + uuid + '\'' +
'}';
}

View File

@@ -0,0 +1,4 @@
## 家乡属性 Dev
home.province=ZheJiang
home.city=WenLing
home.desc=dev: I'm living in ${home.province} ${home.city}.

View File

@@ -0,0 +1,4 @@
## 家乡属性 Prod
home.province=ZheJiang
home.city=WenLing
home.desc=prod: I'm living in ${home.province} ${home.city}.

View File

@@ -0,0 +1,3 @@
# Spring Profiles Active
spring.main.banner-mode=off
spring.profiles.active=dev

View File

@@ -52,4 +52,13 @@ public class HomeProperties1 {
public void setDesc(String desc) {
this.desc = desc;
}
@Override
public String toString() {
return "HomeProperties1{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
", desc='" + desc + '\'' +
'}';
}
}

View File

@@ -5,7 +5,6 @@ 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;
@@ -22,39 +21,19 @@ public class PropertiesTest {
private static final Logger LOGGER = LoggerFactory.getLogger(PropertiesTest.class);
@Autowired
private HomeProperties1 homeProperties1;
private UserProperties userProperties;
@Autowired
private HomeProperties2 homeProperties2;
@Autowired
private User user;
private HomeProperties homeProperties;
@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());
public void getHomeProperties() {
LOGGER.info(homeProperties.toString());
}
@Test
public void randomTestUser() {
LOGGER.info(user.toString());
LOGGER.info(userProperties.toString());
}
}