@PropertySource注解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gybshen/article/details/85288002

@PropertySource是Spring boot为了方便引入properties配置文件提供的一个注解,可以标注在SpringBoot的启动类上,还可以标注在配置类(使用@Configuration标注的类)上。

例如:@PropertySource(value = {"classpath:box.properties"}),将classpath下的box.properties,注入到Spring环境中,使用@Value("${key}")取值。

示例:

box.properties文件:

# 工具箱配置

preserveFilePath=/box/webserver/uploadfile/preservefile/

注入:

@SpringBootApplication
@PropertySource(value = {"classpath:box.properties"})
public class ToolboxApiApplication {

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

}

取值: 

@RestController
public class DeleteFileController {
    @Value("${preserveFilePath}")
    private String preserveFilePath;

    @GetMapping("/deleteFile")
    public void test(){
        System.out.println(preserveFilePath);
    }
}

猜你喜欢

转载自blog.csdn.net/gybshen/article/details/85288002