LRP-614 practice question 24 of 25

The recommended way to create new methods in a core service is to use a service wrapper hook.

Full set: 144 questions
Answer options
A: True
B: False
Correct Answer:
FALSE
Explanation

A service wrapper hook is the recommended mechanism for intercepting and modifying the business logic of existing methods within a Liferay core service. It allows a developer to add custom logic before or after the original method is called. However, a service wrapper cannot be used to add entirely new methods to the service's public interface, because the wrapper class must implement the same service interface as the original service it is wrapping. The recommended Liferay practice for creating new service-layer methods is to build a new, separate service using Liferay's Service Builder in a custom plugin (e.g., a portlet). This new service can then call upon core Liferay services to accomplish its tasks, effectively extending the platform's functionality without altering the core service contracts.

Why Incorrect

The statement "True" is incorrect because it fundamentally misunderstands the purpose and technical constraints of a service wrapper hook. The core design principle of a service wrapper is to "wrap" or "decorate" an existing interface, not to extend it with new method signatures. Attempting to do so would violate the Java interface contract that the wrapper must adhere to, and the new methods would not be accessible through the service's utility class (e.g., UserServiceUtil).

References

1. Liferay in Action (Manning Publications): While a commercial book, its content is derived directly from Liferay engineering principles and is often used in academic contexts to teach the platform. It clarifies the distinction: "Hooks are for overriding existing functionality... If you need to add new functionality, you should write a portlet plugin. For example, you can’t add a new method to a Liferay service with a hook, but you can change the implementation of an existing one."

Reference: Liferay in Action by Richard Sezov, Jr., Chapter 13, Section 13.2.2 "Overriding portal services", Page 388.

2. Official Liferay 6.2 Developer's Guide: This official vendor documentation explicitly describes the use case for service wrappers. The guide focuses on overriding methods. It states, "To override a portal service, you need to create a hook plugin that provides a wrapper for the service." The examples and text consistently refer to changing the behavior of existing methods like addUser or updateUser. The guide directs developers to use Service Builder for creating new services.

Reference: Liferay Portal 6.2 Developer’s Guide, Chapter 7: "Customizing Liferay Portal with Hooks", Section: "Overriding a Portal Service Using a Hook".

3. Official Liferay Documentation (Liferay Learn): Modern Liferay documentation reinforces this long-standing principle. When discussing Service Wrappers (the modern equivalent), the documentation states their purpose is to "customize a service by wrapping it." The tutorial for creating new functionality, in contrast, is titled "Creating a Service Builder Project," which involves defining new entities and service interfaces from scratch.

Reference: Liferay Learn, "Service Wrappers" tutorial. It describes the process as [Your Service Wrapper] → [Original Liferay Service], indicating a modification of the existing path, not the creation of a new one.

Reference: Liferay Learn, "Creating a Service Builder Project" tutorial. This is presented as the standard method for creating new -LocalServiceImpl and -ServiceImpl classes with custom methods.

Scroll to Top