Chapter 1 first spring boot program, springboot

Source: Internet
Author: User

Chapter 1 first spring boot program, springboot

Environment:

  • Jdk: 1.8.0 _ 73
  • Maven: 3.3.9
  • Spring-boot: 1.2.5.RELEASE (specified in pom. xml)

Note: For spring-boot support,

  • Use jdk7 at least (jdk6 is also acceptable, and additional configuration may be required. I have not tried it. jdk8 is officially recommended)
  • Use maven at least 3.2

1. First enter a folder, such "~ /Desktop/project ", and then create a maven project by hand according to" Chapter 1 Enterprise project Development-maven + springmvc + spring + mybatis + velocity integration. xml is as follows:

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 3 xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4 5 <modelVersion> 4.0.0 </modelVersion> 6 7 <groupId> com. xxx </groupId> 8 <artifactId> myboot </artifactId> 9 <version> 1.0-SNAPSHOT </version> 10 11 <! -- 12 introducing spring-boot-starter-parent for parent is the best method, but sometimes we may introduce our own parent. There are two solutions: 14 1) our own parent pom. xml parent is set to spring-boot-starter-parent (not verified, but it feels feasible) 15 2) use the methods in the springboot document: see spring-boot-1.2.5-referencecepage 16 --> 17 <parent> 18 <groupId> org. springframework. boot </groupId> 19 <artifactId> spring-boot-starter-parent </artifactId> 20 <version> 1.2.5.RELEASE </version> 21 </parent> 22 23 <! -- Introduce Actual dependency --> 24 <dependencies> 25 <dependency> 26 <groupId> org. springframework. boot </groupId> 27 <artifactId> spring-boot-starter-web </artifactId> 28 </dependency> 29 </dependencies> 30 31 <build> 32 <plugins> 33 <! -- 34 is used to compress an application into a jar that can be run directly (this jar is used in the production environment) 35 it is worth noting that if spring-boot-starter-parent is not referenced as a parent, 36. The second method is adopted, and corresponding changes 37 --> 38 <plugin> 39 <groupId> org. springframework. boot </groupId> 40 <artifactId> spring-boot-maven-plugin </artifactId> 41 </plugin> 42 </plugins> 43 </build> 44 </project>View Code

Then, use "mvn clean compile" to introduce eclipse. For details, see Chapter 1 Enterprise Project Development-maven + springmvc + spring + mybatis + velocity integration.

 

2. Introduce the project structure in eclipse as follows:

Then, create the red box section.

FirstBoot:

1 package com. xxx. firstboot; 2 3 import org. springframework. boot. springApplication; 4 import org. springframework. boot. autoconfigure. enableAutoConfiguration; 5 import org. springframework. web. bind. annotation. requestMapping; 6 import org. springframework. web. bind. annotation. restController; 7 8/** 9 * @ RestController: Annotation of spring mvcd. 10 * is equivalent to the combination of @ Controller and @ ResponseBody. json11 * 12 * @ EnableAutoConf can be returned directly. Iguration: annotation 13 * of spring boot is a key part of boot without configuration 14 */15 @ RestController16 @ EnableAutoConfiguration17 public class FirstBoot {18 19 @ RequestMapping ("/hello ") 20 public String testFirstMethod () {21 return "Hello jigang !!! "; 22} 23 24/** 25 * spring boot entry. In the entire project, including its sub-projects, 26 * can only have one main method, otherwise, spring boot cannot start up. 27 */28 public static void main (String [] args) {29 SpringApplication. run (FirstBoot. class, args); 30} 31 32}View Code

Explanation:

  • @ RestController: Annotation of spring mvc, equivalent to =>@ Controller + @ ResponseBody + enable json converter annotation, which can directly return json
  • @ EnableAutoConfiguration: Annotation of spring boot, which is a key part of startup without xml configuration

  • Main method: spring boot entry. In the entire project, including its sub-projects, there can only be one main method. Otherwise, spring boot cannot be started.
    • Note: The first parameter of the run method does not apply to full-class names. We recommend that you use full-class names.

 

3. Start spring boot

Start for the first time, use "mvn spring-boot: run" (something will be downloaded), and then start directly in the main class (class containing the main method) run as --> java application"

