##Springboot框架的简单分享,让你入门不是难事

Springboot框架


# 一,简介:

   ~  Spring Boot来简化Spring应用开发,约定大于配置,去繁从简,just run就能创建一个独立的,产品级别的应用
  ~ 背景
    J2EE笨重的开发、繁多的配置、低下的开发效率、
    复杂的部署流程、第三方技术集成难度大。
  ~ 解决:
    “Spring全家桶”时代。
    Spring Boot  J2EE一站式解决方案
    Spring Cloud  分布式整体解决方案
  ~ 优点
     – 快速创建独立运行的Spring项目以及与主流框架集成
    – 使用嵌入式的Servlet容器,应用无需打成WAR包
    – starters自动依赖与版本控制
    – 大量的自动配置,简化开发,也可修改默认值
    – 无需配置XML,无代码生成,开箱即用
    – 准生产环境的运行时应用监控
    – 与云计算的天然集成
  ~ 微服务:

    2014,martin fowler

    微服务:架构风格(服务微化)

    一个应用应该是一组小型服务;可以通过HTTP的方式进行互通;

    单体应用:ALL IN ONE

    微服务:每一个功能元素最终都是一个可独立替换和独立升级的软件单元;

    [详细参照微服务文档](https://martinfowler.com/articles/microservices.html#MicroservicesAndSoa)

  ~  准备环境:   

    环境约束
      –jdk1.8:Spring Boot 推荐jdk1.7及以上;java version "1.8.0_112"(我们可以打开我们doc命令查看自己的jdk的版本)
      –maven3.x:maven 3.3以上版本;Apache Maven 3.5.2
      –IntelliJIDEA2017:IntelliJ IDEA 2017.2.6 x64、STS
      –SpringBoot 1.5.9.RELEASE:1.5.9;
      统一环境;

#  二,入门:

  ~ 1,找到我们的maven所在,打开maven 的settings.xml配置文件的profiles标签添加

<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>

  ~2,整合idea,进行相关设置

  ~3,浏览器发送hello请求,服务器接受请求并处理,响应Hello World字符串

package cn.kairui.springboot;

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

@SpringBootApplication
public class SpringbootDemo01ParentApplication {

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

}

  注意:我们编写一个主方法,这个就是启动springboot的入口

package cn.kairui.springboot.controller;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liurui
 * @date $ {DATE} 13:45
 */
@Component
@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String helle(){
        return "hello World!";
    }
}

  这个是我们编写的一个controller,给浏览器发送一个请求,让他回显hello World!打开我们的浏览器,输入地址

可以看到我们输入的地址不全,找不到路径报了一个404

现在可以看到页面上回显了一个我们想要的内容

#  3,看到这么多,看到我们还有自己编写主方法,编写这些注解,那么springboot简化开发,有没有能够快速的建成一个我们需要的项目呢?

  那么idea给我们提供了能够快速创建springboot项目的方法:

点击next之后,我们看到文件所在的路径,根据自己的需要进行修改就可以,然后点击next完成创建

# 四:创建的项目我们如何启动:

    这里给大家简单介绍下我们创建的springboot项目的依赖:

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

  可以看到上面这个依赖是父级,那么它其实也是依赖他的父级:spring-boot-dependencies

  在spring-boot-dependencies里面给我们封装了很多我们需要的jar包,大家可以自己去看看

  真正管理Spring Boot应用里面的所有依赖版本

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

  **spring-boot-starter**-==web==:

​   spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件;

  Spring Boot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter相关场景的所有依赖都会导入进来。要用什么功能就导入什么场景的启动器

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

  这个是springboot给我们提供的单元测试

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

  这个插件,可以将应用打包成一个可执行的jar包,将这个应用打成jar包,直接使用java -jar的命令进行执行;

@SpringBootApplication
public class HelloWorldMainApplication {

    public static void main(String[] args) {

        // Spring应用启动起来
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

@**SpringBootApplication**: Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用;

- 主程序已经生成好了,我们只需要我们自己的逻辑
- resources文件夹中目录结构
- static:保存所有的静态资源; js css images;
- templates:保存所有的模板页面;(Spring Boot默认jar包使用嵌入式的Tomcat,默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
- application.properties:Spring Boot应用的配置文件;可以修改一些默认设置;

posted @ 2019-09-22 14:34  阿锐互联网  阅读(265)  评论(0编辑  收藏  举报