Spring Boot 入门、Spring Boot 配置、Web 开发、模板引擎、数据存储、数据缓存 案例更新

This commit is contained in:
liqiangqiang
2018-04-08 14:46:30 +08:00
parent c862ecd73b
commit fc5200546c
70 changed files with 2201 additions and 293 deletions

View File

@@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<name>chapter-2-spring-boot-config</name>
<description>《Spring Boot 2.x 核心技术实战 - 上 基础篇》第 2 章《Spring Boot 配置》Demo</description>
<groupId>demo.springboot</groupId>
<artifactId>chapter-2-spring-boot-config</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 自定义 swagger2 Starter 组件依赖 -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>spring-boot-starter-swagger</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Spring Boot Maven 插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.1.RELEASE</version>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/libs-snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>

View File

@@ -0,0 +1,18 @@
package demo.springboot;
import com.spring4all.swagger.EnableSwagger2Doc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Spring Boot 应用启动类
*
* Created by bysocket on 26/09/2017.
*/
@EnableSwagger2Doc // 开启 Swagger
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}

View File

@@ -0,0 +1,46 @@
package demo.springboot.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 书属性
*
*/
@Component
@ConfigurationProperties(prefix = "demo.book")
@Validated
public class BookComponent {
/**
* 书名
*/
@NotEmpty
private String name;
/**
* 作者
*/
@NotNull
private String writer;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
}

View File

@@ -0,0 +1,41 @@
package demo.springboot.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 书属性
*
* Created by bysocket on 27/09/2017.
*/
@Component
public class BookProperties {
/**
* 书名
*/
@Value("${demo.book.name}")
private String name;
/**
* 作者
*/
@Value("${demo.book.writer}")
private String writer;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
}

View File

@@ -0,0 +1,24 @@
package demo.springboot.web;
import demo.springboot.config.BookProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Spring Boot Hello案例
*
* Created by bysocket on 26/09/2017.
*/
@RestController
public class HelloBookController {
@Autowired
BookProperties bookProperties;
@GetMapping("/book/hello")
public String sayHello() {
return "Hello " + bookProperties.getWriter() + " is writing "
+ bookProperties.getName() + " ";
}
}

View File

@@ -0,0 +1,3 @@
## \u4E66\u4FE1\u606F
demo.book.name=[Spring Boot 2.x Core Action] From Dev
demo.book.writer=BYSocket

View File

@@ -0,0 +1,3 @@
## \u4E66\u4FE1\u606F
demo.book.name=[Spring Boot 2.x Core Action] From Prod
demo.book.writer=BYSocket

View File

@@ -0,0 +1,5 @@
## \u4E66\u4FE1\u606F
demo.book.name=[Spring Boot 2.x Core Action]
demo.book.writer=BYSocket
demo.book.description=${demo.book.writer}'s${demo.book.name}

View File

@@ -0,0 +1,5 @@
## 书信息
demo:
book:
name: 《Spring Boot 2.x 核心技术实战 - 上 基础篇》
writer: 泥瓦匠BYSocket

View File

@@ -0,0 +1,33 @@
package demo.springboot;
import demo.springboot.config.BookComponent;
import demo.springboot.config.BookProperties;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ConfigApplicationTests {
@Autowired
BookProperties bookProperties;
@Autowired
BookComponent bookComponent;
@Test
public void testBookProperties() {
Assert.assertEquals(bookProperties.getName(),"[Spring Boot 2.x Core Action]");
Assert.assertEquals(bookProperties.getWriter(),"BYSocket");
}
@Test
public void testBookComponent() {
Assert.assertEquals(bookComponent.getName(),"[Spring Boot 2.x Core Action]");
Assert.assertEquals(bookComponent.getWriter(),"BYSocket");
}
}