springboot learning - hello world

Introduction:
       Before I start, allow me to introduce the spring I know. I have known Spring since 2007. At that time, the book spring without ejb was already famous. I read this book based on my curiosity about spring. At that time, I claimed to be an orthodox java learner, and had a lot of disdain for spring, whether it was from the learning path or later work needs, basically using the j2EE set such as EJB, JMS, JTA, etc., so that the official blue book of j2EE to I have some understanding of the relevant norms. At that time, I felt that spring gave me a simple but unorthodox feeling. Haha, I felt a bit like an old minister after the change of dynasties. Yes, it was stubborn. This is ideological. In terms of specific use, I felt that spring only brought a lot of convenience to developers through various integration and encapsulation. Of course, the most awkward thing was the xml configuration. It turned out that the objects that I can display and dispatch at will must be configured after use. , which virtually lost a lot of free space. This concept 10 years ago has a lot of influence on my subsequent use of spring, although I have tried every case through that book, and I have used many features in the project later. Read the spring insider book and understand its internals, but not in depth. 10 years later, it is still in the spring to start learning again, as your name is spring, this time maybe because you are wearing "boots" in the rainy season, and I will be re-acquainted with SpringBoot.
Summary:
     I still don't know the reason for the start of the springboot project, but from the features it provides, I am willing to re-learn and use it. Let me introduce the official description first:
 (1) Create stand-alone spring applications
 (2) Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files) Embedded tomcat, jetty web container, no need to publish war files to the container
 (3) Provide opinionated 'starter' POMs to simplify your Maven configuration
 (4) Automatically configure Spring whenever possible The automatic configuration of spring can be realized whenever possible, thus getting rid of the configuration problem of xml
 (5) Provide production-ready features such as metrics, health checks and externalized configuration, which provide a variety of features for product preparation, such as health checks
 (6) Absolutely  no code generation  and  no requirement for XML  configuration, no need for code generation at all, no need for xml configuration at all
It is because of these features that I have regained my confidence, and then with its seamless integration with other projects, such as the cloud, social media has given me a lot of interest. For beginners, there may be many features for unknown reasons, or they are good now. Based on this, I will talk about how I solved these features before and why I use this.
(1) For the spring program that runs independently, some applications need to be run separately, especially ordinary java programs. For example, in some business scenarios, a scheduled task is required to run in the background regularly. At this time, the program does not depend on the web container. At the same time, the application introduces For many third-party jar packages, I used the fatjar plugin to package them. This method will automatically decompress the associated jars into classes and then type them into jar packages. Compared with the jar package of springboot, the two are much smaller, mainly because they have optimized the introduction of the associated jar, and then write an analysis article separately for subsequent research.
(2) Embedded web containers, such as tomcat jetty, this application scenario is mainly for easy development and debugging. Of course, the deployment of subsequent production can also be solved in one stop. The previous method is to package and deploy to the web container after development, or develop The tool integrates web container debugging. Application scenarios, such as the need to quickly provide web applications that are easy to deploy and use, for example, applications used for demonstrations do not require complex deployment. In the previous scheme, such as directly calling jetty's api to start directly through the program, springboot has done this scheme without any problems. Seam combined.
(3) Simplify the maven configuration by providing a preset starter pom. This scenario is mainly to solve the complex maven configuration. The more sub-projects used, the more dependencies involved, and the pre-made startup configuration can be greatly simplified. maven configuration.
(4) Automatic configuration, this is the most critical point of my re-learning this time. There is no need for so many configurations, so that the development is free and flexible, and let us only focus on the java code.
(5) Provides a variety of features that serve product preparation, such as health checks. This scenario is not yet useful. The later supplements should generally be based on application monitoring and inspection. This is a good idea, and there is no need to use the first Third-party applications are monitored.
Actual combat:
    For programmers to learn a new language, framework, the first thought is hello world, yes this journey also starts from here:
  1.   Environmental preparation
            java1.8 maven3.3.9 gradle3 springboot2 , the download link can be Baidu, configure the environment variables after downloading, and verify the results.
           Here are my verification results:
  1.   start developing
          (1) Create the springboot/helloworld folder as D disk
          (2) Create a pom file. Here, we mainly introduce some dependencies and plug-ins necessary for springboot. For simplicity, springboot provides the corresponding parent pom (spring-boot-starter-parent), which is the simplified pom configuration during the startup period mentioned above. an example of
The contents of the pom are listed below
<?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>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.BUILD-SNAPSHOT</version>
    </parent>
    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <!-- Add Spring repositories -->
    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>http://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>http://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
</project>
    The above maven file contains dependencies, packaging plugins, warehouse information, etc., mainly focusing on spring-boot-starter-web, others can be defaulted, because the web features can only be used after the introduction of spring-boot-starter-web.
   (3) Create a controller class
         Create src/main/java/HelloWorld.java 
         code show as below:
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
@RestController
public class HelloWorld {
  @RequestMapping("/")
   string home(){
      return "hello world";
    }

   public static void mian (String[] args)  throws Exception{
          SpringApplication.ruu(HelloWorld.class ,args);
    }
}
@RestController defines this class as a Rest-style control class
@RequestMapping("/") defines the request path, which means that the control class can be accessed directly through localhost:port/
this method of
(4) Run the program using the command mvn spring-boot:run
      Start the application through this command. At this time, springboot will automatically start the embedded web container (tomcat), open it through port 8080, and put the control class HelloWorld into the container. 
(5) Verification results 
    Open localhost:8080 through a browser, and the page displays Hello World!
    
       
(6) Packaged as an executable program
     The running results we saw above cannot be executed all the time if the console is closed, and it cannot be executed without maven environment. Now to solve this problem, the way is to package it through mvn package.
    

 
(6) Execute the program. After the package is completed, a target directory will appear. Enter this directory to execute the generated jar file (java -jar command)


 
The verification result is consistent with the above, and now this jar package can be executed arbitrarily in the environment with java8 (this example is developed with java8, which is guaranteed to be consistent with the development environment).
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326423044&siteId=291194637