title: Util class for accesing a Service in Spring 3
body: <p>In Spring 3 it is not possible to set @Autowired in either static fields or methods, so since I want to declare an utility class such as:</p>
  
  <pre><code>public class SchoolYearServiceUtil {
      private static SchoolYearService schoolYearService;
  
      public static SchoolYear getSchoolYear(Long id) {
          return schoolYearService.get(id);
      }
  }
  </code></pre>
  
  <p>to avoid having to inject the schoolYearService everywhere (jsp, command class...) in which I need it. In this case, I don't need an interface to be implemented by SchoolYearServiceUtil.</p>
  
  <p>I don't want to have to initialize the object through code but getting the same instance as the Spring's one. </p>
  
  <p>Which would be the best option to implement the getSchoolYear as a static method?</p>
  
  <p>Thanks.</p>
  
  <p>Would this be conceptually wrong?:</p>
  
  <pre><code>@Component
  public class SchoolYearServiceUtil {
  
  private static SchoolYearService schoolYearService;
  
  @Autowired(required = true)
  private SchoolYearServiceUtil(@Qualifier("schoolYearServiceImpl") SchoolYearService schoolYearService) {
      SchoolYearServiceUtil.schoolYearService = schoolYearService;
  }
  
  public static SchoolYearService getSchoolYearService() {
      return schoolYearService;
  }
  
  public static SchoolYear getSchoolYear(Long id) {
  
      return getSchoolYearService().get(id);
  }
  }
  </code></pre>
  
  <p>I would have to make sure that only Spring calls once the constructor and the constructor is called nowhere else, that's why I declared the constructor as private.</p>