springboot读取自定义配置文件

 


        如题:通过网上查找本来想使用@ConfigurationProperties(prefix = "speed.url",locations = "classpath:config/speedserver.properties")。但貌似locations不能使用了。使用了如下方法读取:

       入口函数加@PropertySource引入自定义的属性文件

      

@SpringBootApplication
@PropertySource("config/speedserver.properties")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}


     项目中就可以直接引用属性文件的内容

@RestController public class SpeedMeasureController {

    private static final String template = "Hello, %s!";     private final AtomicLong counter = new AtomicLong();         @Value("${speed.url.000}")     private String url000;    

    @RequestMapping("/speedmeasure")     public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {      System.out.println("-----------"+url000);         return new Greeting(counter.incrementAndGet(),                             String.format(template, name));     } }

运行后控制台即可显示属性文件的内容

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值