Spring 3 中用于访问 Service 的 Util 类

发布于 2024-10-07 17:47:27 字数 1088 浏览 8 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2024-10-14 17:47:27

我完全支持斯卡夫曼的评论。您不需要使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文