gpt4 book ai didi

java - 以编程方式访问 portlet 特定上下文中的 Bean

转载 作者:太空宇宙 更新时间:2023-11-04 08:54:58 26 4
gpt4 key购买 nike

我有一个 portlet 应用程序。它是使用 Spring 框架 IoC 容器进行配置的。我正在使用 org.springframework.web.context.ContextLoaderListener 来加载我的上下文。

我有一个根级别的应用程序上下文 (applicationContext.xml) 和一个特定于 portlet 的上下文 (MyPortlet-portlet.xml)。

我有一个 org.springframework.web.portlet.DispatcherPortlet 类型的 portlet,它连接到 Controller 。在 Controller 中,我想访问我在 portlet 特定上下文中定义的 bean 之一(例如,id 为“myBean”的 bean)。我已经尝试过

MyBean mybean = (MyBean)PortletApplicationContextUtils.getWebApplicationContext(
getPortletContext()).getBean("myBean")

但是,这里只有我的应用程序上下文中的 Bean 可用 - 我的 Portlet 特定上下文中的 Bean 均不可用。

有没有办法访问我的 portlet 特定上下文中的 Bean?

谢谢

最佳答案

首先,您不能以正常方式将 bean 连接到 Controller ,而不是以编程方式检索它吗?

如果做不到这一点,您应该意识到getWebApplicationContext()获取对根 web 应用程序上下文的引用,而不是 servlet 应用程序上下文:

Find the root WebApplicationContext for this portlet application, which is typically loaded via ContextLoaderListener or ContextLoaderServlet.

如果您的 Controller 需要自己的上下文句柄,那么它应该实现 ApplicationContextAwareBeanFactoryAware,或者如果您想使用 Autowiring ,则可以使用 @Autowired ApplicationContext

关于java - 以编程方式访问 portlet 特定上下文中的 Bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2500782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com