Skip to content

Commit cac0eae

Browse files
authored
Merge pull request #1 from dyc87112/master
更新代码
2 parents f356489 + 88d49d7 commit cac0eae

File tree

40 files changed

+1085
-6
lines changed

40 files changed

+1085
-6
lines changed

Chapter2-1-2/pom.xml

+54
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.didispace</groupId>
7+
<artifactId>Chapter2-1-2</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter2-1-2</name>
12+
<description>Spring Boot 2.0 features : Application Events and Listeners</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.projectlombok</groupId>
40+
<artifactId>lombok</artifactId>
41+
<scope>provided</scope>
42+
</dependency>
43+
</dependencies>
44+
45+
<build>
46+
<plugins>
47+
<plugin>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-maven-plugin</artifactId>
50+
</plugin>
51+
</plugins>
52+
</build>
53+
54+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.didispace;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.CommandLineRunner;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
@Slf4j
10+
@SpringBootApplication
11+
public class Application {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(Application.class, args);
15+
}
16+
17+
@Bean
18+
public DataLoader dataLoader() {
19+
return new DataLoader();
20+
}
21+
22+
@Slf4j
23+
static class DataLoader implements CommandLineRunner {
24+
25+
@Override
26+
public void run(String... strings) throws Exception {
27+
log.info("Loading data...");
28+
}
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
5+
import org.springframework.context.ApplicationListener;
6+
7+
@Slf4j
8+
public class ApplicationEnvironmentPreparedEventListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
9+
10+
@Override
11+
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
12+
log.info("......ApplicationEnvironmentPreparedEvent......");
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.context.event.ApplicationFailedEvent;
5+
import org.springframework.context.ApplicationListener;
6+
7+
@Slf4j
8+
public class ApplicationFailedEventListener implements ApplicationListener<ApplicationFailedEvent> {
9+
10+
@Override
11+
public void onApplicationEvent(ApplicationFailedEvent event) {
12+
log.info("......ApplicationFailedEvent......");
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.context.event.ApplicationPreparedEvent;
5+
import org.springframework.context.ApplicationListener;
6+
7+
@Slf4j
8+
public class ApplicationPreparedEventListener implements ApplicationListener<ApplicationPreparedEvent> {
9+
10+
@Override
11+
public void onApplicationEvent(ApplicationPreparedEvent event) {
12+
log.info("......ApplicationPreparedEvent......");
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.context.event.ApplicationReadyEvent;
5+
import org.springframework.context.ApplicationListener;
6+
7+
@Slf4j
8+
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {
9+
10+
@Override
11+
public void onApplicationEvent(ApplicationReadyEvent event) {
12+
log.info("......ApplicationReadyEvent......");
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.didispace;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.context.event.ApplicationStartedEvent;
6+
import org.springframework.context.ApplicationListener;
7+
8+
@Slf4j
9+
public class ApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {
10+
11+
@Override
12+
public void onApplicationEvent(ApplicationStartedEvent event) {
13+
log.info("......ApplicationStartedEvent......");
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.didispace;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.boot.context.event.ApplicationStartingEvent;
5+
import org.springframework.context.ApplicationListener;
6+
7+
@Slf4j
8+
public class ApplicationStartingEventListener implements ApplicationListener<ApplicationStartingEvent> {
9+
10+
@Override
11+
public void onApplicationEvent(ApplicationStartingEvent event) {
12+
log.info("......ApplicationStartingEvent......");
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
org.springframework.context.ApplicationListener=com.didispace.ApplicationEnvironmentPreparedEventListener,\
2+
com.didispace.ApplicationFailedEventListener,\
3+
com.didispace.ApplicationPreparedEventListener,\
4+
com.didispace.ApplicationReadyEventListener,\
5+
com.didispace.ApplicationStartedEventListener,\
6+
com.didispace.ApplicationStartingEventListener

Chapter2-1-2/src/main/resources/application.properties

Whitespace-only changes.

Chapter2-2-1/pom.xml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.didispace</groupId>
7+
<artifactId>Chapter2-2-1</artifactId>
8+
<version>1.0.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>Chapter2-2-1</name>
12+
<description>Spring Boot 2 : Relaxed Binding</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.0.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter</artifactId>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.projectlombok</groupId>
34+
<artifactId>lombok</artifactId>
35+
<version>1.16.20</version>
36+
</dependency>
37+
38+
</dependencies>
39+
40+
<build>
41+
<plugins>
42+
<plugin>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-maven-plugin</artifactId>
45+
</plugin>
46+
</plugins>
47+
</build>
48+
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.didispace;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
6+
import org.springframework.boot.context.properties.bind.Bindable;
7+
import org.springframework.boot.context.properties.bind.Binder;
8+
import org.springframework.context.ApplicationContext;
9+
10+
import java.util.List;
11+
12+
/**
13+
*
14+
* @author 程序猿DD
15+
* @version 1.0.0
16+
* @blog http://blog.didispace.com
17+
*
18+
*/
19+
@SpringBootApplication
20+
public class Application {
21+
22+
public static void main(String[] args) {
23+
ApplicationContext context = SpringApplication.run(Application.class, args);
24+
25+
Binder binder = Binder.get(context.getEnvironment());
26+
27+
// 绑定简单配置
28+
FooProperties foo = binder.bind("com.didispace", Bindable.of(FooProperties.class)).get();
29+
System.out.println(foo.getFoo());
30+
31+
// 绑定List配置
32+
List<String> post = binder.bind("com.didispace.post", Bindable.listOf(String.class)).get();
33+
System.out.println(post);
34+
35+
List<PostInfo> posts = binder.bind("com.didispace.posts", Bindable.listOf(PostInfo.class)).get();
36+
System.out.println(posts);
37+
38+
// 读取配置
39+
System.out.println(context.getEnvironment().containsProperty("com.didispace.database-platform"));
40+
System.out.println(context.getEnvironment().containsProperty("com.didispace.databasePlatform"));
41+
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.didispace;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@Data
7+
@ConfigurationProperties(prefix = "com.didispace")
8+
public class FooProperties {
9+
10+
private String foo;
11+
12+
private String databasePlatform;
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.didispace;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@Data
7+
@ConfigurationProperties
8+
public class PostInfo {
9+
10+
private String title;
11+
private String content;
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
com.didispace.foo=bar
2+
com.didispace.database-platform=sql
3+
4+
com.didispace.post[0]=Why Spring Boot
5+
com.didispace.post[1]=Why Spring Cloud
6+
7+
com.didispace.posts[0].title=Why Spring Boot
8+
com.didispace.posts[0].content=It is perfect!
9+
com.didispace.posts[1].title=Why Spring Cloud
10+
com.didispace.posts[1].content=It is perfect too!

Chapter3-1-5/src/main/java/com/didispace/web/UserController.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ public String postUser(@RequestBody User user) {
3636
}
3737

3838
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
39-
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
39+
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
4040
@RequestMapping(value="/{id}", method=RequestMethod.GET)
4141
public User getUser(@PathVariable Long id) {
4242
return users.get(id);
4343
}
4444

4545
@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
4646
@ApiImplicitParams({
47-
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
47+
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path"),
4848
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
4949
})
5050
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
@@ -57,7 +57,7 @@ public String putUser(@PathVariable Long id, @RequestBody User user) {
5757
}
5858

5959
@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
60-
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
60+
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long", paramType = "path")
6161
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
6262
public String deleteUser(@PathVariable Long id) {
6363
users.remove(id);

0 commit comments

Comments
 (0)