spring expression read file content

前端 未结 2 1586
傲寒
傲寒 2020-12-30 12:13

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         


        
相关标签:
2条回答
  • 2020-12-30 12:21

    There is a similar question answered here which use Java configuration.

    @Configuration
    public class Spring {
    
        @Value("classpath:choice-test.html")
        private Resource sampleHtml;
    
        @Bean
        public String sampleHtmlData() {
            try(InputStream is = sampleHtml.getInputStream()) {
                return IOUtils.toString(is);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 12:40

    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

    0 讨论(0)
提交回复
热议问题