1.开启Spring Boot

本文详细介绍如何使用SpringBoot简化应用搭建,通过start.spring.io构建项目,配置Maven依赖,实现web功能,以及项目打包和发布流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

开启Spring Boot

Spring Boot是在Spring框架上创建的一个全新的框架,其设计目的是简化Spring应用的搭建和开发过程。开启Spring Boot有许多种方法可供选择,这里介绍使用http://start.spring.io/来构建一个简单的Spring Boot项目。

生成项目文件

访问http://start.spring.io/,页面显示如下:
在这里插入图片描述

这里选择以Maven构建,语言选择Java,Spring Boot版本为2.1.7。然后点击Options,可看到更多的配置以及依赖选择:
在这里插入图片描述

在项目信息里选择以jar包的方式部署,Java版本为8。在页面中点击Dependencies还可以选择诸多的依赖,这里仅选择web进行演示:
在这里插入图片描述
最后点击页面的generate project按钮生成项目文件。文件下载后是一个压缩包,进行解压然后使用IDEA以Maven项目的形式导入。导入后IDEA会自动编译项目并下载相应的依赖,项目目录如下所示:
在这里插入图片描述

简单演示

项目根目录下生成了一个artifactId+Application命名规则的入口类,为了演示简单,不再新建控制器,直接在入口类中编写代码:

	package com.springboot.demo;
	import org.springframework.boot.SpringApplication;
	import org.springframework.boot.autoconfigure.SpringBootApplication;
	import org.springframework.web.bind.annotation.RequestMapping;
	import org.springframework.web.bind.annotation.RestController;

	@RestController
	@SpringBootApplication
	public class DemoApplication {
	
	    @RequestMapping("/")
	    String index() {
	        return "hello spring boot";
	    }
	    
	    public static void main(String[] args) {
	        SpringApplication.run(DemoApplication.class, args);
	    }
	}

然后右键点击DemoAppliction,选择run :
在这里插入图片描述
访问http://localhost:8080,页面显示如下:
在这里插入图片描述

打包发布

在IDEA中点击右侧MAVEN项目,选择package如下图所示:
在这里插入图片描述
点击绿色的执行按钮就将项目打包成jar包(初次打包会自动下载一些依赖)。打包完毕后可看到项目目录target文件夹下生成了一个jar文件:
在这里插入图片描述
生成jar包后,cd到target目录下,执行以下命令:

 java -jar start-spring-boot-0.0.1-SNAPSHOT.jar

在这里插入图片描述

访问http://localhost:8080,效果与从IDEA中启动一致。

关于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.springboot</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.1.7.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>
    </project>
  • spring-boot-starter-parent

spring-boot-starter-parent指定了当前项目为一个Spring Boot项目,它提供了诸多的默认Maven依赖,具体可查看目录X:\springboot2.1.7\spring-boot-dependencies\2.1.7.RELEASE下的spring-boot-dependencies-2.1.7.RELEASE.pom文件,这里仅截取一小部分:

需要说明的是,并非所有在标签中配置了版本号的依赖都有被启用,其启用与否取决于您是否配置了相应的starter。比如tomcat这个依赖就是spring-boot-starter-web的传递性依赖(下面将会描述到)。

当然,我们可以手动改变这些依赖的版本。比如我想把thymeleaf的版本改为3.0.11.RELEASE,我们可以在pom.xml中进行如下配置:

	<properties>
	   <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
	</properties>
  • spring-boot-starter-web

Spring Boot提供了许多开箱即用的依赖模块,这些模块都是以spring-boot-starter-XX命名的。比如要开启Spring Boot的web功能,只需要在pom.xml中配置spring-boot-starter-web即可:

<dependency>
	  <groupId>org.springframework.boot</groupId>
	  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

PS:通常第三方的开箱可用依赖模块starter命名格式为XX-spring-boot-starter 。

因为其依赖于spring-boot-starter-parent,所以这里可以不用配置version。保存后Maven会自动帮我们下载spring-boot-starter-web模块所包含的jar文件。

