代码之家  ›  专栏  ›  技术社区  ›  palto

在没有springmvc的portlet中使用Spring

  •  2
  • palto  · 技术社区  · 14 年前

    有没有一种不用DispatcherPortlet就用spring开发portlet的方法?我想使用其他的UI技术,主要是Vaadin。弹簧用于DI和其他东西。Portlet端是否有类似于ContextLoaderListener类的内容?

    3 回复  |  直到 14 年前
        1
  •  1
  •   Noel M    14 年前

    看着 the Spring documentation :您可以创建 ApplicationContext 具体如下:

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

    引用类路径上的xml文件。

    context.getBean("beanName") . 不需要springmvc。

        2
  •  0
  •   palto    14 年前

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

    将xml文件位置作为init参数提供给我的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);
    

    应用程序上下文属性只是我编的字符串常量。我仍然对更好的解决方案持开放态度。

        3
  •  0
  •   Louie    14 年前

    可以使用PortletApplicationContextUtils检索web应用程序上下文:

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