Spring Boot 的5个实用技巧

在 Java 开发领域,Spring Boot 以简化配置、快速开发等特性,目前成为必不可少的开发框架。但在日常开发中,还有许多实用技巧能让我们更高效地使用 Spring Boot。今天分享工作中常用的 25个实用技巧。

  1. @ConfigurationProperties 管理复杂配置
    在复杂项目中,配置项众多,分散在各处的配置不利于管理。这时,@ConfigurationProperties注解就能派上用场。它能将多个相关配置映射到一个类中,使代码更简洁。
    定义一个配置类:

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "app")
public class AppProperties {
private String name;
private int version;

// getters and setters

}

配置文件中:
app:
name: mySpringApp
version: 1

在其他组件中,通过@Autowired注入AppProperties,就可以方便地获取配置信息:
java 体验AI代码助手 代码解读复制代码import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
@Autowired
private AppProperties appProperties;

public void doSomething() {
    String appName = appProperties.getName();
    int appVersion = appProperties.getVersion();
    // 使用配置信息进行业务逻辑处理
}

}

  1. 自定义启动 Banner
    每次启动 Spring Boot 应用,看到默认的启动 Banner 是不是觉得有点单调?其实,我们可以自定义这个 Banner,让启动界面充满个性。只需在src/main/resources目录下创建一个banner.txt文件,在里面写入你想要展示的内容,比如公司 logo、项目名称、版本号等。
    例如:
    markdown 体验AI代码助手 代码解读复制代码 ____ _ _ _
    | _ | | () | | |
    | |
    ) | __ _ | | _ _ __ ___ __ | |
    | _ < / |/ __| '_ \| | '_ _ \ / ` | __|
    | |
    ) | (
    | | (
    | | | | | | | | | | (| | |
    |__/ _,|_
    _|| |||| || ||_,|__|

这样,下次启动应用时,就能看到自定义的 Banner 。
3. 排除不必要的自动配置
Spring Boot 的自动配置功能十分强大,但有时我们并不需要加载所有的自动配置组件,这时候可以使用@SpringBootApplication的exclude属性来排除不需要的模块,从而加快启动速度,减少内存占用。
比如,若项目中不使用数据库相关的自动配置,可以这样写:
arduino 体验AI代码助手 代码解读复制代码import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}

通过排除DataSourceAutoConfiguration,Spring Boot 在启动时就不会尝试加载数据库相关的配置和组件,启动过程更加轻量化。
4. CommandLineRunner 执行启动任务
当 Spring Boot 应用启动完成后,有时我们需要执行一些初始化任务,比如初始化数据库、加载默认数据等。这时,CommandLineRunner接口就能派上用场。
创建一个实现CommandLineRunner接口的组件:
java 体验AI代码助手 代码解读复制代码import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class StartupRunner implements CommandLineRunner {
@Override
public void run(String... args) throws Exception {
System.out.println("Application started, running initial tasks...");
// 在这里编写具体的初始化任务逻辑,比如数据库初始化操作
}
}

Spring Boot 在启动过程中,会检测到实现了CommandLineRunner接口的组件,并在应用启动完成后,按顺序执行它们的run方法。如果有多个CommandLineRunner实现类,可以通过实现org.springframework.core.Ordered接口或使用@Order注解来指定执行顺序。
5. SpringApplicationBuilder 自定义启动方式
SpringApplicationBuilder为我们提供了更多灵活的启动配置方式,通过链式调用,可以在代码层面方便地设置应用的各种属性。
例如,设置应用的运行环境为开发环境,并指定服务器端口为 8081:
arduino 体验AI代码助手 代码解读复制代码import org.springframework.boot.SpringApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

public class MyApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(MyApplication.class)
.profiles("dev")
.properties("server.port=8081")
.run(args);
}
}

这种方式特别适合一些需要根据不同条件灵活配置启动参数的场景,相比在配置文件中设置,在代码中控制更加直观和便捷。

`

posted @ 2025-11-17 18:25  福寿螺888  阅读(10)  评论(0)    收藏  举报