如果需要具体查看spring-boot-starter-web包含了哪些依赖,可以在Excute Maven Goal对话框的Command line中输入 dependency:tree,点击Execute按钮,控制台查看到如下信息:
在这里插入图片描述

		[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.1.7.RELEASE:compile
		[INFO] |  +- org.springframework.boot:spring-boot-starter:jar:2.1.7.RELEASE:compile
		[INFO] |  |  +- org.springframework.boot:spring-boot:jar:2.1.7.RELEASE:compile
		[INFO] |  |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.1.7.RELEASE:compile
		[INFO] |  |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.1.7.RELEASE:compile
		[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.2.3:compile
		[INFO] |  |  |  |  \- ch.qos.logback:logback-core:jar:1.2.3:compile
		[INFO] |  |  |  +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.11.2:compile
		[INFO] |  |  |  |  \- org.apache.logging.log4j:log4j-api:jar:2.11.2:compile
		[INFO] |  |  |  \- org.slf4j:jul-to-slf4j:jar:1.7.26:compile
		[INFO] |  |  +- javax.annotation:javax.annotation-api:jar:1.3.2:compile
		[INFO] |  |  \- org.yaml:snakeyaml:jar:1.23:runtime
		[INFO] |  +- org.springframework.boot:spring-boot-starter-json:jar:2.1.7.RELEASE:compile
		[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.9:compile
		[INFO] |  |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0:compile
		[INFO] |  |  |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.9:compile
		[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.9.9:compile
		[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.9.9:compile
		[INFO] |  |  \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.9.9:compile
		[INFO] |  +- org.hibernate.validator:hibernate-validator:jar:6.0.17.Final:compile
		[INFO] |  |  +- javax.validation:validation-api:jar:2.0.1.Final:compile
		[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile
		[INFO] |  |  \- com.fasterxml:classmate:jar:1.4.0:compile
		[INFO] |  +- org.springframework:spring-web:jar:5.1.9.RELEASE:compile
		[INFO] |  |  \- org.springframework:spring-beans:jar:5.1.9.RELEASE:compile
		[INFO] |  \- org.springframework:spring-webmvc:jar:5.1.9.RELEASE:compile
		[INFO] |     +- org.springframework:spring-aop:jar:5.1.9.RELEASE:compile
		[INFO] |     +- org.springframework:spring-context:jar:5.1.9.RELEASE:compile
		[INFO] |     \- org.springframework:spring-expression:jar:5.1.9.RELEASE:compile
		[INFO] +- org.springframework.boot:spring-boot-starter-jetty:jar:2.1.7.RELEASE:compile
		[INFO] |  +- org.eclipse.jetty:jetty-servlets:jar:9.4.19.v20190610:compile
		[INFO] |  |  +- org.eclipse.jetty:jetty-continuation:jar:9.4.19.v20190610:compile
		[INFO] |  |  +- org.eclipse.jetty:jetty-http:jar:9.4.19.v20190610:compile
		[INFO] |  |  +- org.eclipse.jetty:jetty-util:jar:9.4.19.v20190610:compile
		[INFO] |  |  \- org.eclipse.jetty:jetty-io:jar:9.4.19.v20190610:compile
		[INFO] |  +- org.eclipse.jetty:jetty-webapp:jar:9.4.19.v20190610:compile
		[INFO] |  |  +- org.eclipse.jetty:jetty-xml:jar:9.4.19.v20190610:compile
		[INFO] |  |  \- org.eclipse.jetty:jetty-servlet:jar:9.4.19.v20190610:compile
		[INFO] |  |     \- org.eclipse.jetty:jetty-security:jar:9.4.19.v20190610:compile
		[INFO] |  |        \- org.eclipse.jetty:jetty-server:jar:9.4.19.v20190610:compile
		[INFO] |  +- org.eclipse.jetty.websocket:websocket-server:jar:9.4.19.v20190610:compile
		[INFO] |  |  +- org.eclipse.jetty.websocket:websocket-common:jar:9.4.19.v20190610:compile
		[INFO] |  |  |  \- org.eclipse.jetty.websocket:websocket-api:jar:9.4.19.v20190610:compile
		[INFO] |  |  +- org.eclipse.jetty.websocket:websocket-client:jar:9.4.19.v20190610:compile
		[INFO] |  |  |  \- org.eclipse.jetty:jetty-client:jar:9.4.19.v20190610:compile
		[INFO] |  |  \- org.eclipse.jetty.websocket:websocket-servlet:jar:9.4.19.v20190610:compile
		[INFO] |  |     \- javax.servlet:javax.servlet-api:jar:4.0.1:compile
		[INFO] |  +- org.eclipse.jetty.websocket:javax-websocket-server-impl:jar:9.4.19.v20190610:compile
		[INFO] |  |  +- org.eclipse.jetty:jetty-annotations:jar:9.4.19.v20190610:compile
		[INFO] |  |  |  +- org.eclipse.jetty:jetty-plus:jar:9.4.19.v20190610:compile
		[INFO] |  |  |  +- org.ow2.asm:asm:jar:7.1:compile
		[INFO] |  |  |  \- org.ow2.asm:asm-commons:jar:7.1:compile
		[INFO] |  |  |     +- org.ow2.asm:asm-tree:jar:7.1:compile
		[INFO] |  |  |     \- org.ow2.asm:asm-analysis:jar:7.1:compile
		[INFO] |  |  +- org.eclipse.jetty.websocket:javax-websocket-client-impl:jar:9.4.19.v20190610:compile
		[INFO] |  |  \- javax.websocket:javax.websocket-api:jar:1.1:compile
		[INFO] |  \- org.mortbay.jasper:apache-el:jar:8.5.40:compile
		[INFO] \- org.springframework.boot:spring-boot-starter-test:jar:2.1.7.RELEASE:test
		[INFO]    +- org.springframework.boot:spring-boot-test:jar:2.1.7.RELEASE:test
		[INFO]    +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.1.7.RELEASE:test
		[INFO]    +- com.jayway.jsonpath:json-path:jar:2.4.0:test
		[INFO]    |  +- net.minidev:json-smart:jar:2.3:test
		[INFO]    |  |  \- net.minidev:accessors-smart:jar:1.2:test
		[INFO]    |  \- org.slf4j:slf4j-api:jar:1.7.26:compile
		[INFO]    +- junit:junit:jar:4.12:test
		[INFO]    +- org.assertj:assertj-core:jar:3.11.1:test
		[INFO]    +- org.mockito:mockito-core:jar:2.23.4:test
		[INFO]    |  +- net.bytebuddy:byte-buddy:jar:1.9.16:test
		[INFO]    |  +- net.bytebuddy:byte-buddy-agent:jar:1.9.16:test
		[INFO]    |  \- org.objenesis:objenesis:jar:2.6:test
		[INFO]    +- org.hamcrest:hamcrest-core:jar:1.3:test
		[INFO]    +- org.hamcrest:hamcrest-library:jar:1.3:test
		[INFO]    +- org.skyscreamer:jsonassert:jar:1.5.0:test
		[INFO]    |  \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
		[INFO]    +- org.springframework:spring-core:jar:5.1.9.RELEASE:compile
		[INFO]    |  \- org.springframework:spring-jcl:jar:5.1.9.RELEASE:compile
		[INFO]    +- org.springframework:spring-test:jar:5.1.9.RELEASE:test
		[INFO]    \- org.xmlunit:xmlunit-core:jar:2.6.3:test
		[INFO] ------------------------------------------------------------------------
		[INFO] BUILD SUCCESS
		[INFO] ------------------------------------------------------------------------
		[INFO] Total time: 8.966 s
		[INFO] Finished at: 2019-08-29T22:50:47+08:00
		[INFO] ------------------------------------------------------------------------

上述这些依赖都是隐式依赖于spring-boot-starter-web,我们也可以手动排除一些我们不需要的依赖。
比如spring-boot-starter-web默认集成了tomcat,假如我们想把它换为jetty,可以在pom.xml中spring-boot-starter-web下排除tomcat依赖,然后手动引入jetty依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
</dependencies>

tips:依赖的坐标可以到上述的spring-boot-dependencies-2.1.7.RELEASE.pom文件里查找。再次运行dependency:tree(其实我上边dependency:tree命令已经是替换过Jetty的显示了)可看到tomcat已被替换为了jetty。

  • spring-boot-maven-plugin

spring-boot-maven-plugin为Spring Boot Maven插件,提供了:

1:把项目打包成一个可执行的超级JAR(uber-JAR),包括把应用程序的所有依赖打入JAR文件内,并为JAR添加一个描述文件,其中的内容能让你用java -jar来运行应用程序。
2:搜索public static void main()方法来标记为可运行类。

源码位置

https://github.com/yanglin1501804006/SpringBoot-Study/tree/master/01.start-spring-boot

赞助作者

在这里插入图片描述

本文作者: MissOfSpring
本文链接: https://mp.youkuaiyun.com/mdeditor/100140156
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值