Spring Initializr快速启动Spring Boot应用!
Spring Initializr http://start.spring.io/是引导Spring Boot项目的绝佳工具。
它允许您从非常简单的UI创建各种基于Spring Boot的应用程序。您可以引导的一些类型的应用程序是:
- Web应用程序
- Restfu的应用程序
- Batch批量处理
Spring Boot提供了大量的入门项目。Spring Initializr支持所有这些以及更多。各种初级项目和支持的选项包括:
- spring-boot-starter-web-services:用于构建公开SOAP Web服务的应用程序
- spring-boot-starter-web - 构建Web应用程序和RESTful应用程序
- spring-boot-starter-test - 编写出色的单元和集成测试
- spring-boot-starter-jdbc - 传统的JDBC应用程序
- spring-boot-starter-hateoas - 通过添加HATEOAS功能使您的服务更加RESTful
- spring-boot-starter-security - 使用Spring Security进行身份验证和授权
- spring-boot-starter-data-jpa - 带有Hibernate的Spring Data JPA
- spring-boot-starter-cache - 启用Spring Framework的缓存支持
- spring-boot-starter-data-rest - 使用Spring Data REST公开简单REST服务
在本指南中,我们考虑使用Spring Initializr创建一个简单的Web应用程序。
使用Spring Initializr创建Web应用程序非常简单。
如上图所示,必须执行以下步骤
- 启动Spring Initializr http://start.spring.io/并选择以下内容
- 输入你的组名:com.xxx.springboot组名
- 选择xxx-services为脚手架Artifact
- 选择以下依赖项
- Web
- 如果希望使用数据库JPA和 Mysql等
- 单击页面底部的“生成项目”按钮。
- 将项目导入Eclipse或IDea,Idea内部已经集成了这个导航功能。
屏幕截图显示了导入的maven项目的项目结构。
- DemoApplication.java - Spring Boot Launcher。初始化Spring Boot自动配置和Spring应用程序上下文。
- application.properties - 应用程序配置文件。
- DemoApplicationTests.java - 用于单元测试的简单启动器。
- pom.xml - 包含Spring Boot Starter Web的依赖项。使用Spring Boot Starter Parent作为父pom。
让我们看一下生成的每个文件
/pom.xml
在pom.xml中配置的三个重要事项。
- Spring Boot Parent Pom - 您可以在这里阅读有关Spring Boot Starter Parent的更多信息 。
- Spring Boot Starter Web - 您可以在此处阅读有关Spring Boot Starter Web的更多信息
- Spring Boot Starter插件
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
代码:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@SpringBootApplication - 初始化Spring Boot自动配置和Spring应用程序上下文。详细见这里SpringApplication.run - 启动Spring Boot应用程序的静态方法。
当您将DemoApplication.java作为Java应用程序运行时,将生成以下日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.8.RELEASE)
2018-09-19 14:32:29.277 INFO 17776 --- [ main] com.example.demo.DemoApplication : Starting DemoApplication on banqjdon with PID 17776 (D:\temp\demo\target\classes started by banq in D:\temp\demo)
2018-09-19 14:32:29.279 INFO 17776 --- [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
2018-09-19 14:32:29.331 INFO 17776 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2357d90a: startup date [Wed Sep 19 14:32:29 CST 2018]; root of context hierarchy
恭喜,Web应用已经在http://localhost:8080可启动了。当然如果需要可访问,还需要配置REST端口。
Spring Boot