创建简单的Spring Boot应用,可以直接创建Maven项目,拉入对应的依赖包即可。
下面展示Spring Boot 1.5.x版本的简单Demo代码:
1. 创建Maven项目
一路Next,填写相关信息即可。

2. 完成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>
<groupId>com.parallelline1996</groupId>
<artifactId>SpringBoot-Starter-Demo</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 引入Spring Boot父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.22.RELEASE</version>
<relativePath/>
</parent>
<!-- 集成简单Web项目的相关依赖 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- spring boot maven plugin -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. 启动引导类
启动类代码:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Starter {
public static void main(String[] args) {
SpringApplication.run(Starter.class, args);
}
}
图示:

这里建议将启动引导类放在其他包的外层,因此启动引导类默认的扫描范围是其所在的包以及所在包下的子包,如果,防止情况为:

启动类是没有办法扫描到MainController.java类,除非在启动的时候,指定扫描的范围:

建议还是直接放在其他包的最外层,不用去特意申明。
demo代码的github链接:
https://github.com/Parallelline1996/Daily-Learning/tree/master/SpringBoot/code-repository/SpringBoot-Starter-Demo
本文介绍了如何创建一个简单的Spring Boot应用,通过创建Maven项目,配置pom.xml,然后编写启动引导类。文章建议将启动类置于其他包的外层,以确保其扫描到所有需要的组件,并提供了相关代码示例和GitHub链接。

1081

被折叠的 条评论
为什么被折叠?



