SpringBoot是Spring新出的一个框架,他的目的一如始初- 简化开发。我们开发项目的时候,为了让项目运行起来,我们要考虑很多架构、配置、依赖等问题,这些问题其实每个项目都要考虑,而且每个项目的开发都有固定的模版,这些重复的工作是每个项目的样板代码,SpringBoot做的就是帮我们完成这些重复行的工作,让我们只关注业务逻辑。
主要帮我们完成了以下几个部分:
1、自动装配:针对Spring应用程序常见的应用功能,SpringBoot自动提供相关的配置。
2、起步依赖:告诉SpringBoot需要什么功能后,他能帮我们引入需要的jar包。
3、命令行界面:这个这里不介绍。
4、Actuator:监控观察你的程序运行。
接下来,我们创建一个简单的SpringBoot应用:
一、创建骨架
我们要使用Spring Initializr 来生成一个项目框架,可以通过sts、idea、SpringBoot CLI和web界面来完成Spring Initializr 的工作,我们这里选用的是web界面,访问网址:http://start.spring.io/
根据你的情况,填写表单,点击generate Project 按钮,生成项目框demo.zip并下载到本地。这个框架除了业务代码,该有的都有了。将demo.zip复制到sts工作空间,解压后,删除多余的代码,只留下src文件夹和pom.xml文件,在sts中通过引入maven项目的方式引入demo项目。
pom.xml报错,原因是spring-boot-starter-parent默认用的是2.1.3,这里有的jar我的私服上下载不到,改成低点的版本2.0.2问题就解决了。如果你连接的是中央仓库,且网络通畅,是不会有这个问题的。
我们在Spring initializr中选择了web和mysql,给我们生成的pom.xml如下:
<?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>