<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>*ServiceUtil classes in OSGi modules</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=111477594" />
  <subtitle>*ServiceUtil classes in OSGi modules</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=111477594</id>
  <updated>2026-04-04T07:56:37Z</updated>
  <dc:date>2026-04-04T07:56:37Z</dc:date>
  <entry>
    <title>RE: *ServiceUtil classes in OSGi modules</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111481873" />
    <author>
      <name>Minhchau Dang</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111481873</id>
    <updated>2018-11-08T01:14:06Z</updated>
    <published>2018-11-08T01:14:06Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;blockquote&gt;
  &lt;div class="quote-title"&gt;Jan Tošovský:&lt;/div&gt;
  &lt;div class="quote"&gt;
    &lt;div class="quote-content"&gt;
      &lt;p&gt;Can somebody elaborate why they should never be used?&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;p&gt;When you use a &lt;code&gt;LocalServiceUtil&lt;/code&gt;, you are claiming that
  your component needs no implementation of that service before it is
  considered ready for activation. In other words, as long as the API
  bundle is there, even if there is no service bundle deployed, even if
  none of its Spring components were registered, you're saying that your
  component will work perfectly and &lt;em&gt;it is perfectly safe for other
    components to start calling methods on your component&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;If you're building code in a vacuum, and you're not calling the
  &lt;code&gt;LocalServiceUtil&lt;/code&gt; from a component activation, it's
  probably safe. However, if you're working with a team, this is a
  debugging nightmare waiting to happen.&lt;/p&gt;
&lt;blockquote&gt;
  &lt;div class="quote-title"&gt;Jan Tošovský:&lt;/div&gt;
  &lt;div class="quote"&gt;
    &lt;div class="quote-content"&gt;
      &lt;p&gt;In my module I use base portlet, which is subclassed. Only
        subclasses have @Component annotation which means no @Reference
        annotation is processed. This behaviour is described here &lt;a href="https://community.liferay.com/blogs/-/blogs/liferay-osgi-annotations-what-they-are-and-when-to-use-them"&gt;https://community.liferay.com/blogs/-/blogs/liferay-osgi-annotations-what-they-are-and-when-to-use-them&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/blockquote&gt;
&lt;p&gt;You can put the &lt;code&gt;@Reference&lt;/code&gt; on a method in the child,
  have that method exist on both the parent and the child, and have the
  child delegate its call to the parent. I believe this is what David
  refers to as "@Reference annotation copying" in a subsequent
  blog on this topic, &lt;a href="https://community.liferay.com/blogs/-/blogs/bnd-instruction-to-avoid"&gt;BND
    Instruction to Avoid&lt;/a&gt;, where the BND instruction that you should
  avoid is one that transparently provides that reference copying (if
  you put the annotation on methods rather than member variables).&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Minhchau Dang</dc:creator>
    <dc:date>2018-11-08T01:14:06Z</dc:date>
  </entry>
  <entry>
    <title>*ServiceUtil classes in OSGi modules</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111477593" />
    <author>
      <name>Jan Tošovský</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=111477593</id>
    <updated>2018-11-07T12:22:27Z</updated>
    <published>2018-11-07T12:22:27Z</published>
    <summary type="html">&lt;p&gt;In the post &lt;a
  href="https://community.liferay.com/forums/-/message_boards/message/83433590"&gt;https://community.liferay.com/forums/-/message_boards/message/83433590&lt;/a&gt;
  there is stated:&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p style="margin-left: 40.0px;"&gt;
  &lt;em&gt;The *LocalServiceUtil classes are purely for legacy portlets as
    they hide the dependency and only provide an indirection to call the
    service. With OSGi components, you should never call the
    *LocalServiceUtil classes any more. &lt;/em&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Can somebody elaborate why they should never be used?&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;In my module I use base portlet, which is subclassed. Only subclasses
  have @Component annotation which means no @Reference annotation is
  processed. This behaviour is described here &lt;a href="https://community.liferay.com/blogs/-/blogs/liferay-osgi-annotations-what-they-are-and-when-to-use-them"&gt;https://community.liferay.com/blogs/-/blogs/liferay-osgi-annotations-what-they-are-and-when-to-use-them&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;However, if I replace injected services (marked by @Reference
  annotations) with *ServiceUtil method calls, it seems to be working.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Now I am not sure if this just coincidence. I really do not want to
  duplicate my code in all subclasses.&lt;/p&gt;</summary>
    <dc:creator>Jan Tošovský</dc:creator>
    <dc:date>2018-11-07T12:22:27Z</dc:date>
  </entry>
</feed>
