溫馨提示×

springboot怎么讀取自定義properties

小億
99
2024-08-23 02:06:32
欄目: 深度學習

Spring Boot可以通過以下方式讀取自定義的properties文件:

  1. application.propertiesapplication.yml文件中添加自定義屬性,例如:
custom.setting=value
  1. application.propertiesapplication.yml中指定自定義properties文件的路徑,例如:
spring.config.location=classpath:/custom.properties
  1. 使用@Value注解或Environment對象來讀取自定義屬性,例如:
@Value("${custom.setting}")
private String customSetting;

@Autowired
private Environment env;

String customSetting = env.getProperty("custom.setting");
  1. 創建一個@Configuration類,使用@PropertySource注解指定自定義properties文件的路徑,并使用@Value注解注入屬性,例如:
@Configuration
@PropertySource("classpath:custom.properties")
public class CustomConfig {

    @Value("${custom.setting}")
    private String customSetting;

    // getters and setters
}

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

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女