Spring Boot, as the product of the best practice of the Spring framework for the concept of "Convention Over Configuration", can help us quickly create independent, product-level Spring framework-based applications. Most Spring Boot applications require very little configuration to run quickly. It is a micro-framework that is quite consistent with microservices.
1. Spring Boot application package
Spring Boot application can be typed into a jar package, with tomcat embedded in it, so it can be started directly. However, before the Spring Boot application is started, packaging is first necessary. This article talks about packaging the Maven project. The prerequisites required for packaging (the content in the pom.xml file) are:
...<packaging>jar</packaging>...<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency>...<build> <plugins> <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>...
The package command is:
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 building.[WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects.[WARNING] [INFO] [INFO] ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 0.0.1-SNAPSHOT[INFO] ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Actually) to copy filtered resources, ie 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, ie 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] --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Or run run -> Maven build... in eclipse, fill in clean package -Dmaven.test.skip=true in Goals, run, package is completed.
2. Spring Boot application startup
The start command of Spring Boot is:
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: default2017-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 hierarchy2017-01-13 15:31:38.602 INFO 62119 --- [ main] sbcetTomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)2017-01-13 15:31:38.615 INFO 62119 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat2017-01-13 15:31:38.616 INFO 62119 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.62017-01-13 15:31:38.718 INFO 62119 --- [ost-startStop-1] occC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2017-01-13 15:31:38.718 INFO 62119 --- [ost-startStop-1] occC[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext2017-01-13 15:31:38.718 INFO 62119 --- [ost-startStop-1] osweb.context.ContextLoader : Root WebApplicationContext: initialization completed in 1740 ms2017-01-13 15:31:38.927 INFO 62119 --- [ost-startStop-1] osbwservlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'metricsFilter' to: [/*]2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'webRequestLoggingFilter' to: [/*]2017-01-13 15:31:38.932 INFO 62119 --- [ost-startStop-1] osbwservlet.FilterRegistrationBean : Mapping filter: 'applicationContextIdFilter' to: [/*]2017-01-13 15:31:39.217 INFO 62119 --- [ main] swsmmaRequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b1a58a3: startup date [Fri Jan 13 15:31:36 CST 2017]; root of context hierarchy2017-01-13 15:31:39.310 INFO 62119 --- [ main] swsmmaRequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.test.Example.home()2017-01-13 15:31:39.313 INFO 62119 --- [ main] swsmmaRequestMappingHandlerMapping : 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] swsmmaRequestMappingHandlerMapping: 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] oswshandler.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] oswshandler.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] oswshandler.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] osbaemvc.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] osbaemvc.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] osbaemvc.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] osbaemvc.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] osbaemvc.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] osbaemvc.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] osbaemvc.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.Servlet.ServletException2017-01-13 15:31:39.669 INFO 62119 --- [ main] osbaemvc.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] osbaemvc.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] osbaemvc.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] osbaemvc.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] osbaemvc.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] osbaemvc.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] osbaemvc.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] osjeaAnnotationMBeanExporter : Registering beans for JMX exposure on startup2017-01-13 15:31:39.809 INFO 62119 --- [ main] oscsupport.DefaultLifecycleProcessor : Starting beans in phase 02017-01-13 15:31:39.944 INFO 62119 --- [ main] sbcetTomcatEmbeddedServletContainer : 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 app closes
There are two main ways to close Spring Boot: sending shutdown signals via HTTP, or using service stop.
The prerequisite for closing Spring Boot app is that POM.xml adds the following content:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency>
Added in application.properties:
#Enable shutdownendpoints.shutdown.enabled=true#Disable password verification endpoints.shutdown.sensitive=false
The closing command is:
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..."}If you want to configure the path, you need to add management.context-path=/manage in application.properties, the shutdown command becomes curl -X POST host:port/manage/shutdown.
4. Security Verification
If security verification is required when closing, add in the pom.xml file:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId></dependency>
Added in application.properties:
#Enable shutdown security verification endpoints.shutdown.sensitive=true#Verify username security.user.name=admin#Verify password security.user.password=admin#Role management.security.role=SUPERUSER#Specify port management.port=8081#Specify address management.address=127.0.0.1
The closing command is:
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..."}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.