springboot怎么读取自定义properties

758
2024/8/23 3:32:20
栏目: 深度学习
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

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文件并注入到应用程序中。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: springboot图片上传的方法是什么