从0开始SpringBoot - 1:quick start

141 阅读1分钟

把java重新捡起来, 学习下Spring Boot

spring.io/quickstart

首先,按照quickstart来一遍,亲测,顺利的话半个小时可以搞定

当然需要额外做一些基础配置

  • JDK及环境变量
  • gradle配置
  • homebrew安装
  • zsh中迁移 .bash_profile -> .zshenv

过程中如果遇到什么问题,Google 一下

  1. java 环境变量配置
we can get java home address from this command

/usr/libexec/java_home -V

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-22.jdk/Contents/Home

PATH=$JAVA_HOME/bin:$PATH:.

CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.

export JAVA_HOME
export PATH
export CLASSPATH

迁移 .bash_profile -> .zshenv

参考: carlosroso.com/the-right-w…

Quick Start

完全按照 spring.io/quickstart 的指南走即可

下载的demo,使用 IDE 打开(可以在 IDE 中运行,或者在 terminal中运行都可以)

Screenshot 2024-09-14 at 11.07.50.png

Gradle文件

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.3.3'
    id 'io.spring.dependency-management' version '1.1.6'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    toolchain {
       languageVersion = JavaLanguageVersion.of(22)
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

tasks.named('test') {
    useJUnitPlatform()
}

gradle中有三个scope

  • implementation
  • testImplementation
  • testRuntimeOnly

可以看下表,详情参阅gradle文档

Configuration NameDescriptionUsed to:
apiDependencies required for both compilation and runtime, and included in the published API.Declare Dependencies
implementationDependencies required for both compilation and runtime.Declare Dependencies
compileOnlyDependencies needed only for compilation, not included in runtime or publication.Declare Dependencies
compileOnlyApiDependencies needed only for compilation, but included in the published API.Declare Dependencies
runtimeOnlyDependencies needed only at runtime, not included in the compile classpath.Declare Dependencies
testImplementationDependencies required for compiling and running tests.Declare Dependencies
testCompileOnlyDependencies needed only for test compilation.Declare Dependencies
testRuntimeOnlyDependencies needed only for running tests.Declare Dependencies

手动添加dependency

可以到 mvnrepository.com/ 查找对应的库

Screenshot 2024-09-14 at 10.56.17.png

maven 版本

除了依赖文件换成了pom.xml, dom形式,其他没有本质差别

Screenshot 2024-09-14 at 11.52.21.png

至此,最基础的quick start结束