views:

182

answers:

1

Hi,

I have a portlet application. It is configured using Spring framework IoC container. I am using org.springframework.web.context.ContextLoaderListener to load my context.

I have an application context at root level (applicationContext.xml) and a portlet specific context (MyPortlet-portlet.xml).

I have a portlet of type org.springframework.web.portlet.DispatcherPortlet which is wired up to a Controller. In the Controller I want to access one of the beans (e.g. bean with id "myBean") I have defined in my portlet specific context. I have tried

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

However only the beans in my application context are available here - none of my beans in my portlet specific context are available.

Is there a way to access the beans in my portlet specific context?

Thanks

+1  A: 

Firstly, can't you just wire in the bean to your controller in the normal way, rather than retrieving it programmatically?

Failing that, you should realise that getWebApplicationContext() gets a reference to the root webapp context, not the servlet app context:

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

If your controller needs a handle on its own context, then it should implement ApplicationContextAware or BeanFactoryAware, or it can use @Autowired ApplicationContext if you want to use autowiring.

skaffman
Hi, the reason I am not wiring my bean in the normal way is because the Controller has a Service which is a singleton, which needs a Processor which is not a singleton. So I am getting the Processor in the Controller every time and passing it to the Service. Is there a better way to configure this in the XML context file?
aos
@Aos: Yes, I described the better way in the answer - use `ApplicationContextAware` etc
skaffman
Okay - thank you for your help. I have my controller working now.
aos