Reading notes: [The subversive developer of Java EE Development Spring Boot combat] Author: Wang Yunfei
Start reading today and keep a record of this reading note.
One, First contact
Spring Boot Project Hello World build
The 1.pom.xml configuration is as follows:
<Projectxmlns= "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> <groupId>Com.springboot</groupId> <Artifactid>Springboot_test1_1</Artifactid> <version>0.0.1-snapshot</version> <Packaging>Jar</Packaging> <name>Springboot_test1_1</name> <URL>http://maven.apache.org</URL> <Parent> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-parent</Artifactid> <version>1.5.2.RELEASE</version> <RelativePath/> </Parent> <Properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </Properties> <Dependencies> <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-web</Artifactid> </Dependency> </Dependencies> <Build> <Plugins> <plugin> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-maven-plugin</Artifactid> </plugin> </Plugins> </Build></Project>
Pom.xml
The 2.main entry class is as follows:
Packagecom.springboot.springboot_test1_1;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController//@SpringBootApplication is the core annotation of the Spring boot project//It is a combination of annotations, the main combination of @configuration, @EnableAutoConfiguration, @ComponentScan//If you do not use @springbootapplication, use @configuration directly, @EnableAutoConfiguration, @ComponentScan can also//TIPS1://@EnableAutoConfiguration: Let spring boot automatically configure the current project based on the jar package dependencies in the Classpath//TIPS2://Spring Boot automatically scans the peers of the @springbootapplication annotation class and the bean in the sub-package, so that the @springbootapplication annotation class is typically placed in the top-level directory@SpringBootApplication Public classApp {//Main method as the starting entry Public Static voidMain (string[] args) {Springapplication.run (App.class, args); } @RequestMapping ("/") PublicString Index () {return"Hello Spring boot!"; }}App.java
3. Run main class, browser access: http://localhost:8080/
Output:
Hello Spring boot!
It worked.
Second, the Properties configuration file
1. Using the Application.properties configuration file
Spring Boot can use the application.properties configuration file, such as using application.properties to modify the port number and access path for Tomcat:
Application.properties:
server.port=9090server.context-path=/helloboot
Browser access: http://localhost:9090/helloboot/
Success.
2. Use the properties in the Application.properties configuration file
Application.properties:
App.name=spring Boot test App
App.java:
Packagecom.springboot.springboot_test1_1;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController//@SpringBootApplication is the core annotation of the Spring boot project//It is a combination of annotations, the main combination of @configuration, @EnableAutoConfiguration, @ComponentScan//If you do not use @springbootapplication, use @configuration directly, @EnableAutoConfiguration, @ComponentScan can also//TIPS1://@EnableAutoConfiguration: Let spring boot automatically configure the current project based on the jar package dependencies in the Classpath//TIPS2://Spring//Boot automatically scans the peers of the @springbootapplication annotation class and the bean in the sub-package, so that the @springbootapplication annotation class is typically placed in the top-level directory@SpringBootApplication Public classApp {//Main method as the starting entry Public Static voidMain (string[] args) {Springapplication.run (App.class, args); } @Value ("${app.name}") PrivateString AppName; @RequestMapping ("/") PublicString Index () {return"Hello Spring boot! AppName: "+AppName; }}App.java
3. Type-safe configuration (based on properties)
Application.properties:
Author.name=cainiao87author.age=
App.java:
Packagecom.springboot.springboot_test1_1;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.beans.factory.annotation.Value;Importorg.springframework.boot.SpringApplication;Importorg.springframework.boot.autoconfigure.SpringBootApplication;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RestController; @RestController//@SpringBootApplication is the core annotation of the Spring boot project//It is a combination of annotations, the main combination of @configuration, @EnableAutoConfiguration, @ComponentScan//If you do not use @springbootapplication, use @configuration directly, @EnableAutoConfiguration, @ComponentScan can also//TIPS1://@EnableAutoConfiguration: Let spring boot automatically configure the current project based on the jar package dependencies in the Classpath//TIPS2://Spring//Boot automatically scans the peers of the @springbootapplication annotation class and the bean in the sub-package, so that the @springbootapplication annotation class is typically placed in the top-level directory@SpringBootApplication Public classApp {//Main method as the starting entry Public Static voidMain (string[] args) {Springapplication.run (App.class, args); } @Value ("${app.name}") PrivateString AppName; @AutowiredPrivateauthorsettings setting; @RequestMapping ("/") PublicString Index () {return"Hello Spring boot! AppName: "+AppName; } @RequestMapping ("/author") PublicString author () {return"I ' m:" + setting.getname () + ", Age:" +Setting.getage (); }}App.java
Three, XML configuration file
Spring Boot does not recommend the use of an XML configuration file, and in some cases it must be required:
Using annotations: @ImportResource ("Classpath:xxx-config.xml")
Directory structure:
Four, profile configuration
Modify the Application.properties file, remove the Server.port property, and add the properties:
Spring.profiles.active=dev
2 new. Properties Configuration file:
Application-dev.properties:
server.port=8080
Application-prod.properties:
Server.port=80
This allows you to switch between the test and production environment configuration files by setting up Spring.profiles.active=dev or prod.
[Reading notes] One, Spring boot project setup and configuration files