[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
springboot虽然免除了绝大部分的配置文件设置,但是对于一些特定情况,还是有定义配置文件的需要。
一、配置文件的格式
SpringBoot可以识别.yml和.properties两种格式的配置文件。如果是自带classpath下的application.properties和application.yml,这两种都可以被SpringBoot自动识别并加载。
在application.properties,通过spring.profiles.active=xxx可以指定不同环境激活不同的配置文件,设置的激活参数dev, test, prod,对应的配置文件有:
application-dev.properties
application-test.properties
application-prod.properties
如果是自定义的配置文件,最好还是使用properties格式,因为SpringBoot中暂时还并未提供手动加载yml格式文件的功能。
二、自定义配置文件
1、自定义配置文件,如:
url.name=editMessage url.path=100120171116031838
2、定义获取配置文件的bean
@Component @ConfigurationProperties(prefix = "url")
@PropertySource("classpath:config.properties",encoding = "utf-8") @Data public
class PropertiesEnv { private String name; private String path; }
3、通过注入配置文件的Bean使用属性
@Autowired PropertiesEnv propertiesEnv ;
4、用@Value注解使用
5、使用Environment
先注入@Autowired private Environment env;
然后env.getProperty("url.name")
6、自实现PropertiesLoaderUtils
7、SpringBoot中使用@Value()只能给普通变量注入值,不能直接给静态变量赋值,如果要赋值给静态变量,可以使用