Skip to content

Service Wrapper

A mechanism to extend or modify existing Liferay services without changing core code.

  • Add logging/validation to core services.
  • Override default behavior (e.g., custom password policies).
Terminal window
blade create -t service-wrapper user-login-wrapper
user-login-wrapper/src/main/java/com/example/wrapper/CustomUserLocalService.java
@Component(service = ServiceWrapper.class)
public class CustomUserLocalService
extends UserLocalServiceWrapper {
@Override
public User getUserById(long userId) throws PortalException {
System.out.println("User " + userId + " was accessed at " + new Date());
return super.getUserById(userId); // Call original service
}
@Override
public User addUser(...) throws PortalException {
if (emailAddress.contains("spam.com")) {
throw new PortalException("Disposable emails not allowed!");
}
return super.addUser(...);
}
}
  • All calls to UserLocalService will now go through your wrapper.
  • Check logs for your debug messages.
  • Audit Logging: Track user logins/data changes.
  • Validation: Enforce custom business rules.
  1. Service Wrappers:

    • Always call super.method() to preserve original behavior.
    • Use @Reference(target = "(origin.bean=...)") for precise targeting.