Spring Boot 2.0 之 Hello World

    Spring Boot 简化了 Spring 的操作, 不需要配置就能运行 Spring 应用. Spring Boot 管理 spring 容器、第三方插件, 并提供很多默认系统级的服务. Spring Boot 通过 Starter 来提供系统级服务. 

    相比于 Spring, Spring Boot 具有以下的特点:

    ①: 实现约定大于配置,是一个低配置的应用级框架. 不像 Spring 那样需要大量的配置. Spring Boot 不需要配置或者极少配置,就能使用 Spring 大量的功能.

    ②: 提供了内置的 Tomcat 或者 Jetty 容器.

    ③: 通过依赖的 jar 包管理、自动装配技术, 容易支持与其他技术体系、工具集成.

    ④: 支持热加载, 开发体验较好. 也支持 Spring Boot 系统监控, 方便了解系统运行情况.

 

Hello Spring Boot 2.0;

POM 配置:

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

<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
</dependencies>
<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
</build>

 启动类:

package com.pangu.helloworld;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication {

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

}

 Controller:

package com.pangu.helloworld;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloWorldController {
    
    @RequestMapping("/index")
    @ResponseBody
    public String index(){
        return "你好 Spring Boot 2.0.";
    }
    
}

结果:

RESTFul 风格:

package com.pangu.helloworld;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName: HelloWorldRESTFulController
 * @Description: TODO RESTFul 架构风格
 * @author etfox
 * @date 2018年10月5日 下午8:41:37
 *
 * @Copyright: 2018 www.etfox.com Inc. All rights reserved.
 */
@RestController
public class HelloWorldRESTFulController {
    
    @RequestMapping("/rest/{id}")
    public String rest(@PathVariable String id){
        return id;
    }
    
}

结果:

 另:热部署>>>>>>>>>>>>>>

<!-- 热部署 -->
	<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
	</dependencies>

修改 pom.xml  后,此后启动项目,修改可免重启编译. 主要多了几个变化:LiveReload server 监控 Spring Boot 应用的变化,另外启动时间变为 0.5 秒, 因为他避免了重启 Spring Boot 应用, 也避免重新加载 Spring 的类, 只重新加载修改的类.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值