springboo加载resources下的任意文件

来源:互联网 发布:钱宝数据助手怎么退订 编辑:程序博客网 时间:2024/05/16 11:27

有两种方式,一种是通过@PropertySource注解,然后使用@Value逐个注入配置。

@Configuration@PropertySource("classpath:test.properties")public class ELConfig {    @Value("${book.name}")    private String bookName;    //注意!配置一个PropertySourcesPlaceholderConfigurer的Bean    @Bean    public static PropertySourcesPlaceholderConfigurer propertyConfigure() {        return new PropertySourcesPlaceholderConfigurer();    }    public void outputSource() {        System.out.println(bookName);    }}

另外一种方式是通过@ConfigurationProperties注解,通过getter、setter方法注入及获取配置。

properties配置

author.name=listenauthor.age=26
@Component//可以使用locations指定读取的properties文件路径,如果不指定locations就会读取默认的properties配置文件@ConfigurationProperties(prefix = "author")public class AuthorSettings {    private String name;    private Long age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Long getAge() {        return age;    }    public void setAge(Long age) {        this.age = age;    }}最后一种URL url = ResourceUtils.getURL("classpath:xx_private_key.pem");
1 0
原创粉丝点击