1. 新建staticCls.properties
com.md.name="abc"
com.md.want="def"
#该随机值只是每次启动时生成一个固定值
com.md.id=${random.int}
2. 新建StaticCls.java
@Configuration
@ConfigurationProperties(prefix = "com.md")
@PropertySource("classpath:staticCls.properties")
public class StaticCls {
private Long id;
private String name;
private String want;
get()/set()
}
3. 在SpringBoot启动类上加注解
@SpringBootApplication
@ComponentScan(basePackages = "com.yweb")
/*配置类*/
@EnableConfigurationProperties({StaticCls.class})
public class Entry {
public static void main(String[] args) throws Exception {
SpringApplication.run(Entry.class, args);
}
}
本文介绍如何在Spring Boot项目中使用属性文件staticCls.properties进行配置,并通过StaticCls.java类读取这些配置。具体步骤包括创建属性文件、定义@ConfigurationProperties类及启用配置。
3707

被折叠的 条评论
为什么被折叠?