When the following information is displayed on the console, it indicates that spring-boot is successfully started,

Enter the path "http: // localhost: 8080/hello" in the browser and output the message "Hello jigang !!! ", Then success !!!

Note:

When we enter a path to access the resource, the console outputs the following information:

That is to say, springMVC's dispatcherServlet is initialized at this time.

 

4. package it into an executable jar, which will be available in the production environment

The process of producing jar depends on the introduced spring-boot-maven-plugin.jar.

Run "mvn package" to view the files under the target:

The package on the back is from maven. The package on the front is spring-boot and repackage on the back)

Note: if you do not use spring-boot-starter-parent for parent (see the pom. xml annotation), you need to adjust the packaging process accordingly.

 

5. Run the packaged jar

Go to the myboot folder and run the "java-jar target/myboot-1.0-SNAPSHOT.jar" command to get the following results:

Then access the browser.

 

 

Note:

View the dependency tree before introducing spring-boot-starter-web.jar:

After introducing the spring-boot-starter-web.jar, view the dependency tree:

Actually, the spring-boot-starter-web.jar jar introduces 5 aspects of jar:

  • Spring-boot startup jar: spring-boot-starter.jar
  • Tomcat jar (spring-boot-1.2.5 --> tomcat 8)
  • Jackson
  • Hiberbate-validator
  • Jar related to spring (spring-boot-1.2.5 --> spring4.1)

 

Two problems:

1) if you do not use spring-boot-starter-parent for parent, how can you configure it?

Solution:

Add the following code.

1 <dependencyManagement> 2 <dependencies> 3 <dependency> 4 <! -- Import dependency management from Spring Boot --> 5 <groupId> org. springframework. boot </groupId> 6 <artifactId> spring-boot-dependencies </artifactId> 7 <version> 1.2.5.RELEASE </version> 8 <type> pom </type> 9 <scope> import </scope> 10 </dependency> 11 </dependencies> 12 </dependencyManagement>View Code

Complete pom. xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" 3 xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4 5 <modelVersion> 4.0.0 </modelVersion> 6 7 <groupId> com. xxx </groupId> 8 <artifactId> myboot </artifactId> 9 <version> 1.0-SNAPSHOT </version> 10 11 <! -- Introducing spring-boot-starter-parent for parent is the best method, but sometimes we may introduce our own parent. There are two solutions: 12 1) our own parent pom. set the xml parent to spring-boot-starter-parent (not verified, but feasible). 2) use the method in the springboot document: see page 13th of spring-boot-1.2.5-reference.pdf --> 13 <! -- <Parent> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-parent </artifactId> 14 <version> 1.2.5.RELEASE </version> </parent> --> 15 16 <dependencyManagement> 17 <dependencies> 18 <dependency> 19 <! -- Import dependency management from Spring Boot --> 20 <groupId> org. springframework. boot </groupId> 21 <artifactId> spring-boot-dependencies </artifactId> 22 <version> 1.2.5.RELEASE </version> 23 <type> pom </type> 24 <scope> import </scope> 25 </dependency> 26 </dependencies> 27 </dependencyManagement> 28 29 <! -- Introduce Actual dependency --> 30 <dependencies> 31 <dependency> 32 <groupId> org. springframework. boot </groupId> 33 <artifactId> spring-boot-starter-web </artifactId> 34 </dependency> 35 </dependencies> 36 37 <build> 38 <plugins> 39 <! -- Used to compress an application into a jar that can be run directly (this jar is used in the production environment). It is worth noting that if spring-boot-starter-parent is not referenced for parent, 40. The second method is used, and the corresponding changes must be made here --> 41 <plugin> 42 <groupId> org. springframework. boot </groupId> 43 <artifactId> spring-boot-maven-plugin </artifactId> 44 </plugin> 45 </plugins> 46 </build> 47 </project>View Code

Note: introduced in dependencyManagement, spring-boot-dependencies type = pom, scope = import

2) if you do not use spring-boot-starter-parent for parent, how to configure the packaging plug-in spring-boot-maven-plugin?

The above configuration is used here, and I can directly execute "mvn package". What are the explanations here? Different from official documents

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.