Spring Boot [Hello World]

导读:


经过上篇文章, 咱们已经了解到了 Spring Boot 做为一个Spring的脚手架, 其核心思想即是约定大于配置,经过一层层的封装让咱们能够在最短的时间内搭建一个web项目,从繁琐的配置中走出来更加关注业务代码。这篇文章便以一个简单的Hello World 为例 带你走进spring Boot 的世。html

快速上手:


系统要求:
尽管你能够在Java6或Java7环境下使用Spring Boot,一般咱们建议你若是可能的话就使用Java8。
Spring Boot 默认使用了内嵌容器 支持开箱即用 。
下面让咱们快速搭建一个最简单的Spring Boot 项目:
第一步:
建立一个maven项目
第二步:
引入pom配置:java

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

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

<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</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

第三步:
建立一个类web

@RestController
@SpringBootApplication
public class Application {

    @GetMapping
    public String hello(){
        return "Hello World";
    }

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

第四步:
运行这个main函数spring

查看控制台打印内容:json

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.2.RELEASE)
 
 2017-03-04 15:00:19.379  INFO 6468 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

能够看到一个很是简单的Spring Boot 项目已经运行起来了, 查看打印信息会发现其运行在8080端口上,而后打开网址http://localhost:8080/ ‘Hello World’已经出如今浏览器窗口上。浏览器

Hello World

结语:


Spring Boot 帮助咱们作了大量的默认配置,使咱们没必要太多去关注这些细节,在下一篇文章中我会记录一下关于自定义配置的内容,Spring Boot 的配置文件 帮助咱们快速开发的同时而不牺牲灵活性。app

备注:


关于第三步注解的一些描述:maven

  • @RestController:该注解是spring 4.0引入的。查看源码可知其包含了 @Controller 和 @ResponseBody 注解。咱们能够将其看作对@Controller注解的加强与细分,经常使用来返回json格式的数据。ide

  • @SpringBootApplication:该注解是Spring Boot 的自定义注解, 查看其源码会发现其包含了@Configuration
    @EnableAutoConfiguration @ComponentScan 这三个注解,其做用等价于:同时修饰了这三个注解,为了方便理解这里列举个简单的例子(这两个代码片断效果相同):函数

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

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

}
@SpringBootApplication
public class Application {

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

}
  • @GetMapping:查看其源码发现其被@RequestMapping注解修饰, 其做用等价与 RequestMapping(method =RequestMethod.GET)。

参考资料:


Spring Boot文档
Spring Boot快速入门
Spring Boot Controller

相关文章
相关标签/搜索