1.springboot:入门程序

时间:2023-03-09 14:43:30
1.springboot:入门程序

一、Spring Boot 简介

官网英文:

Spring Boot makes it easy to create stand-alone, production-grade Spring based Applications that you can “just run”.
We take an opinionated view of the Spring platform and third-party libraries so you can get started with minimum fuss.
Most Spring Boot applications need very little Spring configuration.

官网中文说法:

Spring引导使您可以轻松地创建独立的、生产级的基于Spring的应用程序,您可以“直接运行”。
我们对Spring平台和第三方库采取了一种固执己见的观点,这样您就可以开始使用最小的忙乱。大多数Spring启动应用程序都需要非常少的Spring配置。
简化Spring应用开发的一个框架;
整个Spring技术栈的一个大整合;
J2EE开发的一站式解决方案;
二、微服务
2014,martin fowler
微服务 : 架构风格(服务微化)
一个应用应该是一组小型服务;可以通过HTTP的方式进行互通;
单体应用 : ALL IN ONE
微服务 : 每一个功能元素最终都是一个可独立替换和独立升级的软件单元;

二、环境准备

  • jdk1.8:Spring Boot 推荐jdk1.7及以上;
  • maven3.x:maven 3.3以上版本;
  • IntelliJIDEA2017:IntelliJ IDEA 2017.2.2 x64、STS
  • SpringBoot 1.5.9.RELEASE:1.5.9
  • 统一环境;
三、MAVEN设置
1. 给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设置 整合maven进来;

1.springboot:入门程序

1.springboot:入门程序

四、Spring Boot HelloWorld程序

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

  1、创建一个maven工程;(jar)

  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、编写一个主程序;启动Spring Boot应用

/**
* @author 超轶绝尘
*/
@SpringBootApplication
public class SpringBootDemoApplication { public static void main(String[] args) {
SpringApplication.run(HelloWorldMainApplication.class, args);
}
}

4、编写相关的Controller、Service

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

5、运行主程序测试

6、简化部署

<!‐‐ 这个插件,可以将应用打包成一个可执行的jar包;‐‐>
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring‐boot‐maven‐plugin</artifactId>
    </plugin>
  </plugins>
</build>

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

五、自动部署与刷新


自动部署

为了让开发更加得心应手,我们进一步优化IDE,使得Web应用可以自动重启,页面内容修改时网页可以自动刷新,这可以极大地提高我们的开发体验和开发效率。

为了让Spring Boot在需要重启时,能够自动进行,我们只需要

1、在POM文件中增加以下依赖。

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

2.1、设置Intellij IDEA

  Settings->Build,Execution,Deployment->Compiler->Build project automatically

1.springboot:入门程序

注意:这里有两个选择,Default Settings是全部项目默认的,假如你之前已经添加了项目,那么设置完这个后当前项目是不会生效的,那么此时需要设置当前项目的Settings,也是同样的设置路径。

2.2

Linux/Windows:【Ctrl】+【Alt】+【Shift】+【/】

Mac:【Command】+【Alt】+【Shift】+【/】

1.springboot:入门程序

1.springboot:入门程序