欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring Boot的應(yīng)用啟動(dòng)與關(guān)閉的方法

 更新時(shí)間:2017年12月07日 10:55:50   作者:SnailTyan  
本篇文章主要介紹了Spring Boot的應(yīng)用啟動(dòng)與關(guān)閉的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

Spring Boot,作為Spring框架對(duì)“約定優(yōu)先于配置(Convention Over Configuration)”理念的最佳實(shí)踐的產(chǎn)物,它能幫助我們很快捷的創(chuàng)建出獨(dú)立運(yùn)行、產(chǎn)品級(jí)別的基于Spring框架的應(yīng)用,大部分Spring Boot應(yīng)用只需要非常少的配置就可以快速運(yùn)行起來(lái),是一個(gè)與微服務(wù)(MicroServices)相當(dāng)契合的微框架。

1. Spring Boot應(yīng)用打包

Spring Boot應(yīng)用可以打成jar包,其中內(nèi)嵌tomcat,因此可以直接啟動(dòng)使用。但是在Spring Boot應(yīng)用啟動(dòng)之前,首先需要進(jìn)行打包,本文講述的是Maven工程的打包,打包需要的前提條件(pom.xml文件中的內(nèi)容)是:

...

<packaging>jar</packaging>

...

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

...

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <configuration>
        <mainClass>com.***.Application</mainClass>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>repackage</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

...

打包命令為:

mvn clean package -Dmaven.test.skip=true
# Demo
$ mvn clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[WARNING] 
[WARNING] Some problems were encountered while building the effective model for com.example:myproject:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.springframework.boot:spring-boot-maven-plugin is missing. @ line 38, column 17
[WARNING] 
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING] 
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING] 
[INFO]                                     
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ myproject ---
[INFO] Deleting /Users/ltc/Spring Boot Demo/target
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myproject ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myproject ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/ltc/Spring Boot Demo/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myproject ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myproject ---
[INFO] Not compiling test sources
[INFO] 
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myproject ---
[INFO] Tests are skipped.
[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
[INFO] Building jar: /Users/ltc/Spring Boot Demo/target/myproject-0.0.1-SNAPSHOT.jar
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.0.RC1:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.861 s
[INFO] Finished at: 2017-01-13T15:31:32+08:00
[INFO] Final Memory: 26M/308M
[INFO] ------------------------------------------------------------------------

或在eclipse中運(yùn)行run -> Maven build...,在Goals中填寫(xiě)clean package -Dmaven.test.skip=true,運(yùn)行,打包完成。

2. Spring Boot應(yīng)用啟動(dòng)

Spring Boot的啟動(dòng)命令為:

java -jar application.jar
# Demo
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar 
 .  ____     _      __ _ _
 /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/ ___)| |_)| | | | | || (_| | ) ) ) )
 ' |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::    (v1.4.3.RELEASE)
