掲示板

Liferay Spring ApplicationContext Retrieval error

7年前 に Sourabh Lonikar によって更新されました。

Liferay Spring ApplicationContext Retrieval error

Junior Member 投稿: 42 参加年月日: 16/11/14 最新の投稿
Hi all,
I am trying to get applicationContext spring object using indirect method to call portal-impl.jar class BeanLocatorImpl.java

In its way, below code is giving confusing error message:

/** Roundabout way of beanLocatorImpl.getApplicationContext() usage since BeanLocatorImpl is located in portal-impl.jar. For reference
		Object beanLocatorImpl = PortalBeanLocatorUtil.getBeanLocator();
			Method method = beanLocatorImpl.getClass().getMethod("getApplicationContext");
			[b]ApplicationContext [/b]applicationContext = (ApplicationContext) method.invoke(beanLocatorImpl);



** Error **
10:09:44,848 ERROR [http-bio-8080-exec-1064][PortletServlet:116] javax.portlet.PortletException: java.lang.ClassCastException: com.liferay.portal.spring.context.PortalApplicationContext cannot be cast to org.springframework.context.ApplicationContext
javax.portlet.PortletException: java.lang.ClassCastException: com.liferay.portal.spring.context.PortalApplicationContext cannot be cast to org.springframework.context.ApplicationContext
at com.liferay.portal.kernel.portlet.LiferayPortlet.callActionMethod(LiferayPortlet.java:181)
...
...

Question
As per source code ApplicationContext is mentioned as return type while error mentions PortalApplicationContext as expected return type
1. Why is PortalApplicationContext is the expected return type instead of ApplicationContext as seen in source code?
2. What is the expected way for retrieving spring bean of already instantiated class EG DLFileVersionPersistence?

Regards,
Sourabh
thumbnail
7年前 に David H Nebinger によって更新されました。

RE: Liferay Spring ApplicationContext Retrieval error

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
First, you are in different class loaders. The instance from Liferay is from a class in Liferay's class loader and therefore you cannot cast it on your own side. Basically you should never be going after Liferay's application context, it's not yours to muck with. And with the different class loaders, you can't pluck objects from Liferay's class loader and expect to just cast and go, it's not going to work.

Second, you shouldn't try to get Liferay beans directly. Liferay provides a whole suite of util classes to expose the Liferay apis, so you should find the right util class to get to what you need.