1. Create a project
Use IntelliJ idea to create a new spring boot Project for Web engineering
2. View the initialized Spring boot project
After the project is built, the following directory structure will appear:
The first thing to note is that the entire project structure follows the layout of the traditional Maven project, where the main application code is located in the Src/main/java directory, the resources are in the Src/main/resources directory, and the test code is in the Src/test/java directory. There are no testing resources at the moment, but if so, put them in the src/test/resources.
Further, you will see that there are still many documents in the project.
- Pom.xml:Maven build the documentation.
- springboothelloworldapplication: The application's Startup boot Class (Bootstrap Class), which is also the primary spring configuration class.
- Application.properties: Used to configure the properties of the application and spring boot.
- springboothelloworldapplicationtests: A basic integration Test class.
Let's take a look at springboothelloworldapplicationfirst.
2.1 Boot Boot Spring
springboothelloworldapplication has two functions in the Spring boot application: Configuration and Boot boot. First, this is the main spring configuration class. Although spring boot's automatic configuration eliminates many spring configurations, you also need to make a few configurations to enable automatic configuration.
Package Com.cloud; Import org.springframework.boot.SpringApplication; Import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication Public class springboothelloworldapplication { publicstaticvoid Main ( String[] args) { springapplication.run (springboothelloworldapplication. class , args);} }
The @SpringBootApplication opens the Spring component scan and spring boot auto-configuration feature. In fact, @SpringBootApplication combine three useful annotations together.
- Spring's @configuration: Indicates that the class uses the spring Java-based configuration. Although this book does not write too many configurations, we would prefer to use Java-based rather than XML configurations.
- Spring @componentscan: Enables component scanning so that the Web controller classes and other components that you write can be automatically discovered and registered as beans in the spring application context. A simple spring MVC controller is written later in this chapter, annotated with @controller so that the component scan can find it.
- Spring boot @enableautoconfiguration: This humble little note can also be called @abracadabra, that is, this line of configuration turns on the magic of Spring boot auto-configuration, so you don't have to write the configuration of the article.
In earlier versions of Spring boot, you would need to mark these three annotations on the Readinglistapplication class at the same time, but starting with spring boot 1.2.0, there are @springbootapplication.
As I said,springboothelloworldapplication is still a startup boot class. There are several ways to run the Spring boot application, which includes the traditional war file deployment. But here the main () method allows you to run the application as an executable jar file on the command line. Here, the Springapplication.run () passes a
The springboothelloworldapplication class references, as well as command-line arguments, start the application through these things.
In fact, even if a line of code is not written, you can still build the application to taste fresh.
The
application should function properly to start a Tomcat server that listens on port 8080. If you like, you can access http://localhost:8080 with your browser, but since you haven't written the controller class, you will only receive an HTTP 404 (not FOUND) error and see the error page. This URL will provide a reading list application before the end of this chapter.
You hardly need to modify springboothelloworldapplication . java. If your application requires a spring configuration other than spring boot autoconfiguration, it is generally best to write it in a separate @configuration labeled class. (component scans will discover and use these classes.) In extremely simple cases, you can add a custom configuration to springboothelloworldapplication . java.
2.2. Configure application properties
The application.properties file generated for you by INITIALIZR is an empty file. In fact, this file is completely optional, you can delete it, it won't have any effect on the application, but it's OK to keep it.
Later on, we certainly have a chance to add a few entries to application.properties. But now, if you want to try a little sledgehammer,
You can add a line to see:
server.port=8000
With this line, the embedded Tomcat's listening port becomes 8000 instead of the default of 8080. You can rerun the application to see if this is the case.
This means that the Application.properties file can easily help you fine-tune the automatic configuration of spring boot in a granular manner. You can also use it to specify the configuration items that your application code requires. In the 3rd chapter we will see several examples demonstrating these two usages of application.properties.
Note that you do not have to tell the spring boot to load application.properties for you, as long as it is present and will be loaded, and both spring and application code can get the properties.
We've almost finished introducing the initialized project and the last thing left, let's take a look at how the spring boot application was built.
3. Create a Pom.xml file
3.1 Set Spring Boot's parent
<parent> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-parent</artifactid> <version>1.5.7.RELEASE</version> < Relativepath/> <!--lookup parent from repository to </parent>
The Spring boot project must have the parent set to spring Boot's parent, which contains a large number of default configurations.
3.2 Importing Spring boot Web support
<dependency> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-starter-web</artifactid> </dependency>
3.3 Adding the Spring boot plugin
<plugin> <groupId>org.springframework.boot</groupId> <artifactId> Spring-boot-maven-plugin</artifactid> </plugin>
After the project is created, the default Pom.xml file is generated, as follows:
<?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> <groupId>com.cloud</groupId> <artifactid>spring-boot-helloworld</ artifactid> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>s Pring-boot-helloworld</name> <description>demo Project for Spring boot</description> <parent> <groupId>org.springframework.boot</groupId> & Lt;artifactid>spring-boot-starter-parent</artifactid> <version>1.5.7.RELEASE</version> <relativePath/> <!--lookup parent from repository to </parent> <properties> <pro Ject.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding >UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties& Gt <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-starter-test</ar Tifactid><scope>test</scope> </dependency> </dependencies> <build> <plugins> ; <plugin> <groupId>org.springframework.boot</groupId> <ARTIFACTID>SPR ing-boot-maven-plugin</artifactid> </plugin> </plugins> </build></project& Gt
4.HelloWorld Practical Explanation
4.1 Controller Layer
The code for Helloworldcontroller is as follows:
Package Com.cloud;
ImportOrg.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
* Created by Peter on 2017-10-09.
*/
@RestController
Span style= "color: #000080; Font-weight:bold ">public class Helloworldcontroller {
@RequestMapping ( public String SayHello () {
return " hello,world! ";
}
}
@RestController and @requestmapping annotations are annotations from SPRINGMVC, which are not specific parts of springboot.
(1). @RestController: Provides implementation of the rest API, which can serve json,xml or other. Here, the result is rendered in string form.
(2). @RequestMapping: Provides routing information, and the HTTP request for the "/" path is mapped to the SayHello method for processing.
4.2 Starting the Application class
As described in the first paragraph, it is available out of the box. such as the following application class:
Package Com.cloud;
Org.springframework.boot.SpringApplication;
Org.springframework.boot.autoconfigure. springbootapplication;
@SpringBootApplication
public class Springboothelloworldapplication {
public static void Main (string[] args) {
Springapplication. run (springboothelloworldapplication. class, args);
}
}
(1). @SpringBootApplication: The identity of the Spring Boot app
(2). Application is very simple, a main function as the main entrance. Springapplication directs the application and passes the application itself as a parameter to the Run method. The specific run method launches the embedded Tomcat and initializes the spring environment with its spring components.
4.3 controller layer test class
A good program, not lack of good UT. UT for Helloworldcontroller is as follows:
PackageCom.cloud;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.boot.test.context.SpringBootTest;ImportOrg.springframework.test.context.junit4.SpringRunner;Import Staticorg.junit.Assert.assertEquals; the @RunWith (Springrunner.class) @SpringBootTest Public classspringboothelloworldapplicationtests {@Test Public voidTestsayhello () {assertequals ("Hello,world!",NewHelloworldcontroller (). SayHello ()); }}
4.4 Run
Compile the run code directly and then visit http://localhost:8080/to see the spring boot say hello to you on the page:
hello,world!
Spring Boot Growth Path (i) get started quickly