SpringBoot配置文件读取

来源:互联网 发布:怎么在知乎发表文章 编辑:程序博客网 时间:2024/05/18 01:19

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);    }}

原创粉丝点击