Spring Boot application development started, springboot Application Development

Source: Internet
Author: User
Tags groovy script

Spring Boot application development started, springboot Application Development
Create a Spring Boot Application

Spring Boot supports multiple integration methods, such as CLI, Maven, and Gradle. Here, Mavan is used as an example. A typical Maven Spring Boot application must meet the Spring Boot conventions. For example, the most common method is spring-boot-starter-web, the Project build script can be configured as follows:

 1 <?xml version="1.0" encoding="UTF-8"?> 2  3 <project xmlns="http://maven.apache.org/POM/4.0.0" 4          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 6     <modelVersion>4.0.0</modelVersion> 7  8     <groupId>com.example</groupId> 9     <artifactId>cnblogs_example</artifactId>10     <version>1.0-SNAPSHOT</version>11 12     <parent>13         <groupId>org.springframework.boot</groupId>14         <artifactId>spring-boot-starter-parent</artifactId>15         <version>2.0.0.BUILD-SNAPSHOT</version>16     </parent>17 18 19     <dependencies>20         <dependency>21             <groupId>org.springframework.boot</groupId>22             <artifactId>spring-boot-starter-web</artifactId>23         </dependency>24     </dependencies>25 26     <!-- ignore others-->27 </project>

 

A basic application can be defined by Example. java:

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@EnableAutoConfigurationpublic class Example {    @RequestMapping("/")    String home() {        return "Hello World!";    }    public static void main(String[] args) throws Exception {        SpringApplication.run(Example.class, args);}

Since then, a simple Spring Boot Web application has been created. Of course, if you need to directly use "mvn package", you can generate an all-in-one executable jar (Uber jar. add some content in xml. The final content is as follows:

<?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.example</groupId>    <artifactId>cnblogs_example</artifactId>    <version>1.0-SNAPSHOT</version>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.0.0.BUILD-SNAPSHOT</version>    </parent>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>                <configuration>                    <mainClass>Example</mainClass>                </configuration>            </plugin>            <plugin>                <groupId>org.apache.maven.plugins</groupId>                <artifactId>maven-compiler-plugin</artifactId>                <configuration>                    <source>1.8</source>                    <target>1.8</target>                </configuration>            </plugin>        </plugins>    </build></project>
View Code

In this way, you can simply run this web application through the "java-jar cnblogs_example-1.0-SNAPSHOT.jar", and can access it through http: // 127.0.0.1: 8080/, it is really very simple, it is also highly portable. A jar file can run in any place where Java is running.

Some configuration specification Application sources

According to [2], the supported sources types are:

  • Class: Java Class that can be loaded by AnnotatedBeanDefinitionReader
  • Resource: XML Resource that can be loaded by XmlBeanDefinitonReader or groovy script that can be loaded by yybeandefinitionreader
  • Package: Java Package scanned by classpatemediandefinition.pdf
  • CharSequence: tries to load data as Java class, Resource, and Java packages in turn.

 

 

How is an application started?

Obviously, Spring Boot hides many technical details, including the startup process. Here we hope to summarize the startup principles.

Self-managed package dependency

This is a maven feature, but Spring Boot provides a good encapsulation for microservice support. After adding the "spring-boot-starter-web" dependency, Spring Boot gets the following dependencies, including Tomcat and Spring Boot jar:

D:\Workspace\MicroServices\cnblogs_example>mvn dependency:tree[INFO] Scanning for projects...[INFO][INFO] ------------------------------------------------------------------------[INFO] Building cnblogs_example 1.0-SNAPSHOT[INFO] ------------------------------------------------------------------------[INFO][INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ cnblogs_example ---[INFO] com.example:cnblogs_example:jar:1.0-SNAPSHOT[INFO] \- org.springframework.boot:spring-boot-starter-web:jar:2.0.0.BUILD-SNAPSHOT:compile[INFO]    +- org.springframework.boot:spring-boot-starter:jar:2.0.0.BUILD-SNAPSHOT:compile[INFO]    |  +- org.springframework.boot:spring-boot:jar:2.0.0.BUILD-SNAPSHOT:compile[INFO]    |  |  \- io.projectreactor.addons:reactor-test:jar:3.0.5.RELEASE:compile[INFO]    |  |     \- io.projectreactor:reactor-core:jar:3.0.5.RELEASE:compile[INFO]    |  |        \- org.reactivestreams:reactive-streams:jar:1.0.0:compile[INFO]    |  +- org.springframework.boot:spring-boot-autoconfigure:jar:2.0.0.BUILD-SNAPSHOT:compile[INFO]    |  +- org.springframework.boot:spring-boot-starter-logging:jar:2.0.0.BUILD-SNAPSHOT:compile[INFO]    |  |  +- ch.qos.logback:logback-classic:jar:1.2.2:compile[INFO]    |  |  |  +- ch.qos.logback:logback-core:jar:1.2.2:compile[INFO]    |  |  |  \- org.slf4j:slf4j-api:jar:1.7.25:compile[INFO]    |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile[INFO]    |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile[INFO]    |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.25:compile[INFO]    |  +- org.springframework:spring-core:jar:5.0.0.BUILD-SNAPSHOT:compile[INFO]    |  \- org.yaml:snakeyaml:jar:1.18:runtime[INFO]    +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.0.0.BUILD-SNAPSHOT:compile[INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-core:jar:8.5.12:compile[INFO]    |  +- org.apache.tomcat.embed:tomcat-embed-el:jar:8.5.12:compile[INFO]    |  \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:8.5.12:compile[INFO]    +- org.hibernate:hibernate-validator:jar:5.4.1.Final:compile[INFO]    |  +- javax.validation:validation-api:jar:1.1.0.Final:compile[INFO]    |  +- org.jboss.logging:jboss-logging:jar:3.3.1.Final:compile[INFO]    |  \- com.fasterxml:classmate:jar:1.3.3:compile[INFO]    +- com.fasterxml.jackson.core:jackson-databind:jar:2.9.0.pr2:compile[INFO]    |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.9.0.pr2:compile[INFO]    |  \- com.fasterxml.jackson.core:jackson-core:jar:2.9.0.pr2:compile[INFO]    +- org.springframework:spring-web:jar:5.0.0.BUILD-SNAPSHOT:compile[INFO]    |  +- org.springframework:spring-aop:jar:5.0.0.BUILD-SNAPSHOT:compile[INFO]    |  +- org.springframework:spring-beans:jar:5.0.0.BUILD-SNAPSHOT:compile[INFO]    |  \- org.springframework:spring-context:jar:5.0.0.BUILD-SNAPSHOT:compile[INFO]    \- org.springframework:spring-webmvc:jar:5.0.0.BUILD-SNAPSHOT:compile[INFO]       \- org.springframework:spring-expression:jar:5.0.0.BUILD-SNAPSHOT:compile[INFO] ------------------------------------------------------------------------[INFO] BUILD SUCCESS[INFO] ------------------------------------------------------------------------

 

SpringApplication Bootstrap

The SpringApplication class provides a guiding program for the user program. The startup method is called in the user's main method. The main work of SpringApplication is as follows ]:

  • Create an appropriate ApplicationContxt instance based on classpath
  • Register CommandLinePropertySource for the startup command line parameters and expose it as a Spring attribute.
  • Refresh ApplicationContext and load all Singleton beans
  • Trigger all CommandLineRunner beans.

 

References:

[2]: http://zhaox.github.io/java/2016/03/22/spring-boot-start-flow

[2]: http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/SpringApplication.html

[3] Spring Boot Startup Process and callback interface Summary: https://segmentfault.com/a/1190000006918229

 

Copyright statement: Welcome to share, reprint please indicate the source of http://www.cnblogs.com/1xin/p/6627054.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.