1. SpringBoot入门


  • 23.12.25 2:30更新

1.1. 官方地址

https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.introducing-spring-boot

1.2. SpringBoot2系统要求

  • Java 8 & 兼容java14 .
  • Maven 3.3+
  • idea 2019.1.2

1.2.1. 命令

  • 监测java版本
java -version
java version "1.8.0_361"
Java(TM) SE Runtime Environment (build 1.8.0_361-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.361-b09, mixed mode)
  • 查看maven版本
mvn -v
Apache Maven 3.9.3 (21122926829f1ead511c958d89bd2f672198ae9f)
Maven home: /Users/chenchangqing/Documents/apps/apache-maven-3.9.3
Java version: 1.8.0_361, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_361.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "11.7.10", arch: "x86_64", family: "mac"

1.2.2. 修改setting.xml

  1. 打开Maven home/conf/settings.xml
  2. 替换配置文件:
  <mirrors>
      <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>central</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
      </mirror>
  </mirrors>

  <profiles>
         <profile>
              <id>jdk-1.8</id>
              <activation>
                <activeByDefault>true</activeByDefault>
                <jdk>1.8</jdk>
              </activation>
              <properties>
                <maven.compiler.source>1.8</maven.compiler.source>
                <maven.compiler.target>1.8</maven.compiler.target>
                <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
              </properties>
         </profile>
  </profiles>

1.3. HelloWorld

1.3.1. 打开官方文档

https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.first-application

1.3.2. 创建maven的springboot项目

http://1221.site/pages/idea/createp.html#%E4%BD%BF%E7%94%A8maven%E5%88%9B%E5%BB%BAspringboot%E9%A1%B9%E7%9B%AE

1)点击New Project

2)项目设置

3)Create

4)pom.xml

1.3.3. 默认会有一个Main入口

@SpringBootApplication
public class Springboot01HelloworldApplication {

    public static void main(String[] args) {
        SpringApplication.run(Springboot01HelloworldApplication.class, args);
    }

}

1.3.4. 创建类HelloController

右键包名,New->Java Class,输入HelloController

@RestController
public class HelloController {

    @RequestMapping("/hello")
    public String handle01(){
        return "Hello, Spring Boot!";
    }
}

1.3.5. 运行项目

Springboot01HelloworldApplication运行main方法:

发现错误

springboot3.2.1不支持jdk1.8,需要更新到java17

1.4. 配置文件

application.properties

https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties

  • 例如:Server Properties -> server.port

1.5. 简化部署

springboot支持通过.jar文件直接启动服务。官方文档:

https://docs.spring.io/spring-boot/docs/current/reference/html/getting-started.html#getting-started.first-application.executable-jar

1.5.1. 配置pom.xml

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

1.5.2. 打包

1.5.3. 找到jar

target->xxx-1.0-SNAPSHOT.jar

1.5.4. 运行项目

cd到jar目录,执行:

java -jar xxx-1.0-SNAPSHOT.jar

xxx-1.0-SNAPSHOT.jar/BOOT-INF/lib:这里目录下有tomcat-embed-core-9.0.38.jartomcat-embed-websocket-9.0.38,带有tomcat环境。

1.6. 视频地址

results matching ""

    No results matching ""