mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-14 14:13:52 +08:00
Spring Boot 配置文件
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,55 +0,0 @@
|
||||
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 HomeProperties1 {
|
||||
|
||||
/**
|
||||
* 省份
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
}
|
||||
@@ -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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
## 家乡属性 Dev
|
||||
home.province=ZheJiang
|
||||
home.city=WenLing
|
||||
home.desc=dev: I'm living in ${home.province} ${home.city}.
|
||||
@@ -0,0 +1,4 @@
|
||||
## 家乡属性 Prod
|
||||
home.province=ZheJiang
|
||||
home.city=WenLing
|
||||
home.desc=prod: I'm living in ${home.province} ${home.city}.
|
||||
@@ -0,0 +1,3 @@
|
||||
# Spring Profiles Active
|
||||
spring.main.banner-mode=off
|
||||
spring.profiles.active=dev
|
||||
@@ -1,12 +0,0 @@
|
||||
## 家乡属性
|
||||
home:
|
||||
province: 浙江省
|
||||
city: 温岭松门
|
||||
desc: 我家住在${home.province}的${home.city}
|
||||
|
||||
## 随机属性
|
||||
user:
|
||||
id: ${random.long}
|
||||
age: ${random.int[1,200]}
|
||||
desc: 泥瓦匠叫做${random.value}
|
||||
uuid: ${random.uuid}
|
||||
Reference in New Issue
Block a user