【问题标题】:Using Spring managed DAO without controller?在没有控制器的情况下使用 Spring 托管 DAO?
【发布时间】:2009-11-25 19:10:08
【问题描述】:

我试图让我的 Portlet 与我配置为 spring bean 的一些 DAO 对象一起工作。我在我的 WEB-INF 中包含了一个 application.xml,将它添加到我的 web.xml,配置了 hibernater 支持,一个数据源。一切似乎都很好,部署后我看到来自 spring 的上下文加载消息。现在我正在尝试使用

将一个 dao 对象注入到我的常规 portlet(扩展 GenericPortlet)中

[at]自动连线

注解。但是对象仍然是空的。没有常规的 spring mvc 控制器就不能使用 spring 管理的 DAO 对象吗?

感谢阅读

【问题讨论】:

    标签: java spring


    【解决方案1】:

    要注入 DAO 的类也必须在 spring 上下文中。 您可以查看this,或者通过访问ApplicationContext 获取DAO,而不是注入bean。 ApplicationContext 可以通过以下方式获得:

    PortletApplicationContextUtils.getRequiredWebApplicationContext(portletContext) 
    

    【讨论】:

    • 好吧,我排除了这样的事情。我不想将我所有的 portlet 重构为 spring mvc portlet。你有通过ApplicationContext获取DAO的例子吗
    • 我明天会检查一下并在这里报告——也许它可以通过 portletcontext 工作。谢谢博卓!
    • PortletApplicationContextUtils.getRequiredWebApplicationContext(portletContext) - 就是这样。
    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 2023-03-25
    • 2018-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多