Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Gary Pinkham
Multiple Spring Portlets Shared beans?
March 29, 2013 3:14 PM
Answer

Gary Pinkham

Rank: New Member

Posts: 5

Join Date: March 29, 2013

Recent Posts

Curious if there are any examples of multiple Spring MVC portlets but with shared beans using component scan and autowiring? I currently have two portlets in a single war and the component scans for the service beans is in both portlets portletContext file.. I had thought that the appContext referenced in the web.xml was the parent context of the portletContexts and therefore if I defined the component scan in that file those beans could be used for autowiring in the controllers defined in the portletContexts.. This didn't work.. wondering if I need to define all the connections manually and not use component scan and autowiring.. or maybe it's just not possible..

thanks in advance!
Gary
David H Nebinger
RE: Multiple Spring Portlets Shared beans?
March 30, 2013 6:05 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 11261

Join Date: September 1, 2006

Recent Posts

Each portlet has a portlet context, even though they are in the same war file.

Try using a spring context loader listener at the servlet level to instantiate your beans.
Gary Pinkham
RE: Multiple Spring Portlets Shared beans?
March 30, 2013 6:12 AM
Answer

Gary Pinkham

Rank: New Member

Posts: 5

Join Date: March 29, 2013

Recent Posts

Thanks David. . I did actually have the app context defined in the context listener in the web.xml.. let me provide some more details..

web.xml -> ContextLoaderListener -> main application context xml file.. defines services via component scan for @service

portlet.xml -> multiple DispatcherPortlets -> each pointing to it's own portlet context xml file.. defines the controller and view resolver (have moved that up to with no luck).. The controller is using autowiring to find it's services..

This works fine with the @service component scan is in each portlet context file.. but I have the service instances multiple times.. when I moved the component scan up to the app context the autowiring breaks..

question. should I "import" the application context into the portlet context via:
1<import resource="classpath:META-INF/spring/applicationContext.xml" />
Gary Pinkham
RE: Multiple Spring Portlets Shared beans?
March 31, 2013 4:32 AM
Answer

Gary Pinkham

Rank: New Member

Posts: 5

Join Date: March 29, 2013

Recent Posts

ok.. not sure what I had done wrong the first time but it worked this time.. I did notice if I redeploy my war into the liferay deploy folder it fails to autowire. . but if I stop the server.. delete the app from the webapps folder and redeploy, restart (twice) the autowiring works perfectly...
Gary Pinkham
RE: Multiple Spring Portlets Shared beans?
March 31, 2013 6:01 PM
Answer

Gary Pinkham

Rank: New Member

Posts: 5

Join Date: March 29, 2013

Recent Posts

a coworker discovered this (nice catch Thomas).. http://issues.liferay.com/browse/LPS-29103 this is what was causing the issues we were having with the Spring MVC Portlets and our shared beans..

Thanks
Pritesh Shah
RE: Multiple Spring Portlets Shared beans?
November 6, 2013 11:18 PM
Answer

Pritesh Shah

Rank: Junior Member

Posts: 31

Join Date: July 4, 2012

Recent Posts

Hello,

I am facing the same issue.
I have bean defined in context file which is loaded under ContextLoaderListener in web.xml by specifying contextConfigLocation. This bean is reference in portlet controllers and some servlets.

As portlet context is loaded first in 6.1.2 bean defined in context file which would be loading in ContextLoaderListener is not yet loaded so I am not able to initialize controller having those bean references.

I checked the http://issues.liferay.com/browse/LPS-29103 and it seems to be the same issue.

Can anyone please provide any workaround for the same?
I am using LR v6.1.20EE

Thanks in advance!!!