1. Create a web project first join a web-dependent
<-! Web-dependent -> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
2. Add webFlux dependence (which I do not find out, plus a repeat)
<-! Webflux is a new thing, specifically to learn, with the first on here -> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency>
3. Using hot deployment
<-! Hot deployment -> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency>
4. Profiles
# Configure the port number, default 8080 the server.port = 8888 # configuring custom properties as Chinese garbled, Unicode-encoded form so # Combat tours book.name=spring boot2\u5b9e\u6218\u4e4b\u65c5 # Yang Yang book.author=\u6768\u6D0B # Configuration uses a random number # Random string book.value=${random.value} # Random int value book.intValue=${random.int} # Random long value book.longValue=${random.long} # Random uuid book.uuid=${random.uuid} # Random number less than 1000 book.randomNumber=${random.int(1000)} # Self-defined attribute references between The title is # book.title=\u4e66\u540d\u662f:${book.name}
5. Create controller
// equivalent @Controller and @ResponseBody sum @RestController public class TestController { // read the configuration file @Value ( "$ {attribute name}") @Value ( "$ {} book.name" ) Private String bookName ; @Value("${book.author}") private String bookAuthor; // equivalent to @ RequestMapping (= Method, RequestMethod.GET) @GetMapping ( "test1" ) public String test1 () { return "The book title is" + bookName + ", is the author of the book" + BookAuthor; } }
Spring Boot2 title of this book is a real journey, this book is the author Yang Yang
The above has a drawback that when a plurality of attributes, when used @Value ( "attribute name $ {}") would be more troublesome, to create a package JavaBean these properties, such as:
// Because of the custom properties are prefixed to book // need to add promoter based on @EnableConfigurationProperties (BookConfigBean.class) start the configuration class @ConfigurationProperties (prefix = "book" ) public class BookConfigBean { Private String name; Private author String; Private String value; Private int intValue; Private Long longValue; Private String uuid; Private int randomNumber; Private String title; // setter getter }
Add a method to the controller:
@Autowired private BookConfigBean bookConfigBean; @GetMapping("test2") public BookConfigBean test2() { return bookConfigBean; }
result:
{ "Name": "spring boot2 combat tours", "author": "Yang Yang", "value": "44a5b03e2dcdd7a8e428cc8149b22197", "intValue": - 1288322961, "longValue": - 7180575194781037483, "uuid": "44d75104- 4b67-4844-ad40-910cf2719b58 "," randomNumber ": 993," title ":" title is: spring boot2 combat tour "}
Just returned from the object to include all the properties of the object.
Another method does not need to add @EnableConfigurationProperties (BookConfigBean.class) start the startup configuration classes on the class, for example:
@Component @PropertySource(value="classpath:test.properties") @ConfigurationProperties(prefix = "com.book") public class ConfigBean { private String name; private String author; //setter getter }
On the plus @Component JavaBean, @PropertySource (value = "the CLASSPATH: the corresponding configuration file name" ) on the line (do not write is the default configuration file, so the second method does not actually start classes on the plus @EnableConfigurationProperties (BookConfigBean. class), plus @Component directly on BookConfigBean class can also be tested successfully), write a controller:
@Autowired private ConfigBean configBean; @GetMapping("test3") public ConfigBean test3() { return configBean; }
{ "Name": "Spring Boot2 actual trip bbb", "author": "Yang Yang"}
Multi-environment configuration
application-dev.properties: Development Environment
application-test.properties: Test Environment
application-prod.properties: the production environment
Set in application.properties in which the use of the environment: for example spring.profiles.active = test using the test environment
Use page templates
1. Use the template Thymeleaf
1.1 introducing its dependencies
<! - thymeleaf -> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <-! Remove html strict check -> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency>
# Caching is turned on, the proposed closure of the development, otherwise the page will not show the effect of changes in real time spring.thymeleaf.cache=false #Encoding format spring.thymeleaf.encoding=UTF-8 # Use this to remove thymeleaf strict check spring.thymeleaf.mode=LEGACYHTML5 # Prefix spring.thymeleaf.prefix=classpath:/static/ #suffix spring.thymeleaf.suffix=.html
@Controller public class IndexController { @GetMapping("/") public String index(ModelMap modelMap) { modelMap.addAttribute("msg","hello dalaoyang"); return "index"; } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1 th:text="${msg}"></h1> </body> </html>
th: text = "" This is the front end thymeleaf interactive format, more on this later.
2. Using FreeMarker template
2.1 introduces dependence
<!-- FreeMarker--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
# Caching is turned on, the proposed closure of the development, otherwise the page will not show the effect of changes in real time spring.freemarker.cache=false #Encoding format spring.freemarker.charset=UTF-8 # Prefix spring.freemarker.template-load-path=classpath:/static/ #suffix spring.freemarker.suffix=.ftl
2.3 write controller
@Controller public class IndexController { @GetMapping("/") public String index(ModelMap modelMap) { modelMap.addAttribute("msg","hello dalaoyang"); return "index"; } }
<!DOCTYPE html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>${msg}></h1> </body> </html>
Here, compared to less line ThymeLeaf. To configure th should be thymeleaf these things, and FreeMarker does not.
3. Using traditional jsp
Not recommended for use in the future to say.