Fórum

Liferay Spring Bean Retrieval @ runtime method ?

Sourabh Lonikar, modificado 7 Anos atrás.

Liferay Spring Bean Retrieval @ runtime method ?

Junior Member Postagens: 42 Data de Entrada: 14/11/16 Postagens Recentes
Hi mates,
Couple of days back I raised issue thread asking where I ran into null liferay spring bean within liferay code. In order reuse spring bean instanciated by liferay during lifecycle workflow, I have tried below 3 approaches:

Solutions

I. Class variable with ServiceReference annotation:
	  @ServiceReference(type = DLFileVersionPersistence.class)
	     protected DLFileVersionPersistence dlFileVersionPersistence;


II. Class variable with BeanReference annotation:
	  @BeanReference(type = DLFileVersionPersistence.class)
	  protected DLFileVersionPersistence dlFileVersionPersistence;


III. Retrieving bean id in portal-spring.xml definition using ApplicationContext spring object.

In order to achieve solution #3, I am referring below code from Liferay InitUtil.java:
 
		ApplicationContext infrastructureApplicationContext = new ArrayApplicationContext(PropsValues.SPRING_INFRASTRUCTURE_CONFIGS);
		ApplicationContext appApplicationContext = new ClassPathXmlApplicationContext(configLocations.toArray(new String[configLocations.size()]), infrastructureApplicationContext);
		ApplicationContext applicationContext =  beanLocator.getApplicationContext();
		DLFileVersionPersistence dlFileVersionPersistence = (DLFileVersionPersistence) applicationContext.getBean("com.liferay.document.library.kernel.service.persistence.DLFileVersionPersistence");


Observations
Solution #I & #II are not working for me while encountering this issue for solution #III

Question
1. Is there any proper way in Liferay for retrieving spring bean initialized already during liferay lifecycle ?


Infra Details
Liferay EE 6.2 GE
Tomcat 7.0.6.2

Sorry for long post.
Have a merry Christmas
emoticon
Sourabh
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: Liferay Spring Bean Retrieval @ runtime method ?

Liferay Legend Postagens: 14916 Data de Entrada: 02/09/06 Postagens Recentes
#1 isn't going to work because the persistence object is not a service.

#2 isn't going to work because Liferay's spring context is separate from yours so spring cannot inject it.

#3 I'm not even sure what you are trying to do with this.

Instead of all of these vague and wrong ways that you are attempting to get something you probably shouldn't, how about sharing what you need to do? There may be a better way to get at what you are trying to accomplish.
Sourabh Lonikar, modificado 7 Anos atrás.

RE: Liferay Spring Bean Retrieval @ runtime method ?

Junior Member Postagens: 42 Data de Entrada: 14/11/16 Postagens Recentes
Hi David,
I am trying to update Document tags and categories. So while using trying to retrieve DLFileVersion object for updating tags & categories, I am running into Null Pointer Exception for dlFileVersionPersistence bean inside liferay portal-impl.jar code.
To reinitialize this liferay bean, I am trying to get said bean within custom portlet and hence above code for getting liferay bean.

Context
I] Calling dlFileVersionLocalServiceImplObject.getLatestFileVersion(dlFileEntryId, true) :

portalClassLoader = PortalClassLoaderUtil.getClassLoader();
	Class<!--?--> dlFileVersionLocalServiceImplClass = portalClassLoader.loadClass("com.liferay.portlet.documentlibrary.service.impl.DLFileVersionLocalServiceImpl");	
	c = dlFileVersionLocalServiceImplClass.getDeclaredConstructor();	
	Object dlFileVersionLocalServiceImplObject = c.newInstance();	
	method = dlFileVersionLocalServiceImplObject.getClass().getMethod("getLatestFileVersion", long.class, boolean.class);
	this.logger.info("addName | DLFileVersionLocalServiceImpl : invoke");
	DLFileVersion dlFileVersion = (DLFileVersion) method.invoke(dlFileVersionLocalServiceImplObject, dlFileEntry.getFileEntryId(), true);
	this.logger.info("addName | DLFileVersionLocalServiceImpl : DONE");


2] Using dlFileVersion object in below code to update document tag and category
updateAsset(long userId, FileEntry fileEntry, FileVersion fileVersion, long[] assetCategoryIds, String[] assetTagNames, long[] assetLinkEntryIds)

Question
1. Why am I getting NPE dlFileVersionPersistence bean inside liferay code.
2. How to get liferay beans within custom portlet.

Crossposted @ Liferay Spring Object Initialization NPE in 3. Development

Regards,
Sourabh
thumbnail
David H Nebinger, modificado 7 Anos atrás.

RE: Liferay Spring Bean Retrieval @ runtime method ?

Liferay Legend Postagens: 14916 Data de Entrada: 02/09/06 Postagens Recentes
What is all of this nonsense?

Why aren't you just calling DLFileVersionLocalServiceUtil.getLatestFileVersion() method directly?