springboot介绍及简单使用
1.什么是Springboot
<springboot就是javaweb的开发框架,和springMVC类似,好处就是简化开发:约定大于配置。能迅速开发web应用。
<微服务: <微服务是一种架构风格,它要求我们在开发一个应用的时候,这个应用必须构建成一系列小服务的组合,可以通过http的方式进行互通。 Martin Flower 于 2014 年 3 月 25 日写的《Microservices》,详细的阐述了什么是微服务。 原文地址:http://martinfowler.com/articles/microservices.html 翻译:https://www.cnblogs.com/liuning8023/p/4493156.html <单体应用架构: <单体应用架构是指,我们将一个应用中的所有应用服务都封装在一个应用中。 <无论是ERP、CRM或是其他什么系统,都把数据库访问,web访问,等等各个功能放在一个wer包内。 <单体应用架构的好处:易于开发和测试;也十分方便部署;当需要扩展时,只需要将war复制多份,然后放到多个服务器上,再做个负载均衡就可以了。 <单体应用架构的缺点:哪怕我要修改一个非常小的地方,我都需要停掉整个服务,重新打包、部署这个应用war包。特别是对于一个大型应用,我们不可能吧所有内容都放在一个应用里面,我们如何维护、如何分工合作都是问题。
spring为我们带来了构建大型分布式微服务的全套、全程产品: 构建一个个功能独立的微服务应用单元,可以使用springboot,可以帮我们快速构建一个应用; 大型分布式网络服务的调用,这部分由spring cloud来完成,实现分布式; 在分布式中间,进行流式数据计算、批处理,我们有spring cloud data flow。 spring为我们想清楚了整个从开始构建应用到大型分布式应用全流程方案。
2.springboot程序Hello World
环境配置:
jdk1.8 、 maven3.x 、SpringBoot1.5.9RELEASE 、idea开发
依赖配置
- 导入父工程坐标
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> </parent>
如果我们想要修改SpringBoot项目的JDK版本,只需要简单的添加以下属性即可,如果没有需求,则不添加。
<properties> <java.version>1.8</java.version> </properties>
- 添加相应启动器
web启动器:集成了Tomcat与web的相关配置
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
单元测试依赖
<!--单元测试--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency>
打包发布必须加的依赖
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
若有依赖需要降版本,也需要指定版本号
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!--parent:继承spring-boot-starter-parent的依赖管理,控制版本与打包等内容--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <!--项目元数据信息:创建时候输入的Project Metadata部分,也就是Maven项目的基本元素,包括:groupId、artifactId、version、name、description等--> <groupId>com.kuang</groupId> <artifactId>springboot-01-helloworld</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-01-helloworld</name> <description>springboot-01-helloworld</description> <properties> <java.version>1.8</java.version> </properties> <!--dependencies:项目具体依赖,这里包含了spring-boot-starter-web用于实现HTTP接口(该依赖中包含了Spring MVC),官网对它的描述是:使用Spring MVC构建Web(包括RESTful)应用程序的入门者,使用Tomcat作为默认嵌入式容器。--> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--spring-boot-starter-test用于编写单元测试的依赖包。--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <!--build:构建配置部分。默认使用了spring-boot-maven-plugin,配合spring-boot-starter-parent就可以把Spring Boot应用打包成JAR来直接运行。--> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
程序配置
1.编写启动类
注解@SpringBootApplication表明是启动类
2.编写main函数:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); // args一定要写,否则报无法加载主类 } }
在与启动类同级目录下建包:pojo、controller、service、mapper 。一定要在同级目录下,否则识别不到
3.编写Controller
@RestController public class HelloController { @GetMapping("hello") public String hello(){ return "hello, spring boot!"; } }
将这个应用打成jar包,直接使用java -jar
的命令进行执行;
本文来自博客园,作者:Lz_蚂蚱,转载请注明原文链接:https://www.cnblogs.com/leizia/p/16686797.html
分类:
SpringBoot1.5版本
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步