springboot学习第一个应用程序

本文介绍了一个简单的SpringBoot应用程序的创建过程,包括代码实现、运行方式、JAR打包及执行,适用于初学者快速上手。

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

跟着官网继续学习,写了第一个应用程序:
代码如下所示:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    public String home(){
        return "hello world";
    }

    public static void main(String[] args) {
        SpringApplication.run(Example.class,args);
    }
}

写完上面的程序代码以后,你可以直接run Example.java,然后运行成功后,你会看到类似于下面的输出信息:

D:\Java8\jdk1.8.0_211\bin\java.exe -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dspring.jmx.enabled=true -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.1\lib\idea_rt.jar=62949:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.1\bin" -Dfile.encoding=UTF-8 -classpath D:\Java8\jdk1.8.0_211\jre\lib\charsets.jar;D:\Java8\jdk1.8.0_211\jre\lib\deploy.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\access-bridge-64.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\cldrdata.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\dnsns.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\jaccess.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\jfxrt.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\localedata.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\nashorn.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\sunec.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\sunjce_provider.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\sunmscapi.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\sunpkcs11.jar;D:\Java8\jdk1.8.0_211\jre\lib\ext\zipfs.jar;D:\Java8\jdk1.8.0_211\jre\lib\javaws.jar;D:\Java8\jdk1.8.0_211\jre\lib\jce.jar;D:\Java8\jdk1.8.0_211\jre\lib\jfr.jar;D:\Java8\jdk1.8.0_211\jre\lib\jfxswt.jar;D:\Java8\jdk1.8.0_211\jre\lib\jsse.jar;D:\Java8\jdk1.8.0_211\jre\lib\management-agent.jar;D:\Java8\jdk1.8.0_211\jre\lib\plugin.jar;D:\Java8\jdk1.8.0_211\jre\lib\resources.jar;D:\Java8\jdk1.8.0_211\jre\lib\rt.jar;D:\demo\target\classes;C:\maven\mvn_repo3.0\org\springframework\boot\spring-boot-starter-web\2.1.5.RELEASE\spring-boot-starter-web-2.1.5.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\boot\spring-boot-starter\2.1.5.RELEASE\spring-boot-starter-2.1.5.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\boot\spring-boot\2.1.5.RELEASE\spring-boot-2.1.5.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\boot\spring-boot-autoconfigure\2.1.5.RELEASE\spring-boot-autoconfigure-2.1.5.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\boot\spring-boot-starter-logging\2.1.5.RELEASE\spring-boot-starter-logging-2.1.5.RELEASE.jar;C:\maven\mvn_repo3.0\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;C:\maven\mvn_repo3.0\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;C:\maven\mvn_repo3.0\org\apache\logging\log4j\log4j-to-slf4j\2.11.2\log4j-to-slf4j-2.11.2.jar;C:\maven\mvn_repo3.0\org\apache\logging\log4j\log4j-api\2.11.2\log4j-api-2.11.2.jar;C:\maven\mvn_repo3.0\org\slf4j\jul-to-slf4j\1.7.26\jul-to-slf4j-1.7.26.jar;C:\maven\mvn_repo3.0\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;C:\maven\mvn_repo3.0\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;C:\maven\mvn_repo3.0\org\springframework\boot\spring-boot-starter-json\2.1.5.RELEASE\spring-boot-starter-json-2.1.5.RELEASE.jar;C:\maven\mvn_repo3.0\com\fasterxml\jackson\core\jackson-databind\2.9.8\jackson-databind-2.9.8.jar;C:\maven\mvn_repo3.0\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;C:\maven\mvn_repo3.0\com\fasterxml\jackson\core\jackson-core\2.9.8\jackson-core-2.9.8.jar;C:\maven\mvn_repo3.0\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.8\jackson-datatype-jdk8-2.9.8.jar;C:\maven\mvn_repo3.0\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.8\jackson-datatype-jsr310-2.9.8.jar;C:\maven\mvn_repo3.0\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.8\jackson-module-parameter-names-2.9.8.jar;C:\maven\mvn_repo3.0\org\springframework\boot\spring-boot-starter-tomcat\2.1.5.RELEASE\spring-boot-starter-tomcat-2.1.5.RELEASE.jar;C:\maven\mvn_repo3.0\org\apache\tomcat\embed\tomcat-embed-core\9.0.19\tomcat-embed-core-9.0.19.jar;C:\maven\mvn_repo3.0\org\apache\tomcat\embed\tomcat-embed-el\9.0.19\tomcat-embed-el-9.0.19.jar;C:\maven\mvn_repo3.0\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.19\tomcat-embed-websocket-9.0.19.jar;C:\maven\mvn_repo3.0\org\hibernate\validator\hibernate-validator\6.0.16.Final\hibernate-validator-6.0.16.Final.jar;C:\maven\mvn_repo3.0\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;C:\maven\mvn_repo3.0\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;C:\maven\mvn_repo3.0\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;C:\maven\mvn_repo3.0\org\springframework\spring-web\5.1.7.RELEASE\spring-web-5.1.7.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\spring-beans\5.1.7.RELEASE\spring-beans-5.1.7.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\spring-webmvc\5.1.7.RELEASE\spring-webmvc-5.1.7.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\spring-aop\5.1.7.RELEASE\spring-aop-5.1.7.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\spring-context\5.1.7.RELEASE\spring-context-5.1.7.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\spring-expression\5.1.7.RELEASE\spring-expression-5.1.7.RELEASE.jar;C:\maven\mvn_repo3.0\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar;C:\maven\mvn_repo3.0\org\springframework\spring-core\5.1.7.RELEASE\spring-core-5.1.7.RELEASE.jar;C:\maven\mvn_repo3.0\org\springframework\spring-jcl\5.1.7.RELEASE\spring-jcl-5.1.7.RELEASE.jar com.example.demo.Example

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.5.RELEASE)

