title: Injecting a Spring Data Rest repository into a utility class
body: <p>After searching for a day, trying all configurations (app context) and annotations (autowire, inject, component, etc), I cannot seem to get my pojo class to inject a working repository successfully -- the value is always null.
  I am starting to wonder whether injecting into anything but a controller goes contrary to the architecture of spring data rest.</p>
  
  <p>Here is my application context (taken from spring docs):</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:jpa="http://www.springframework.org/schema/data/jpa"
         xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"&gt;
  
      &lt;jpa:repositories base-package="com.test.springservice"/&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>My main class that draws this context in:</p>
  
  <pre><code>package com.test.springservice;
  
  import ...
  
  @Configuration
  @ComponentScan
  @EnableJpaRepositories
  @Import(RepositoryRestMvcConfiguration.class)
  @ImportResource("classpath:WEB-INF/applicationContext.xml")
  @EnableAutoConfiguration
  @PropertySource("application.properties")
  public class Application {
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  }
  </code></pre>
  
  <p>My working repository and model:</p>
  
  <pre><code>package com.test.springservice.greek;
  
  import ...
  
  @RepositoryRestResource
  public interface GreekLetterRepository extends CrudRepository&lt;GreekLetter, Integer&gt; {
      @Query("SELECT l FROM GreekLetter l WHERE l.translit Like :translit% ORDER BY length(l.translit) Desc")
      public List&lt;GreekLetter&gt; findByTranslitStartsWith(@Param("translit") String translit);
  
  }
  
  package com.test.springservice.greek.model;
  
  import ....
  
  @Entity
  @Table(name="letters", catalog="greek")
  public class GreekLetter extends Letter {
      public GreekLetter() {}
      public GreekLetter(String name, String translit, String present, String types) { super(name,translit,present,types); }
  }
  </code></pre>
  
  <p>And finally, my class that I cannot inject the repository into:</p>
  
  <pre><code>package com.test.springservice.greek.model;
  
  import ...
  
  public class GreekString extends Letters {
  
      @Autowired
      public GreekLetterRepository repository; // this is null (class not managed by container)
  
      public GreekString(String str) {
          super();
          setTranslit(str);
          if (this.getTranslit().equals(this.getPresent())) { setPresent(str); }
      }
      public void setTranslit(String str) { // litterates from str as translit
          List&lt;Letter&gt; lets = new ArrayList&lt;Letter&gt;();
          for (int i = 0; i &lt; str.length(); i++) {
              String partialWord = str.substring(i);
              String partialWord0 = partialWord.substring(0,1);
              List&lt;GreekLetter&gt; potentialMatches = repository.findByTranslitStartsWith(partialWord0);
              ....
          }
          ....        
      }
      ....
  }
  </code></pre>
  
  <p>Does anyone see a basic flaw in the approach? </p>
  
  <p>Thanks in advance.</p>