title: How to use autowiring in classes where the method can be called for outside the spring context
body: <p>Converting my web-application to Spring. Autowiring is successful in cases where it is initiated in Spring context but fails in other situations, as it should.</p>
  
  <p>I have a <code>MangaBean</code> which has a property named <code>genre</code>. Genre's value should be one from the excepted set of genres. I have <strong>placed the validations in the bean itself</strong>. Something like this:</p>
  
  <pre><code>    @Component
  public class MangaBean{
      private String title;
      private String author;
      private String isbn;
      private String genre;
  
      //getters
  
      public void setTitle(String title){
          //validations
      }
  
      public void setGenre(String genre){
          boolean result=MangaUtil.verifyGenre(genre);
          if(result){
              this.genre=genre;
          }else{
              this.genre=null;
          }
      }
  }
  </code></pre>
  
  <p>The util calls the method that fetches set of genres from the table and validates the provided genre. </p>
  
  <pre><code>@Component
  public class MangaUtil{
      @Autowired
      MangaDao mDao;
  
      public static boolean verifyGenre(String genre){
          List&lt;String&gt; genres=mDao.getGenresList();   //null pointer exception 
              //do validations
      }
  }
  </code></pre>
  
  <p><code>MangaDao</code> contains an autowired <code>NamedParameterJDBCTemplate</code> which fetches genres from DB.</p>
  
  <p>Code for MangaDao:</p>
  
  <pre><code>@Repository
  public class MangaDao{
      @Autowired
      private NamedParameterJdbcTemplate template;
  
      public List&lt;String&gt; getGenresList(){
          String query="select genres from manga_table";
          Map&lt;String,String&gt; paramMap=new HashMap&lt;String, String&gt;();
          return template.queryForList(query, paramMap, String.class);
      }
  }
  </code></pre>
  
  <p>In the above arrangement, the call to MangaUtil works fine when I autowire MangaUtil as well. Example:</p>
  
  <pre><code>@Component
  public class MangaBean{
      @Autowired
      MangaUtil mangaUtil;
      private String title;
      private String author;
      private String isbn;
      private String genre;
  
      //getters
  
      public void setTitle(String title){
          //validations
      }
  
      public void setGenre(String genre){
          boolean result=mangaUtil.verifyGenre(genre);
          if(result){
              this.genre=genre;
          }else{
              this.genre=null;
          }
      }
  } 
  </code></pre>
  
  <p>But if I use autowiring inside a bean, the autowire would fail in cases I instantiate the bean myself like <code>MangaBean mb=new MangaBean()</code>. Please advice for a situation like this. <strong>I want to call the validator method from my bean without any autowiring in bean itself. Is it possible?</strong>. If not, is there any way I can store a list of genres and use it in the bean to validate my data. Please advice.</p>