2019-06-12 21:15:35.329  INFO 11928 --- [           main] com.example.demo.Example                 : Starting Example on DESKTOP-J2GV336 with PID 11928 (D:\demo\target\classes started by danis in D:\demo)
2019-06-12 21:15:35.334  INFO 11928 --- [           main] com.example.demo.Example                 : No active profile set, falling back to default profiles: default
2019-06-12 21:15:36.775  INFO 11928 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-06-12 21:15:36.796  INFO 11928 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-06-12 21:15:36.796  INFO 11928 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-06-12 21:15:36.896  INFO 11928 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-06-12 21:15:36.897  INFO 11928 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1505 ms
2019-06-12 21:15:37.126  INFO 11928 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-06-12 21:15:37.342  INFO 11928 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-06-12 21:15:37.345  INFO 11928 --- [           main] com.example.demo.Example                 : Started Example in 2.521 seconds (JVM running for 4.071)

然后你在浏览器中输入http://localhost:8080,回车,就会发现出现如下图所示的界面,那么就说明你的第一个springboot application运行成功了:
在这里插入图片描述


下面解释一下上面的那段代码:
(1)这个类有一个main方法,是主入口,也是程序启动的时候的进口,当程序启动的时候,会去执行SpringApplication.run方法,其中第一个参数是Example.class,第二个参数是main方法的形式参数args
(2)@RestController注解:表示给类被当做了一个@Controller,当接收到来自外部的web请求的时候会通过它。该注解会告诉Spring直接把结果字符串移交给调用者。
(3)@EnableAutoConfiguration注解:它是一个类级别的注解,这个注解告诉spring boot来猜测你想怎么配置Spring,基于你已经添加的依赖jar。
(4)@RequestMapping注解:它可以告诉Spring任何的http请求以"/"结尾的路径都会被映射到home方法。


除了你直接在IDEA里面用run Example.java的方式以外,还可以在IDEA中的Terminal中使用命令来进行:

 mvn spring-boot:run

如果想停止可以使用ctrl-c

如何把我们的那个应用打包成一个可执行的JAR文件,我们需要在pom.xml中添加如下配置:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

可以使用mvn package命令进行打JAR包,出出现下面的信息:

