Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: Accessing Liferay Services correctly
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 :)
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
Hi Kyle,
thank you for linking that post, seems to answer all my questions.
Best regards
- Kyle