title: How to populate @Query value for native queries in Spring boot Repository when Queries are stored as external files
body: <p>Similar question has been asked and answered back in 2015 <a href="https://stackoverflow.com/questions/27902242/how-to-store-query-sql-in-external-file-for-crudrepository">here</a>.</p>
  
  <p>However , rather than storing queries in XML files , I would like to store them in external .sql files and read query from there.
  To achieve this in our spring boot application - 2.1.5.RELEASE with Java 8 , we are storing queries in resources folder as below </p>
  
  <p><code>src/main/resources
     - queries
       - A.sql
       - B.sql</code></p>
  
  <p>To read the above queries , I'm reading them in QueryReader class as below</p>
  
  <pre><code>@Component
  public class QueryReader {
  
      public String getQueryFromFile(String fileName) {
          System.out.println("getQueryFromFile() : " + fileName);
          return this.convertInputStreamToString(this.getTemplateAsStream(fileName));
      }
  
  
  
  
       private InputStream getTemplateAsStream(String queryFileName){
              ClassLoader classLoader = getClass().getClassLoader();
              InputStream iStream =classLoader.getResourceAsStream("queries/" + queryFileName);
  
              return iStream;
          }
  }
  </code></pre>
  
  <p>And to use it anywhere in the code , i'm having below class , so that i can call it's methods</p>
  
  <pre><code>@Component
  public class MyQueries {
  
  
      @Autowired
      private QueryReader qfr;
  
      public String queryErroredRecords() {
          return qfr.getQueryFromFile("A.sql");
      }
  </code></pre>
  
  <p>While using it with JDBCTemplate , this works as expected but when I'm trying to use this method from @Query annotation from Repository , i'm not able to do so with errors as below.</p>
  
  <p>Repository Code</p>
  
  <pre><code>@Repository
  public interface AdminRepository extends JpaRepository&lt;OrderBackupRecordEO, BigDecimal&gt;{
  
      @Autowired
      public MyQueries queries;
  
  
  @Query(value = queries.queryErroredRecords() , nativeQuery= true)
      List&lt;String&gt; findX();
  }
  </code></pre>
  
  <p>Above repository is giving errors as below :</p>
  
  <p>1.The value for annotation attribute Query.value must be a constant expression (For @Query annotation)</p>
  
  <p>2.The blank final field queries may not have been initialized (For @Autowired annotation)</p>
  
  <p>How can I make it work? </p>