springboot怎么读取自定义properties

Spring Boot可以通过以下方式读取自定义的properties文件:

在application.properties或application.yml文件中添加自定义属性,例如:

custom.setting=value

在application.properties或application.yml中指定自定义properties文件的路径,例如:

spring.config.location=classpath:/custom.properties

使用@Value注解或Environment对象来读取自定义属性,例如:

@Value("${custom.setting}")
private String customSetting;
@Autowired
private Environment env;
String customSetting = env.getProperty("custom.setting");

创建一个@Configuration类,使用@PropertySource注解指定自定义properties文件的路径,并使用@Value注解注入属性,例如:

@Configuration
@PropertySource("classpath:custom.properties")
public class CustomConfig {
    @Value("${custom.setting}")
    private String customSetting;
    // getters and setters
}

使用以上方法,Spring Boot可以读取自定义的properties文件并注入到应用程序中。