怎么创建SpringBoot 项目 (Spring Boot菜鸟教学一)

1.  Spring Boot

1.1. 什么是Spring Boot

1.2. Spring Boot的优缺点

2.  实现我的第一个HelloWorld

2.1. 新建一个maven  web项目




<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>springboot</groupId>
  <artifactId>com.springboot.dome</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>com.springboot.dome Maven Webapp</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>
	</parent>
  <dependencies>
  	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
   </dependencies>
   <build>
    <finalName>com.springboot.dome</finalName>   
  </build>
</project>

2.1.1设置spring boot的parent


<parent>
	<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
</parent>

说明:Spring boot的项目必须要将parent设置为spring boot的parent,该parent包含了大量默认的配置,大大简化了我们的开发。

2.1.2导入spring boot的web支持

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

2.2. 编写第一个Spring Boot的应用Appliction

新建目录:格式如下(注意Apppliction类与controller包同级)


package demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Appliction {
	public static void main(String[] args) {
		SpringApplication.run(Appliction.class, args);
	}
}

2.3. 编写一个SpringBootController

package demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class SpringBootController {
	@RequestMapping("/hello")
	@ResponseBody
	public  String  index(){
		return  "HelloWorld";
	}	
}

注解说明:

1、@SpringBootApplication:Spring Boot项目的核心注解,主要目的是开启自动配置,以及会自动扫描同级下的目录或子级下的目录.

2、@Controller:标明这是一个SpringMVC的Controller控制器.

3、main方法:在main方法中启动一个应用,即:这个应用的入口.

2.4. 启动应用

在Spring Boot项目中,直接run Java Application

启动Appliction的main方法:

启动效果:


看到如下信息就说明启动成功了:

INFO 6188 --- [           main]c.i.springboot.demo.HelloApplication    : Started HelloApplication in 3.281 seconds (JVM running for 3.601)

2.5.测试

打开浏览器,输入地址:http://本机IP:8080/hello

效果:

 



猜你喜欢

转载自blog.csdn.net/sou_time/article/details/79641438