2

有没有办法在不使用 DispatcherPortlet 的情况下使用 spring 开发 portlet?我想为 UI 使用其他技术,主要是 Vaadin。Spring用于DI和其他东西。Portlet 端有类似 ContextLoaderListener 类的东西吗?

4

3 回答 3

1

查看Spring 文档:您可以创建ApplicationContext如下:

ApplicationContext context =
    new ClassPathXmlApplicationContext(new String[] {"services.xml", "daos.xml"});

引用类路径上的 xml 文件。

然后,您可以通过调用context.getBean("beanName"). 不需要 Spring MVC。

于 2010-07-30T06:10:52.437 回答
0

我希望得到比诺埃尔给出的更详细的答案。也许有一些最佳实践?这是我目前的解决方案:

将 xml 文件位置作为初始化参数提供给我的 portlet。我的 init 方法看起来像这样

@Override
public void init(PortletConfig config) throws PortletException {
    super.init(config);
    String configLocations = config.getInitParameter("contextConfigLocation");
    ClassPathXmlApplicationContext springContext = new ClassPathXmlApplicationContext();
    springContext.setConfigLocation(configLocations);
    springContext.refresh();
    config.getPortletContext().setAttribute(APPLICATION_CONTEXT_ATTRIBUTE, springContext);
}

现在我可以在每次需要时通过 PortletContext 访问我的 applicationContext。

(ApplicationContext) portalContext.getAttribute(APPLICATION_CONTEXT_ATTRIBUTE);

APPLICATION_CONTEXT_ATTRIBUTE 只是我编的字符串常量。我仍然愿意寻求更好的解决方案。

于 2010-07-30T11:51:04.830 回答
0

您可以使用 PortletApplicationContextUtils 来检索 Web 应用程序上下文:

ApplicationContext ctx = PortletApplicationContextUtils.getWebApplicationContext(getPortletContext());

然后你只需要在你的 web.xml 上添加一些配置。

于 2011-01-28T07:43:42.710 回答