zoukankan      html  css  js  c++  java
  • Spring boot 官网学习笔记

    1. Creating the POM
      1. <?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>
            
            <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.1.1.RELEASE</version>
            </parent>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </dependency>
            </dependencies>
        </project>
            
      2. 通过POM的dependencies 实现 Adding Classpath Dependencies
      3. 关于spring-boot-starter-parent:You should need to specify only the Spring Boot version number on this dependency. If you import additional starters, you can safely omit the version number.

    2. 使用 mvn dependency:tree  查看project dependencies
    3. 编写java代码
      1. src/main/java/Example.java
      2. import org.springframework.boot.*;
        import org.springframework.boot.autoconfigure.*
        import org.springframework.web.bind.annotation.*;
        
        @RestController
        @EnableAutoConfiguration
        public class Example{
            @RequestMapping("/")
            String home(){
                return "Hello World!";
            }
            public static void main(String[] args) throws Exception{
                SpringApplication.run(Example.class,args);
            }
        }
      3. @RestController and @RequestMapping annotations are Spring MVC annotations
      4. @EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.
      5. Starters and Auto-configuration

        Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied. You are free to pick and choose jar dependencies outside of the starters. Spring Boot still does its best to auto-configure your application.

    4. Running the Example
      1. mvn spring-boot:run
    5. Creating an Executable Jar
      1. To create an executable jar, we need to add the spring-boot-maven-plugin to our pom.xml. To do so, insert the following lines just below the dependencies section:
      2. Spring Boot includes a Maven plugin that can package the project as an executable jar. Add the plugin to your <plugins> section if you want to use it, as shown in the following example:
      3. <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
        
        
        
        
        <?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>
            
            <parent>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>2.1.1.RELEASE</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>
                    </plugin>
                </plugins>
            </build>
        </project>
            
      4. mvn package
      5. java -jar target/myproject-0.0.1-SNAPSHOT.jar
    6. 参考:https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#getting-started-first-application
  • 相关阅读:
    Linux下替换默认版本的protobuf
    论文笔记——NEURAL ARCHITECTURE SEARCH WITH REINFORCEMENT LEARNING
    kafka 学习之初体验
    git命令01
    git 命令02
    SSH远程连接连接其他主机,等待时间过长的原因。
    lsof命令详解
    文本处理命令
    Windows Server 2008 远程桌面连接拒绝
    vim文本编辑工具—修改文件内容
  • 原文地址:https://www.cnblogs.com/jiangtao1218/p/10099255.html
Copyright © 2011-2022 走看看