把java重新捡起来, 学习下Spring Boot
首先,按照quickstart来一遍,亲测,顺利的话半个小时可以搞定
当然需要额外做一些基础配置
- JDK及环境变量
- gradle配置
- homebrew安装
- zsh中迁移 .bash_profile -> .zshenv
过程中如果遇到什么问题,Google 一下
- 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中运行都可以)
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 Name | Description | Used to: |
|---|---|---|
api | Dependencies required for both compilation and runtime, and included in the published API. | Declare Dependencies |
implementation | Dependencies required for both compilation and runtime. | Declare Dependencies |
compileOnly | Dependencies needed only for compilation, not included in runtime or publication. | Declare Dependencies |
compileOnlyApi | Dependencies needed only for compilation, but included in the published API. | Declare Dependencies |
runtimeOnly | Dependencies needed only at runtime, not included in the compile classpath. | Declare Dependencies |
testImplementation | Dependencies required for compiling and running tests. | Declare Dependencies |
testCompileOnly | Dependencies needed only for test compilation. | Declare Dependencies |
testRuntimeOnly | Dependencies needed only for running tests. | Declare Dependencies |
手动添加dependency
可以到 mvnrepository.com/ 查找对应的库
maven 版本
除了依赖文件换成了pom.xml, dom形式,其他没有本质差别
至此,最基础的quick start结束