Spring boot 入门1-helloworld

Spring boot 入门-helloworld

1、Spring boot简介

简化spring应用开发的一个框架

整个Spring 技术栈的一个大整合

J2EE开发的一站式解决方案

2.什么是微服务

2014年martin fowler提出,它是一种架构风格

一个应用应该是一组小型服务

每一个小型服务通过http的方式进行沟通

每一个功能元素,都是一个可以独立升级的软件单元

详细参照问服务文档

 

单体应用:一个应用是所有的服务的集合

开发测试简单,水平扩展能

 

3、环境约束

jdk1.8+ 我的java version "1.8.0_191"

maven 3.x 我的3.5.3

IDEA 2018

Spring boot1.5.9+ 使用的springboot与教程一致1.5.9

 

maven 设置

<profile>
      <id>jdk-1.8</id>
​
      <activation>
                <activeByDefault>true</activeByDefault>
        <jdk>1.8</jdk>
      </activation>
            <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
            </properties>
​
    </profile>
  

4、SpringBoot Hello world

浏览器发送一个hello请求,服务器接收响应,返回hello world字符串

 

1.#创建一个maven工程

2.导入spring boot相关依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

 

3.编写一个主程序,启动springboot应用

Spring boot 入门1-helloworld

//标注一个主程序,说明这是一个springboot应用
@SpringBootApplication
public class HelloWorldMainApplication {
    public static void main(String[] args) {
        //springbooot跑起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

4,编写相关的Controller,Service

@ResponseBody
@Controller
public class HelloController {
    @RequestMapping("/hello")
    public String Hello(){
        return "hello world!";
    }
}

5.运行主程序测试

6.简化部署

打开maven 选项,双击package

Spring boot 入门1-helloworld

 

 

等待打包完成Spring boot 入门1-helloworld

 

生成的jar位置

Spring boot 入门1-helloworld

jar拷出,运行命令:Java -jar xxx.jar

Spring boot 入门1-helloworld

项目启动

截至目前为止的pom

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
​
    <groupId>com.atguigu</groupId>
    <artifactId>spring-boot-01-helloworld</artifactId>
    <version>1.0-SNAPSHOT</version>
​
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
​
    <!--这个插件可以将应用打包成一个可执行的jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
​
​
</project>