SpringBoot自定义application配置文件及使用
    		       		warning:
    		            这篇文章距离上次修改已过420天,其中的内容可能已经有所变动。
    		        
        		                
                在Spring Boot中,可以创建自定义的.properties或.yml配置文件来覆盖默认的application.properties或application.yml。
步骤如下:
- 创建自定义配置文件,例如custom.properties或custom.yml。
- 在Spring Boot应用中加载自定义配置文件。
以下是一个使用自定义配置文件的例子:
- 创建custom.properties文件:
custom.property=value或者创建custom.yml文件:
custom:
  property: value- 在Spring Boot应用中加载自定义配置文件:
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
@Component
public class CustomConfig {
 
    @Value("${custom.property}")
    private String customProperty;
 
    public String getCustomProperty() {
        return customProperty;
    }
}- 在application.properties或application.yml中引用自定义配置文件:
spring.config.import=classpath:custom.properties或者
spring:
  config:
    import: classpath:custom.yml确保自定义配置文件位于Spring Boot应用的类路径下,例如src/main/resources目录。
当Spring Boot应用启动时,它会加载application.properties或application.yml中指定的自定义配置文件,并使其属性可在Spring应用上下文中注入和使用。
评论已关闭