在Spring Boot项目中,application.yml
或 application.properties
文件中的配置参数可以通过以下几种方式在代码中使用:
前提导入SpringBoot的依赖!!!
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- Spring Boot起步依赖,用于创建RESTful控制器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1. 使用 @Value
注解
@Value
注解可以直接将配置文件中的值注入到字段中。
# application.yml
app:
name: MyApp
version: 1.0.0
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyAppConfig {
@Value("${app.name}")
private String appName;
@Value("${app.version}")
private String appVersion;
public void printConfig() {
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
}
}
2. 使用 @ConfigurationProperties
注解
@ConfigurationProperties
注解可以将配置文件中的一组属性绑定到一个Java对象中。
# application.yml
app:
name: MyApp
version: 1.0.0
description: This is a Spring Boot application
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private String version;
private String description;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void printConfig() {
System.out.println("App Name: " + name);
System.out.println("App Version: " + version);
System.out.println("App Description: " + description);
}
}
3. 使用 Environment
对象
Environment
对象可以用来获取配置文件中的属性值。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class MyAppConfig {
@Autowired
private Environment env;
public void printConfig() {
String appName = env.getProperty("app.name");
String appVersion = env.getProperty("app.version");
System.out.println("App Name: " + appName);
System.out.println("App Version: " + appVersion);
}
}
4. 使用 @PropertySource
注解
如果有自定义的配置文件,可以使用 @PropertySource
注解来加载该文件,并使用 @Value
或 Environment
来获取属性值。
# custom.yml
custom:
property: Custom Value
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource("classpath:custom.yml")
public class CustomConfig {
@Value("${custom.property}")
private String customProperty;
public void printConfig() {
System.out.println("Custom Property: " + customProperty);
}
}
5. 使用 @ConfigurationProperties
和 @EnableConfigurationProperties
结合
将配置属性绑定到一个独立的类中,可以使用 @ConfigurationProperties
和@EnableConfigurationProperties
结合的方式。
# application.yml
app:
name: MyApp
version: 1.0.0
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "app")
public class AppConfig {
private String name;
private String version;
// Getters and Setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(AppConfig.class)
public class MyAppConfig {
private final AppConfig appConfig;
public MyAppConfig(AppConfig appConfig) {
this.appConfig = appConfig;
}
public void printConfig() {
System.out.println("App Name: " + appConfig.getName());
System.out.println("App Version: " + appConfig.getVersion());
}
}
总结
-
@Value
适用于单个属性的注入。 -
@ConfigurationProperties
适用于将一组属性绑定到一个对象中。 -
Environment
适用于动态获取属性值。 -
@PropertySource
适用于加载自定义配置文件。 -
@ConfigurationProperties
和@EnableConfigurationProperties
结合适用于将配置属性绑定到独立的类中。
番外:
如果你有一个进行SpringBoot的启动类,你现在需要调用另外一个类里面的方法,那么该怎么让Spring去调用这个类呢?
情景再现:在 Spring Boot 项目中,如果你有两个类:ApplicationRun
(Spring Boot 启动类)和 MyAppConfig
(配置类),并且你想在项目启动时运行 MyAppConfig
中的 printAppInfo
方法,怎么做??
方法 1:在 MyAppConfig
中使用 @PostConstruct
注解
@PostConstruct
注解可以标记一个方法,在 Spring 容器初始化 Bean 后自动执行。
代码示例
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
@Configuration
public class MyAppConfig {
public void printAppInfo() {
System.out.println("应用信息:MyAppConfig");
}
@PostConstruct
public void init() {
printAppInfo(); // 在 Bean 初始化后自动调用
}
}
说明
-
@Configuration
注解表示这是一个配置类,Spring 会将其加载为 Bean。 -
@PostConstruct
注解标记的方法会在 Bean 初始化后自动执行。
方法 2:在 ApplicationRun
中手动调用 printAppInfo
方法
在 Spring Boot 启动类中,可以通过 ApplicationContext
获取 MyAppConfig
的实例,然后调用其方法。
代码示例
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class ApplicationRun {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(ApplicationRun.class, args);
// 获取 MyAppConfig 的实例
MyAppConfig myAppConfig = context.getBean(MyAppConfig.class);
// 调用 printAppInfo 方法
myAppConfig.printAppInfo();
}
}
说明
-
ApplicationContext
是 Spring 的上下文对象,可以通过它获取容器中的 Bean。 -
context.getBean(MyAppConfig.class)
获取MyAppConfig
的实例。
方法 3:实现 CommandLineRunner
或 ApplicationRunner
接口
CommandLineRunner
或 ApplicationRunner
是 Spring Boot 提供的接口,可以在应用启动后执行一些逻辑。
代码示例
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
public class MyAppConfig implements CommandLineRunner {
public void printAppInfo() {
System.out.println("应用信息:MyAppConfig");
}
@Override
public void run(String... args) throws Exception {
printAppInfo(); // 在应用启动后执行
}
}
说明
-
CommandLineRunner
接口的run
方法会在 Spring Boot 应用启动后执行。 -
可以将
MyAppConfig
实现CommandLineRunner
接口,并在run
方法中调用printAppInfo
。
方法 4:使用 @EventListener
监听应用启动事件
通过监听 Spring Boot 的 ApplicationReadyEvent
事件,在应用启动完成后执行逻辑。
代码示例
import org.springframework.context.event.EventListener;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAppConfig {
public void printAppInfo() {
System.out.println("应用信息:MyAppConfig");
}
@EventListener(ApplicationReadyEvent.class)
public void onApplicationReady() {
printAppInfo(); // 在应用启动完成后执行
}
}
说明
-
@EventListener
注解用于监听 Spring 的事件。 -
ApplicationReadyEvent
是 Spring Boot 应用启动完成后触发的事件。
方法 5:在 MyAppConfig
中注入 ApplicationRunner
或 CommandLineRunner
Bean
如果不想直接实现 CommandLineRunner
接口,可以在 MyAppConfig
中定义一个 CommandLineRunner
或 ApplicationRunner
的 Bean。
代码示例
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyAppConfig {
public void printAppInfo() {
System.out.println("应用信息:MyAppConfig");
}
@Bean
public CommandLineRunner commandLineRunner() {
return args -> {
printAppInfo(); // 在应用启动后执行
};
}
}
说明
-
通过
@Bean
注解定义一个CommandLineRunner
的 Bean。 -
Spring Boot 会自动执行这个 Bean 的逻辑。
总结
方法 | 适用场景 |
---|---|
@PostConstruct | 适合在 Bean 初始化后立即执行的逻辑。 |
手动调用 printAppInfo | 适合需要在启动类中显式调用某些方法的场景。 |
CommandLineRunner | 适合在应用启动后执行一些初始化逻辑。 |
@EventListener | 适合监听应用启动事件,执行一些启动后的逻辑。 |
定义 CommandLineRunner Bean | 适合将启动逻辑封装在配置类中,而不是直接实现接口。 |
根据你的需求选择合适的方式即可!如果只是简单的打印信息,推荐使用 @PostConstruct
或 CommandLineRunner
。