Spring Boot 2.0 之 Hello World

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

    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 的类, 只重新加载修改的类.

猜你喜欢

转载自blog.csdn.net/qq_29689487/article/details/82946904