Moved to the Liferay Faces - English category.

thumbnail
Michel Schmidt, modified 6 Years ago. New Member Posts: 21 Join Date: 2/27/17 Recent Posts

Hey everyone,

 

i was recently wondering about the following question:

When I want to inject a liferay service into my module, how do I do that correctly?

 

Example:

I'm developing a JSF Portlet and want to access my custom service-builder service (let's call it CustomEntityLocalServiceImpl).

When developing a JSF Portlet, I know from the documentation to do it with the ServiceTracker approach.

 

 

When developing a liferay service builder module and I want to use the CustomEntityLocalServiceImpl, I will generate setters for the services interface and annotate the setters with:

@Reference(unbind = "-")

to get the service injected into my service by the osgi runtime.

 

 

I also saw this approach while browsing through the forums:

@ServiceReference(type =  CustomEntityLocalService.class)
private CustomEntityLocalServiceImpl customEntityLocalService;

 

So what approach is considered best practice when ..

.. I want to inject a portal-impl service into my JSF-Porlet?

.. I want to inject a portal-impl service into my service builder module?

.. I want to inject a service builder custom service into my JSF-Porlet?

.. I want to inject a service builder custom service into my service builder custom service?

 

Also:

From what I read in the forums etc, the CustomEntityLocalServiceUtil approach is considered bad because there is no way to guarantee that the service is available.

But when I'm developing within my custom service builder module and want to reference a service within the same service.xml, I could use it, or is it still considered bad?

 

Thanks for reading :)

thumbnail
Kyle Joseph Stiemann, modified 6 Years ago. Liferay Master Posts: 760 Join Date: 1/14/13 Recent Posts
Moved to the Liferay Faces - English category.
thumbnail
Kyle Joseph Stiemann, modified 6 Years ago. Liferay Master Posts: 760 Join Date: 1/14/13 Recent Posts

Hi Michel,

Please take a look at James Hinkey's post on this topic:

There are these situations that come to mind when invoking a local service:

  • To invoke a service that's in the same application context as your Service Builder code (a Spring Bean) that's in a module JAR, use @BeanReference
  • To invoke a service that's outside of your application context from your Service Builder code (a Spring Bean) that's in a module JAR, use @ServiceReference
  • To invoke a service outiside of your application context from your WAR application, use a ServiceTracker
  • To invoke a service outside of your application context from your OSGi component, use @Reference

Note, these are all 7.1 articles but are still relevant to 7.0.

Please let me know if you need more information.

- Kyle

thumbnail
Michel Schmidt, modified 6 Years ago. New Member Posts: 21 Join Date: 2/27/17 Recent Posts

Hi Kyle,

 

thank you for linking that post, seems to answer all my questions.

 

Best regards

thumbnail
Kyle Joseph Stiemann, modified 6 Years ago. Liferay Master Posts: 760 Join Date: 1/14/13 Recent Posts
Glad we could help, Michel! Thanks for using Liferay Faces!

- Kyle