Skip to content

Commit 6a1fd9b

Browse files
committed
第三篇 : SpringBoot Profile多环境配置
1 parent 9dfe30d commit 6a1fd9b

10 files changed

Lines changed: 325 additions & 0 deletions

File tree

springboot-config/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
46.5 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip

springboot-config/pom.xml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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>com.gf</groupId>
7+
<artifactId>springboot-config</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springboot-config</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.9.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<!-- 导入配置文件处理器,配置文件进行绑定就会有提示 -->
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-configuration-processor</artifactId>
42+
<optional>true</optional>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-maven-plugin</artifactId>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
56+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.gf;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootConfigApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootConfigApplication.class, args);
11+
}
12+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.gf.entity;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
public class Cat {
8+
@Value( "${cat.name}" )
9+
private String name;
10+
@Value( "${cat.age}" )
11+
private Integer age;
12+
13+
public String getName() {
14+
return name;
15+
}
16+
17+
public void setName(String name) {
18+
this.name = name;
19+
}
20+
21+
public Integer getAge() {
22+
return age;
23+
}
24+
25+
public void setAge(Integer age) {
26+
this.age = age;
27+
}
28+
29+
@Override
30+
public String toString() {
31+
final StringBuilder sb = new StringBuilder( "{\"Cat\":{" );
32+
sb.append( "\"name\":\"" )
33+
.append( name ).append( '\"' );
34+
sb.append( ",\"age\":" )
35+
.append( age );
36+
sb.append( "}}" );
37+
return sb.toString();
38+
}
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.gf.entity;
2+
3+
4+
public class Dog {
5+
6+
private String name;
7+
private Integer age;
8+
9+
public String getName() {
10+
return name;
11+
}
12+
13+
public void setName(String name) {
14+
this.name = name;
15+
}
16+
17+
public Integer getAge() {
18+
return age;
19+
}
20+
21+
public void setAge(Integer age) {
22+
this.age = age;
23+
}
24+
25+
@Override
26+
public String toString() {
27+
final StringBuilder sb = new StringBuilder( "{\"Dog\":{" );
28+
sb.append( "\"name\":\"" )
29+
.append( name ).append( '\"' );
30+
sb.append( ",\"age\":" )
31+
.append( age );
32+
sb.append( "}}" );
33+
return sb.toString();
34+
}
35+
}
36+
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package com.gf.entity;
2+
3+
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.stereotype.Component;
6+
7+
import java.util.Date;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
/**
12+
* 将配置文件中的配置,映射到这个组件中
13+
* @ConfigurationProperties: 告诉SpringBoot将本类所有属性和配置文件中相关的配置进行绑定
14+
* prefix = "person" : 配置文件中那个下面的所有属性进行一一映射
15+
* 只有这个这个组件是容器中的组件,才能使用容器提供的@ConfigurationProperties的功能
16+
*/
17+
@Component
18+
@ConfigurationProperties(prefix = "person")
19+
public class Person {
20+
private String lastName;
21+
private Integer age;
22+
private Boolean boss;
23+
private Date birth;
24+
25+
private Map<String , Object> map;
26+
private List<Object> list;
27+
private Dog dog;
28+
29+
public String getLastName() {
30+
return lastName;
31+
}
32+
33+
public void setLastName(String lastName) {
34+
this.lastName = lastName;
35+
}
36+
37+
public Integer getAge() {
38+
return age;
39+
}
40+
41+
public void setAge(Integer age) {
42+
this.age = age;
43+
}
44+
45+
public Boolean getBoss() {
46+
return boss;
47+
}
48+
49+
public void setBoss(Boolean boss) {
50+
this.boss = boss;
51+
}
52+
53+
public Date getBirth() {
54+
return birth;
55+
}
56+
57+
public void setBirth(Date birth) {
58+
this.birth = birth;
59+
}
60+
61+
public Map<String, Object> getMap() {
62+
return map;
63+
}
64+
65+
public void setMap(Map<String, Object> map) {
66+
this.map = map;
67+
}
68+
69+
public List<Object> getList() {
70+
return list;
71+
}
72+
73+
public void setList(List<Object> list) {
74+
this.list = list;
75+
}
76+
77+
public Dog getDog() {
78+
return dog;
79+
}
80+
81+
public void setDog(Dog dog) {
82+
this.dog = dog;
83+
}
84+
85+
@Override
86+
public String toString() {
87+
final StringBuilder sb = new StringBuilder( "{\"Person\":{" );
88+
sb.append( "\"lastName\":\"" )
89+
.append( lastName ).append( '\"' );
90+
sb.append( ",\"age\":" )
91+
.append( age );
92+
sb.append( ",\"boss\":" )
93+
.append( boss );
94+
sb.append( ",\"birth\":\"" )
95+
.append( birth ).append( '\"' );
96+
sb.append( ",\"map\":" )
97+
.append( map );
98+
sb.append( ",\"list\":" )
99+
.append( list );
100+
sb.append( ",\"dog\":" )
101+
.append( dog );
102+
sb.append( "}}" );
103+
return sb.toString();
104+
}
105+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
server:
2+
port: 8081
3+
4+
person:
5+
lastName: zhangsan # 也可写成(松散语法) last-name:zhansang
6+
age: 18
7+
boss: false
8+
birth: 2017/12/12
9+
map: {k1: v1, k2 : 12}
10+
list:
11+
- lisi
12+
- zhaoliu
13+
dog:
14+
name: 史努比
15+
age: 2
16+
email: xxxxxx
17+
cat:
18+
name: Tom
19+
age: 5
20+
email: xxxxxx
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.gf;
2+
3+
import com.gf.entity.Cat;
4+
import com.gf.entity.Person;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.test.context.junit4.SpringRunner;
10+
11+
/**
12+
* SpringBoot单元测试
13+
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
14+
*/
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest
17+
public class SpringbootConfigApplicationTests {
18+
19+
@Autowired
20+
Person person;
21+
22+
@Autowired
23+
Cat cat;
24+
25+
@Test
26+
public void contextLoads() {
27+
System.out.println(person);
28+
System.out.println(cat);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)