如题:通过网上查找本来想使用@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));
}
}
运行后控制台即可显示属性文件的内容