Spring Development Guide

Spring 开发指引

IDEA

配置指定 JDK

File > Project Structure ...

Spring Boot

自动配置

通过 src/main/resources/application.properties 可以在不同环境中跳过部分自动配置:

spring.autoconfigure.exclude = \
  org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, \
  org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration, \
  org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration, \
  org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration, \
  org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration

启动

mvn clean install
java -jar target/PACKAGE_NAME.<jar|war>

# 通过 Maven Spring Boot 插件启动
mvn clean spring-boot:run

环境

JDK

curl -s "https://get.sdkman.io" | bash

sdk list java

JDK 版本切换

# 查看已安装 JDK
/usr/libexec/java_home -V

macOS 可以配置以下别名快速设置 JAVA_HOME:

alias j15="export JAVA_HOME=`/usr/libexec/java_home -v 15`"
alias j11="export JAVA_HOME=`/usr/libexec/java_home -v 11`"
alias j8="export JAVA_HOME=`/usr/libexec/java_home -v 1.8`"

Maven

# 安装 mvn 命令支持
brew install maven

# 项目目录下安装 Maven Wrapper
mvn -N io.takari:maven:wrapper

# 下载依赖库源代码
mvn dependency:sources dependency:resolve -Dclassifier=javadoc

Mave 的全局设置保存在 ~/m2/settings.xml

<settings>
    <!-- 设置 maven 自动下载依赖库源代码及 Java Docs -->
    <profiles>
        <profile>
            <id>downloadSourcesDocs</id>
            <properties>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </properties>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>downloadSourcesDocs</activeProfile>
    </activeProfiles>
</settings>

Gradle

# 安装 Gradle 命令支持
brew install gradle

Spring Boot 命令行

安装 spring 命令行:

# 通过 SDK 命令
sdk install springboot

# 通过 brew 命令
brew tap spring-io/tap
brew install spring-boot
# 确定 Spring CLI 版本
spring --version

JMX

开启 8855 侦听,可用 jconsole 分析:

-Djavax.management.builder.initial=
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=8855
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false

Spring 脚手架初始化服务

JDK 11.0.9、15

Spring Boot 2.3.x 官方文档:起步、功能

Author: njun
njun's picture
Updated: 2021/02/12