SpringBoot Hello World

本文首发于我的github博客

前言

SpringBoot是Spring MVC升级版,基于『约定优于配置』的原则,快速开发出web程序。

环境

本系列笔记环境如下:

  • Sun JDK1.8.0_20
  • Maven 3.3.9
  • Eclipse Mars2

编写springBoot Hello World程序

  • 创建一个Maven工程,POM.xml如下:
<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.yeyouluo.springboot</groupId>
  <artifactId>spring-boot-learn</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  	<name>Chapter1</name>
	<description>The first Spring Boot project</description>

	  <!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<!-- Add typical dependencies for a web application -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

	</dependencies>
	
	<!-- Package as an executable jar -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
  • 编写启动类Main.java
package com.yeyouluo.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 
 * @author yeyouluo
 * @date 2017-10-19 22:21:16
 */
@RestController
@EnableAutoConfiguration
public class Main {
	
	@RequestMapping("/")
	public String home(){
		return "hello,springboot!";
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SpringApplication.run(Main.class, args);
	}

}

运行

  • 方式①:在项目右键 --> run as --> Java Application
  • 方式②:在项目右键 --> run as --> Spring Boot App
  • 方式③:用Maven将项目打成jar,在控制台进入该jar所在目录,执行java -jar spring-boot-learn-0.0.1-SNAPSHOT.jar
    以上三种方式都可以启动springboot程序。启动后通过浏览器访问http:localhost:8080 即可在页面看到字样:

    hello,springboot!

要点分析

POM文件要点

  • <parent>标签:父POM为我们添加了很多默认的配置。
  • spring-boot-starter:核心模块,包括自动配置支持、日志和YAML
  • spring-boot-starter-test:测试模块,包括JUnit、Hamcrest、Mockito
  • spring-boot-starter-web:Web模块。
  • spring-boot-maven-plugin:使用该插件会帮助我们快速构建一个可执行的jar。

入口类Main.java要点

  • @RestController:当处理进来的web请求时,Spring会询问它。并且告诉Spring以字符串的形式渲染结果,并直接返回给调用者。
  • @RequestMapping:提供路由信息。它告诉Spring任何来自"/"路径的HTTP请求都应该被映射到home方法。

    @RestController和@RequestMapping注解是Spring MVC注解(它们不是Spring Boot的特定部分)。

  • @EnableAutoConfiguration:根据字面理解,允许自动配置,即告诉Spring Boot根据添加的jar依赖来配置Spring。由于spring-boot-starter-web添加了Tomcat和Spring MVC,所以auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。
  • main方法:委托SpringApplication类的run方法启动Spring,相应地启动被自动配置的Tomcat web服务器。

参考

原文地址:https://www.cnblogs.com/helloIT/p/7696241.html