1

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:

public class SchoolYearServiceUtil {
    private static SchoolYearService schoolYearService;

    public static SchoolYear getSchoolYear(Long id) {
        return schoolYearService.get(id);
    }
}

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.

I don't want to have to initialize the object through code but getting the same instance as the Spring's one.

Which would be the best option to implement the getSchoolYear as a static method?

Thanks.

Would this be conceptually wrong?:

@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);
}
}

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.

  • 3
    You're missing the point of dependency injection, which *is* to inject the service everywhere that it's needed. What you're describing is the Service Locator pattern, which is the opposite of DI. Spring will not help you there. – skaffman Dec 14 '10 at 17:15
  • @Juan: It's not conceptually "wrong", it's just not the way Spring does things. Spring is built around DI, and what you're describing is not DI. – skaffman Dec 14 '10 at 17:38
  • @skaffman What do you think about the solution I posted?? I understand what u say but I want the util class to be able to call the Service initialized by Spring. So both, calls to Service where this is injected and calls to Service through ServiceUtil are processed by the same instance. –  Dec 14 '10 at 17:38
  • @skaffman I fully understand what u are telling me, but I can't see a better solution –  Dec 14 '10 at 17:49
  • @Juan Carlos Blanco Martínez - the question is - why do you need to access the bean in a static way? – Bozho Dec 14 '10 at 17:55
  • @Bozho Because I want to be able to access the util class from Spring Commands, jsps... The best solution for u seems to be injecting Service to all those places. –  Dec 14 '10 at 18:00
  • Yes. But the best choice is not to access it there at all :) – Bozho Dec 14 '10 at 18:02
  • I always try it, but sometimes not possible :) –  Dec 14 '10 at 18:07

1 Answers1

1

I fully support skaffman's comment. You don't need static fields with DI. You just define a bean of scope singleton (default).

There is a way to obtain a bean statically, but you should be aware that it is not to be used in regular situations. (there are some valid applications). It is to use the WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)

You notice that you need to pass a ServletContext argument.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140