mirror of
https://github.com/JeffLi1993/springboot-learning-example.git
synced 2026-03-13 21:43:45 +08:00
Spring Boot 入门、Spring Boot 配置、Web 开发、模板引擎、数据存储、数据缓存 案例更新
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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() + " !";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
## \u4E66\u4FE1\u606F
|
||||
demo.book.name=[Spring Boot 2.x Core Action] From Dev
|
||||
demo.book.writer=BYSocket
|
||||
@@ -0,0 +1,3 @@
|
||||
## \u4E66\u4FE1\u606F
|
||||
demo.book.name=[Spring Boot 2.x Core Action] From Prod
|
||||
demo.book.writer=BYSocket
|
||||
@@ -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}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
## 书信息
|
||||
demo:
|
||||
book:
|
||||
name: 《Spring Boot 2.x 核心技术实战 - 上 基础篇》
|
||||
writer: 泥瓦匠BYSocket
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user