前言
上一片文章介绍了Spring Boot的基本概念,并通过官方的网站建立了第一个Spring Boot的项目。本文针对Spring Boot最简项目进行介绍,并启动运行。好了,开始新的一文的介绍。
项目结构
本文介绍的是最简单的Spring Boot 项目。主要是由启动类、配置文件和依赖所组成。这三部分组成Spring Boot项目的基本配置,集成业务功能代码之后,就组成一个完整的项目了。
@SpringBootApplication
@SpringBootApplication注解包含:@Target、@Retention、@Documented、@Inherited、@SpringBootConfiguration、@EnableAutoConfiguration、@ComponentScan。SpringBootApplication是springboot的基本注解,是写在springboot的启动类上的注解,目的是开启spring Boot 的自动配置。 其中:
- @ComponentScan:可以配置多个需要扫描的包
- @Target:用于设定注解使用范围。
- @Retention:被它所注解的注解保留多久。
- @Documented:在自定义注解的时候可以使用@Documented来进行标注,如果使用@Documented标注了,在生成javadoc的时候就会把@Documented注解给显示出来。
- @EnableAutoConfiguration:开启自动配置。
- @SpringBootConfiguration:指示此类提供了应用程序配置。
- @SpringBootApplication的目的只是为了简化,让开发人员少写代码,实现相同的目标,这也算Java封装思想的提现。
POM依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
spring-boot-starter-web:依赖启动器的主要作用是提供Web开发场景所需的底层所有依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> 复制代码
spring-boot-starter-test:进行单元测试的相关依赖。
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency>
commons-lang3:集成的相关工具类。
配置文件
配置文件application.properties,为了演示项目,本文仅配置一个简单的服务端口。在application.properties中可以配置更多的服务的相关参数,包含:SQL、MQ、Redis、项目名称等等。
项目原码
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 https://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.3.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>simple</artifactId> <version>0.0.1-SNAPSHOT</version> <name>simple</name> <description>Demo project for Spring Boot and MyBatis and Swagger</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!-- commons start --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.7</version> </dependency> <!-- commons end --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
测试Controller
package com.example.demo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; /** * @ClassName TestController * @Description: 测试接口 * @Author JavaZhan @公众号:Java全栈架构师 * @Date 2020/6/13 * @Version V1.0 **/ @Controller @RequestMapping("/test") public class TestController { @RequestMapping("hello") @ResponseBody public String getTest(){ return "Hello World ,This is Test"; } }
启动类DemoSimpleApplication
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @MethodName: DemoSimpleApplication * @Description: 启动类 * @Author: JavaZhan @公众号:Java全栈架构师 * @Date: 2020/6/13 **/ @SpringBootApplication public class DemoSimpleApplication { /** * @MethodName: main * @Description: * @param args * @Return: void * @Author: JavaZhan @公众号:Java全栈架构师 * @Date: 2020/6/13 **/ public static void main(String[] args) { SpringApplication.run(DemoSimpleApplication.class, args); } }
配置文件
配置文件application.properties,为了演示项目,本文仅配置一个简单的服务端口。
server.port=8888
结语
本次基于Spring Boot建立的第一个项目就完成了,执行DemoSimpleApplication类,在浏览器中输入http://localhost:8888/test/hello 就会返回正常的“Hello World ,This is Test”信息。好了,本次第一个Spring Boot项目启动啦!