创建第一个SpringBoot

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/DTJ_74/article/details/53054697

定义:
spring Boot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目。大多数SpringBoot项目只需要很少的配置文件。


特质:
spring是一个全栈的开发框架,核心是IOC,AOP。springboot是为了简化spring开发而生的。


创建第一个SpringBoot 项目:
1 安装Gradle 插件。(翻墙)
2 创建Gradle 项目,并且为web项目。

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

The @RestController and @RequestMapping annotations are Spring MVC annotations (they are not specific to Spring Boot). See the MVC section in the Spring Reference Documentation for more details.
@RestController 和@RequestMapping 都是Spring MVC里面的注解。

简单来讲就系:
@RestController

    @ResponseBody和@Controller的合集

@RequestMapping

    提供路由信息,负责URL到Controller中的具体函数的映射。

@EnableAutoConfiguration

   The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web application and setup Spring accordingly.
   这个大概意思是,SpringBoot会尝试根据你添加的jar包,帮你配置Spring应用。由于spring-boot-starter-web已经添加了Tomcat和Spring MVC,所以会springboot 会帮你自动配置。

最后一个就系主方法:
java入口,通过SpringApplication类调用run()方法,会自动配置tomcat web server的。也就是相当于我们开启了tomcat服务器一样。

启动浏览器,输入 localhost:8080.就可以看到 Hello World!

猜你喜欢

转载自blog.csdn.net/DTJ_74/article/details/53054697