Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring expression read file content

How to use spring expression to read file content and put it into a string?

I would like to do the following.

For example,

@Value("classpath:myquery.sql")
File f;

@Value("#{org.apache.commons.io.FileUtils.readFileToString(f)}")
String sql;

Or even better

@Value("#{FileUtils.readFileToString(classpath:myquery.sql)}")
String sql;

However, none of above code work.

Just to mention I am using spring version 3.2.0

Thanks.

like image 876
Timmy Chiu Avatar asked Feb 01 '13 03:02

Timmy Chiu


1 Answers

Finally got the answer myself...

@Value("#{T(org.apache.commons.io.FileUtils).readFileToString(" +
    "T(org.springframework.util.ResourceUtils).getFile('classpath:myquery.sql')" +
    ")}")
String sql;

sql is now default filled by the exact content of myquery.sql under classpath

like image 172
Timmy Chiu Avatar answered Nov 09 '22 13:11

Timmy Chiu