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).
Implementation
Section titled “Implementation”Create the Module
Section titled “Create the Module”blade create -t service-wrapper user-login-wrapperWrap the Target Service
Section titled “Wrap the Target Service”@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(...); }}Deploy and Verify
Section titled “Deploy and Verify”- All calls to
UserLocalServicewill now go through your wrapper. - Check logs for your debug messages.
Use Cases
Section titled “Use Cases”- Audit Logging: Track user logins/data changes.
- Validation: Enforce custom business rules.
Pro Tips
Section titled “Pro Tips”-
Service Wrappers:
- Always call
super.method()to preserve original behavior. - Use
@Reference(target = "(origin.bean=...)")for precise targeting.
- Always call