D:\Java8\jdk1.8.0_211\bin\java.exe -Dmaven.multiModuleProjectDirectory=D:\demo -Dmaven.home=C:\maven\apache-maven-3.6.1 -Dclassworlds.conf=C:\maven\apache-maven-3.6.1\bin\m2.conf "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.1\lib\idea_rt.jar=51048:C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.1\bin" -Dfile.encoding=UTF-8 -classpath C:\maven\apache-maven-3.6.1\boot\plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version2019.1.1 -s C:\maven\apache-maven-3.6.1\conf\settings.xml -Dmaven.repo.local=C:\maven\mvn_repo3.0 package
[INFO] Scanning for projects...
[INFO] 
[INFO] --------------------------< com.example:demo >--------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ demo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\demo\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ demo ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ demo ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.demo.DemoApplicationTests
22:37:38.858 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.example.demo.DemoApplicationTests]
22:37:38.870 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate]
22:37:38.884 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)]
22:37:38.919 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.example.demo.DemoApplicationTests] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper]
22:37:38.945 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.example.demo.DemoApplicationTests], using SpringBootContextLoader
22:37:38.952 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.demo.DemoApplicationTests]: class path resource [com/example/demo/DemoApplicationTests-context.xml] does not exist
22:37:38.955 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.example.demo.DemoApplicationTests]: class path resource [com/example/demo/DemoApplicationTestsContext.groovy] does not exist
22:37:38.957 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.example.demo.DemoApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
22:37:38.960 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.example.demo.DemoApplicationTests]: DemoApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
22:37:39.053 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.example.demo.DemoApplicationTests]
22:37:39.234 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\demo\target\classes\com\example\demo\DemoApplication.class]
22:37:39.251 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.example.demo.DemoApplication for test class com.example.demo.DemoApplicationTests
22:37:39.426 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.example.demo.DemoApplicationTests]: using defaults.
22:37:39.427 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
22:37:39.462 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
22:37:39.465 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
22:37:39.465 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@79924b, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@7b9a4292, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@4a94ee4, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@4cc451f2, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6379eb, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@294425a7, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@67d48005, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@9f116cc, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@12468a38, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@1aa7ecca]
22:37:39.473 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.demo.DemoApplicationTests]
22:37:39.476 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.demo.DemoApplicationTests]
22:37:39.480 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.demo.DemoApplicationTests]
22:37:39.481 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.demo.DemoApplicationTests]
22:37:39.482 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.demo.DemoApplicationTests]
22:37:39.483 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.demo.DemoApplicationTests]
22:37:39.500 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@8f4ea7c testClass = DemoApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@436813f3 testClass = DemoApplicationTests, locations = '{}', classes = '{class com.example.demo.DemoApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@27808f31, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@1d8d30f7, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@44c8afef, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@e720b71], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null].
22:37:39.510 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.example.demo.DemoApplicationTests]
22:37:39.511 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.example.demo.DemoApplicationTests]
22:37:39.546 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true, server.port=-1}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.5.RELEASE)

2019-06-12 22:37:40.015  INFO 12868 --- [           main] com.example.demo.DemoApplicationTests    : Starting DemoApplicationTests on DESKTOP-J2GV336 with PID 12868 (started by danis in D:\demo)
2019-06-12 22:37:40.016  INFO 12868 --- [           main] com.example.demo.DemoApplicationTests    : No active profile set, falling back to default profiles: default
2019-06-12 22:37:42.601  INFO 12868 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-06-12 22:37:43.091  INFO 12868 --- [           main] com.example.demo.DemoApplicationTests    : Started DemoApplicationTests in 3.528 seconds (JVM running for 5.188)
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.959 s - in com.example.demo.DemoApplicationTests
2019-06-12 22:37:43.496  INFO 12868 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] 
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) @ demo ---
[INFO] Building jar: D:\demo\target\demo-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:2.1.5.RELEASE:repackage (repackage) @ demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  12.069 s
[INFO] Finished at: 2019-06-12T22:37:45+08:00
[INFO] ------------------------------------------------------------------------

Process finished with exit code 0

说明你的JAR包已经成功打好。
从控制台的输出信息中可以看到你的JAR包的路径:Building jar: D:\demo\target\demo-0.0.1-SNAPSHOT.jar
所以你可以去你应用的target目录下寻找你的JAR包。
(你也可以使用IDEA直接打JAR包,如下图所示:)
在这里插入图片描述
target目录下,不单单只有一个JAR,还会存在一个*.jar.original的文件,它是在使用Spring Boot重新打包之前Maven创建的一个文件。
在这里插入图片描述
如何运行这个JAR文件,在terminal中使用java -jar target/demo-0.0.1-SNAPSHOT.jar,就会出现如下图所示的效果:

D:\demo>java -jar target/demo-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.5.RELEASE)

2019-06-12 22:44:49.327  INFO 12476 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT on DESKTOP-J2GV336 with PID 12476 (D:\demo\t
arget\demo-0.0.1-SNAPSHOT.jar started by danis in D:\demo)
2019-06-12 22:44:49.344  INFO 12476 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2019-06-12 22:44:51.522  INFO 12476 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-06-12 22:44:51.568  INFO 12476 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-06-12 22:44:51.570  INFO 12476 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.19]
2019-06-12 22:44:51.696  INFO 12476 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-06-12 22:44:51.697  INFO 12476 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2272 ms
2019-06-12 22:44:51.997  INFO 12476 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-06-12 22:44:52.289  INFO 12476 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-06-12 22:44:52.294  INFO 12476 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.705 seconds (JVM running for 4.366)
2019-06-12 22:45:15.732  INFO 12476 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-06-12 22:45:15.733  INFO 12476 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-06-12 22:45:15.741  INFO 12476 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 8 ms

然后你可以在浏览器里面访问:http://localhost:8080会出现hello world

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值