1. 程式人生 > >springboot讀取自定義配置檔案

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

執行後控制檯即可顯示屬性檔案的內容