2017-01-13 15:31:36.911 INFO 62119 --- [      main] com.test.Example             : Starting Example on local with PID 62119 (/Users/ltc/Spring Boot Demo/target/myproject-0.0.1-SNAPSHOT.jar started by liutianchi in /Users/ltc/Spring Boot Demo)
2017-01-13 15:31:36.916 INFO 62119 --- [      main] com.test.Example             : No active profile set, falling back to default profiles: default
2017-01-13 15:31:36.981 INFO 62119 --- [      main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b1a58a3: startup date [Fri Jan 13 15:31:36 CST 2017]; root of context hierarchy
2017-01-13 15:31:38.602 INFO 62119 --- [      main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-01-13 15:31:38.615 INFO 62119 --- [      main] o.apache.catalina.core.StandardService  : Starting service Tomcat
2017-01-13 15:31:38.616 INFO 62119 --- [      main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.6
2017-01-13 15:31:38.718 INFO 62119 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]    : Initializing Spring embedded WebApplicationContext
2017-01-13 15:31:38.718 INFO 62119 --- [ost-startStop-1] o.s.web.context.ContextLoader      : Root WebApplicationContext: initialization completed in 1740 ms
2017-01-13 15:31:38.927 INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean  : Mapping filter: 'metricsFilter' to: [/*]
2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean  : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean  : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean  : Mapping filter: 'requestContextFilter' to: [/*]
2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean  : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean  : Mapping filter: 'applicationContextIdFilter' to: [/*]
2017-01-13 15:31:39.217 INFO 62119 --- [      main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b1a58a3: startup date [Fri Jan 13 15:31:36 CST 2017]; root of context hierarchy
2017-01-13 15:31:39.310 INFO 62119 --- [      main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.test.Example.home()
2017-01-13 15:31:39.313 INFO 62119 --- [      main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-01-13 15:31:39.313 INFO 62119 --- [      main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-01-13 15:31:39.338 INFO 62119 --- [      main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-13 15:31:39.338 INFO 62119 --- [      main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-13 15:31:39.378 INFO 62119 --- [      main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-13 15:31:39.665 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/metrics/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2017-01-13 15:31:39.665 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/metrics || /manage/metrics.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.666 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/mappings || /manage/mappings.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.667 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/trace || /manage/trace.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.667 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/info || /manage/info.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.668 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/configprops || /manage/configprops.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.669 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/heapdump || /manage/heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
2017-01-13 15:31:39.669 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/autoconfig || /manage/autoconfig.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.673 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/env/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2017-01-13 15:31:39.673 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/env || /manage/env.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.674 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/health || /manage/health.json],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal)
2017-01-13 15:31:39.675 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/dump || /manage/dump.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.677 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/shutdown || /manage/shutdown.json],methods=[POST]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.ShutdownMvcEndpoint.invoke()
2017-01-13 15:31:39.678 INFO 62119 --- [      main] o.s.b.a.e.mvc.EndpointHandlerMapping   : Mapped "{[/manage/beans || /manage/beans.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.799 INFO 62119 --- [      main] o.s.j.e.a.AnnotationMBeanExporter    : Registering beans for JMX exposure on startup
2017-01-13 15:31:39.809 INFO 62119 --- [      main] o.s.c.support.DefaultLifecycleProcessor : Starting beans in phase 0
2017-01-13 15:31:39.944 INFO 62119 --- [      main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-01-13 15:31:39.949 INFO 62119 --- [      main] com.test.Example             : Started Example in 4.292 seconds (JVM running for 4.726)

3. Spring Boot應(yīng)用關(guān)閉

下面主要有兩種方式進(jìn)行Spring Boot的關(guān)閉:通過(guò)HTTP發(fā)送shutdown信號(hào),或者通過(guò)service stop的方式。

Spring Boot應(yīng)用關(guān)閉的前提條件是POM.xml添加以下內(nèi)容:

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

application.properties中添加:

#啟用shutdown
endpoints.shutdown.enabled=true
#禁用密碼驗(yàn)證
endpoints.shutdown.sensitive=false

關(guān)閉命令為:

curl -X POST host:port/shutdown
# Demo
$ curl -X POST http://localhost:8080/shutdown
{"message":"Shutting down, bye..."}
$ curl -X POST http://localhost:8080/manage/shutdown
{"message":"Shutting down, bye..."}

如果要配置路徑,需要在application.properties中添加management.context-path=/manage,則關(guān)閉命令變?yōu)閏url -X POST host:port/manage/shutdown。

4. 安全驗(yàn)證

如果在關(guān)閉時(shí)需要安全驗(yàn)證,則在pom.xml文件中添加:

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

application.properties中添加:

#開(kāi)啟shutdown的安全驗(yàn)證
endpoints.shutdown.sensitive=true
#驗(yàn)證用戶名
security.user.name=admin
#驗(yàn)證密碼
security.user.password=admin
#角色
management.security.role=SUPERUSER
# 指定端口
management.port=8081
# 指定地址
management.address=127.0.0.1

關(guān)閉命令為:

curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown
# Demo
$ curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown
{"message":"Shutting down, bye..."}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SSH框架網(wǎng)上商城項(xiàng)目第9戰(zhàn)之添加和更新商品類別功能實(shí)現(xiàn)

    SSH框架網(wǎng)上商城項(xiàng)目第9戰(zhàn)之添加和更新商品類別功能實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了SSH框架網(wǎng)上商城項(xiàng)目第9戰(zhàn)之添加和更新商品類別功能實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2016-06-06
  • Hadoop組件簡(jiǎn)介

    Hadoop組件簡(jiǎn)介

    Hadoop作為一種分布式基礎(chǔ)架構(gòu),可以使用戶在不了解分布式底層細(xì)節(jié)的情況下,開(kāi)發(fā)分布式程序。接下來(lái)通過(guò)本文給大家分享Hadoop組件簡(jiǎn)介,感興趣的朋友一起看看吧
    2017-09-09
  • Atomikos + MybatisPlus解決多數(shù)據(jù)源事務(wù)一致性問(wèn)題解決

    Atomikos + MybatisPlus解決多數(shù)據(jù)源事務(wù)一致性問(wèn)題解決

    在實(shí)際項(xiàng)目的開(kāi)發(fā)過(guò)程中,我們經(jīng)常會(huì)遇到在同一個(gè)項(xiàng)目或微服務(wù)中牽涉到使用兩個(gè)或多個(gè)數(shù)據(jù)源的,本文主要介紹了Atomikos + MybatisPlus解決多數(shù)據(jù)源事務(wù)一致性問(wèn)題解決,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • JAVA實(shí)現(xiàn)多線程的兩種方法實(shí)例分享

    JAVA實(shí)現(xiàn)多線程的兩種方法實(shí)例分享

    這篇文章介紹了JAVA實(shí)現(xiàn)多線程的兩種方法實(shí)例分享,有需要的朋友可以參考一下
    2013-08-08
  • Mybatis generator自動(dòng)生成代碼插件實(shí)例解析

    Mybatis generator自動(dòng)生成代碼插件實(shí)例解析

    這篇文章主要介紹了Mybatis generator自動(dòng)生成代碼插件實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 利用Java編寫(xiě)一個(gè)出敬業(yè)福的小程序

    利用Java編寫(xiě)一個(gè)出敬業(yè)福的小程序

    新年將至,又開(kāi)始掃福活動(dòng),每年的敬業(yè)福成了大家難過(guò)的坎。所以本文將介紹一個(gè)通過(guò)Java編寫(xiě)的一款福字生成器,感興趣的小伙伴可以試一試
    2022-01-01
  • MyBatis實(shí)戰(zhàn)之Mapper注解的示例

    MyBatis實(shí)戰(zhàn)之Mapper注解的示例

    本文主要介紹了MyBatis實(shí)戰(zhàn)之Mapper注解的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • Java如何檢測(cè)當(dāng)前CPU負(fù)載狀態(tài)

    Java如何檢測(cè)當(dāng)前CPU負(fù)載狀態(tài)

    在Java中,直接檢測(cè)CPU負(fù)載狀態(tài)并不像在操作系統(tǒng)命令行中那樣簡(jiǎn)單,因?yàn)镴ava標(biāo)準(zhǔn)庫(kù)并沒(méi)有直接提供這樣的功能,這篇文章主要介紹了java檢測(cè)當(dāng)前CPU負(fù)載狀態(tài)的方法,需要的朋友可以參考下
    2024-06-06
  • Java基礎(chǔ)入門(mén)總結(jié)之序列化和反序列化

    Java基礎(chǔ)入門(mén)總結(jié)之序列化和反序列化

    序列化是一種對(duì)象持久化的手段,普遍應(yīng)用在網(wǎng)絡(luò)傳輸、RMI等場(chǎng)景中,下面這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)入門(mén)總結(jié)之序列化和反序列化的相關(guān)資料,需要的朋友可以參考下
    2022-02-02
  • Kotlin 基礎(chǔ)語(yǔ)法詳細(xì)介紹

    Kotlin 基礎(chǔ)語(yǔ)法詳細(xì)介紹

    這篇文章主要介紹了Kotlin 基礎(chǔ)語(yǔ)法詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下
    2017-05-05

最新評(píng)論