views:

46

answers:

2

Hi

Is there a way to develop portlets with spring without using the DispatcherPortlet? I want to use other technologies for the UI, mainly Vaadin. Spring is used for DI and other stuff. Is there something similar to ContextLoaderListener class in the Portlet side?

+1  A: 

Looking at the Spring documentation: You can create an ApplicationContext as follows:

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

Referencing xml files on your classpath.

You can then get beans by calling context.getBean("beanName"). No need for Spring MVC.

Noel M
A: 

I was hoping for a more detailed answer than what Noel gave. Maybe there is some best practice to this? Here is my current solution:

Give the xml file locations as a init parameter to my portlet. My init method looks something like this

@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);
}

Now I can access my applicationContext through PortletContext every time I need.

(ApplicationContext) portalContext.getAttribute(APPLICATION_CONTEXT_ATTRIBUTE);

APPLICATION_CONTEXT_ATTRIBUTE is just string constant that I made up. I'm still open for better solutions.

palto