Spring Boot入门篇(基于Spring Boot 2.0系列)

it2022-05-05  137

1:概述:

Spring Boot是用来简化Spring应用的初始化开发过程。

2:特性:

创建独立的应用(jar|war形式);

需要用到spring-boot-maven-plugin插件

直接嵌入式Tomcat,Jetty,Undertow等Web容器;

提供固化的starter依赖,简化构建配置;

提供条件化自动装配Spring或者第三方组件

提供运维(Production-Ready)特性,如`指标信息(Metrics),健康检查或者外部化配置;

Spring Boot Admin(Spring Boot Web监控平台)

提倡无代码生成,并且不需要XML配置;

3:环境准备

Java运行环境: Spring5.0最低版本要求是Java8。

模块类库管理Apache Maven:SpringBoot官方兼容Maven 3.2或者更高版本。

装配IDE(基础开发环境):Idea或者Eclipse或者MyEclipse。

4:第一个Spring Boot应用

建立springboot-learn项目,并配置公共POM依赖。
   <packaging>pom</packaging>  <properties>        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>        <java.version>1.8</java.version>        <maven.compiler.source>1.8</maven.compiler.source>        <maven.compiler.target>1.8</maven.compiler.target>        <lombok.version>1.16.8</lombok.version>    </properties>​    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.0.8.RELEASE</version>        <relativePath/>    </parent>​    <dependencies>        <!--提供简单注解简化Java代码开发-->        <dependency>            <groupId>org.projectlombok</groupId>            <artifactId>lombok</artifactId>            <version>${lombok.version}</version>            <scope>provided</scope>        </dependency>    </dependencies>​    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <version>${spring-boot.version}</version>                <!--must configuration executable-->                <configuration>                    <executable>true</executable>                </configuration>                <executions>                    <execution>                        <goals>                            <goal>repackage</goal>                        </goals>                    </execution>                </executions>            </plugin>        </plugins>    </build>
建立spring-boot-helloworld项目,POM依赖
   <dependencies>        <!--spring boot web 依赖-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>
建立Spring Boot应用启动主类
@SpringBootApplicationpublic class HelloWorldApplication {​    public static void main(String[] args) {        SpringApplication.run(HelloWorldApplication.class, args);   }​​    @RestController    public class HelloController {​        @GetMapping("/hello")        public String hello() {            return "my first spring boot application.";       }   }}

启动项目,spring-boot-starter-web默认嵌入式web容器时Tomcat,默认启动端口是8080。并且

spring-boot-starter-web模块引入了spring-boot-starter-json模块,可以使用Restful

风格API设计。

打开浏览器,访问http://localhost:8080/hello 。效果不在演示。

 

 

转载于:https://www.cnblogs.com/liuenyuan1996/p/11026651.html


最新回复(0)