Spring 3 中用于访问 Service 的 Util 类
在 Spring 3 中,不可能在静态字段或方法中设置@Autowired,所以因为我想声明一个实用程序类,例如:
public class SchoolYearServiceUtil {
private static SchoolYearService schoolYearService;
public static SchoolYear getSchoolYear(Long id) {
return schoolYearService.get(id);
}
}
以避免必须在其中的任何地方(jsp、命令类...)注入 schoolYearService需要它。在这种情况下,我不需要由 SchoolYearServiceUtil 实现的接口。
我不想通过代码初始化对象,而是获得与 Spring 相同的实例。
将 getSchoolYear 实现为静态方法的最佳选择是什么?
谢谢。
这在概念上是错误的吗?:
@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);
}
}
我必须确保只有 Spring 才会调用构造函数,并且构造函数不会在其他地方被调用,这就是为什么我将构造函数声明为私有的。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我完全支持斯卡夫曼的评论。您不需要使用 DI 的
static
字段。您只需定义一个单例范围的 bean(默认)。有一种静态获取 bean 的方法,但您应该注意它不适合在常规情况下使用。 (有一些有效的应用程序)。它是使用
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)
您注意到需要传递
ServletContext
参数。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.