@Value加载classpath下的文件

maven工程中,要加载classpath下的文件并以InputStream的形式返回,通常使用的方法是

InputStream inputStream = Test.class.getClassLoader.getResourceAsStream("config.properties");

今天无意中发现另一种方法,如下:

@Value("classpath:/config.properties")
Resource configFile;

得到Resource对象后,使用getInputStream()方法即可获得,当然也可以得到指向文件的File对象,代码如下:

InputStream inputStream = configFile.getInputStream();

File file = configFile.getFile();

 

posted @ 2017-09-20 16:31  sprinkle  阅读(661)  评论(0编辑  收藏  举报