Skip to content

Commit 71ba105

Browse files
committed
Add lesson 18
1 parent b7fbb78 commit 71ba105

File tree

19 files changed

+448
-0
lines changed

19 files changed

+448
-0
lines changed
Binary file not shown.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.segmentfault</groupId>
7+
<artifactId>spring-boot-lesson-18</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-lesson-18</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.6.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-actuator</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
54+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.segmentfault.springbootlesson18;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.context.EnvironmentAware;
6+
import org.springframework.context.annotation.ImportResource;
7+
import org.springframework.core.env.*;
8+
9+
import java.util.Arrays;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
@SpringBootApplication
14+
@ImportResource(locations = {
15+
"META-INF/spring/context.xml",
16+
"META-INF/spring/context-test.xml",
17+
"META-INF/spring/context-prod.xml"
18+
})
19+
public class SpringBootLesson18Application implements EnvironmentAware {
20+
21+
public static void main(String[] args) {
22+
SpringApplication application =
23+
new SpringApplication(SpringBootLesson18Application.class);
24+
application.setAdditionalProfiles("prod");
25+
application.run(args);
26+
}
27+
28+
@Override
29+
public void setEnvironment(Environment environment) {
30+
// 获取当前Profiles
31+
System.err.println("当前激活的Profiles : " + Arrays.asList(environment.getActiveProfiles()));
32+
33+
if (environment instanceof ConfigurableEnvironment) {
34+
ConfigurableEnvironment env = ConfigurableEnvironment.class.cast(environment);
35+
36+
MutablePropertySources mutablePropertySources = env.getPropertySources();
37+
38+
Map<String, Object> source = new HashMap<>();
39+
40+
source.put("server.port", 1234);
41+
42+
PropertySource propertySource = new MapPropertySource("java-code", source);
43+
44+
mutablePropertySources.addFirst(propertySource);
45+
46+
}
47+
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.segmentfault.springbootlesson18.configuration;
2+
3+
import com.segmentfault.springbootlesson18.domain.Person;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.context.annotation.Profile;
7+
8+
/**
9+
* TODO
10+
*
11+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
12+
* @see
13+
* @since 2017.09.09
14+
*/
15+
@Configuration
16+
public class PersonConfiguration {
17+
18+
@Bean
19+
@Profile("prod")
20+
public Person zhangxueyou() {
21+
Person person = new Person();
22+
return person;
23+
}
24+
25+
@Bean
26+
@Profile("test")
27+
public Person zhangkai() {
28+
Person person = new Person();
29+
return person;
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.segmentfault.springbootlesson18.context;
2+
3+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
4+
import org.springframework.context.ApplicationListener;
5+
import org.springframework.core.Ordered;
6+
import org.springframework.core.env.ConfigurableEnvironment;
7+
import org.springframework.core.env.MapPropertySource;
8+
import org.springframework.core.env.MutablePropertySources;
9+
import org.springframework.core.env.PropertySource;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
/**
15+
* 自定义 Spring Boot {@link ApplicationListener}
16+
*
17+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
18+
* @see ApplicationEnvironmentPreparedEvent
19+
* @since 2017.09.09
20+
*/
21+
public class CustomizedSpringBootApplicationListener implements
22+
ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
23+
24+
25+
@Override
26+
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
27+
28+
ConfigurableEnvironment env = event.getEnvironment();
29+
30+
MutablePropertySources mutablePropertySources = env.getPropertySources();
31+
32+
Map<String, Object> source = new HashMap<>();
33+
34+
source.put("server.port", 5678);
35+
source.put("spring.profiles.include", "abc");
36+
37+
PropertySource propertySource = new MapPropertySource("from-application-listener", source);
38+
39+
mutablePropertySources.addFirst(propertySource);
40+
}
41+
42+
@Override
43+
public int getOrder() {
44+
return Ordered.HIGHEST_PRECEDENCE;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.segmentfault.springbootlesson18.controller;
2+
3+
import com.segmentfault.springbootlesson18.domain.Book;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* TODO
11+
*
12+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
13+
* @see
14+
* @since 2017.09.09
15+
*/
16+
@RestController
17+
@EnableConfigurationProperties(Book.class)
18+
public class BookController {
19+
20+
@Autowired
21+
private Book book;
22+
23+
@GetMapping("/book")
24+
public Book book() {
25+
return book;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.segmentfault.springbootlesson18.controller;
2+
3+
import com.segmentfault.springbootlesson18.domain.Person;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.beans.factory.annotation.Qualifier;
6+
import org.springframework.beans.factory.annotation.Value;
7+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
8+
import org.springframework.context.EnvironmentAware;
9+
import org.springframework.core.env.Environment;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import java.util.LinkedHashMap;
15+
import java.util.Map;
16+
17+
/**
18+
* {@link Person} {@link RestController}
19+
*
20+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
21+
* @see
22+
* @since 2017.09.09
23+
*/
24+
@RestController
25+
public class PersonController implements EnvironmentAware {
26+
27+
@Autowired
28+
@Qualifier("person")
29+
private Person person;
30+
31+
@Value("${person.id}")
32+
private Long id;
33+
34+
@Value("${person.name:小马哥}")
35+
private String name;
36+
37+
private Integer age;
38+
39+
40+
@GetMapping("/person/xiaomage")
41+
public Person xiaomage() {
42+
return person;
43+
}
44+
45+
@GetMapping("/person/xiaomage/data")
46+
public Map<String, Object> xiaomageData() {
47+
48+
Map<String, Object> data = new LinkedHashMap<>();
49+
50+
data.put("id", id);
51+
data.put("name", name);
52+
data.put("age", age);
53+
54+
return data;
55+
}
56+
57+
@Override
58+
public void setEnvironment(Environment environment) {
59+
60+
this.age = environment.getProperty("person.age", Integer.class);
61+
62+
}
63+
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.segmentfault.springbootlesson18.domain;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
5+
/**
6+
* TODO
7+
*
8+
* @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
9+
* @see
10+
* @since 2017.09.09
11+
*/
12+
@ConfigurationProperties("book")
13+
public class Book {
14+
15+
private String isbn;
16+
17+
private String name;
18+
19+
public String getIsbn() {
20+
return isbn;
21+
}
22+
23+
public void setIsbn(String isbn) {
24+
this.isbn = isbn;
25+
}
26+
27+
public String getName() {
28+
return name;
29+
}
30+
31+
public void setName(String name) {
32+
this.name = name;
33+
}
34+
35+
}

0 commit comments

Comments
 (0)