Liferay

How to create Model Listeners in Liferay?

Kishan Khatsuriya
Kishan KhatsuriyaOct 15, 2019
How to create Model Listeners in Liferay?

Model Listeners are designed to listen for persistence events on models and perform lightweight actions in response. Model Listeners implement Model Listener interface. Each model can have it’s own model listener events either before or after model changes. There can be multiple listeners on a single model, and the order in which the listeners run can be controlled.

Blog Image

You can create a custom model listener in a module by following steps:

  1. Create model listener class

Create a custom model listener class that extends BaseModelListener<T> as below

1package;
2
3import;
4
5public class CustomModelListener extends BaseModelListener<CustomEntity> {
6
78
9}
  1. Register the model listener service

Register the model listener service with Liferay’s OSGi runtime by adding @Component annotation configuration as below

1package;
2
3import;
4
5@Component(
6immediate = true,
7service = ModelListener.class
8)
9public class CustomModelListener extends BaseModelListener<CustomEntity> {
10
11...
12
13}
  1. Listen for persistence events

Implement any methods of ModelListener interface and add your business logic into method body.

1package;
2
3import;
4
5@Component(
6immediate = true,
7service = ModelListener.class
8)
9public class CustomModelListener extends BaseModelListener<CustomEntity> {
10
11/*
12 * implement your desired methods of ModelListener interface to add your business logic
13 */
14
15}
  1. That’s all, Deploy the module in Liferay server and test your custom model listener.

The ModelListener interface provides a number of methods to listen for the model events.

Event NameUsage
onBeforeAddAssociationIf there’s an association between two models, use this method to do something before an association record is added.
onBeforeCreateUse this method to do something before the persistence layer’s “create” method is called
onBeforeUpdateUse this method to do something before the persistence layer’s “update” method is called
onBeforeRemoveAssociationIf there’s an association between two models, use this method to do something before a removal from the mapping table.
onBeforeRemoveUse this method to do something before the persistence layer’s “remove” method is called
onAfterAddAssociationIf there’s an association between two models, use this method to do something after an association record is added
onAfterCreateUse this method to do something after the persistence layer’s “create” method is called.
onAfterUpdateUse this method to do something after the persistence layer’s “update” method is called.
onAfterRemoveAssociationIf there’s an association between two models, use this method to do something after an association record is removed.
onAfterRemoveUse this method to do something after the persistence layer’s “remove” method is called.

Please refer below example of Model Listener for User model.

1package ...;
2
3import org.osgi.service.component.annotations.Component;
4
5import com.liferay.portal.kernel.exception.ModelListenerException;
6import com.liferay.portal.kernel.log.Log;
7import com.liferay.portal.kernel.log.LogFactoryUtil;
8import com.liferay.portal.kernel.model.BaseModelListener;
9import com.liferay.portal.kernel.model.ModelListener;
10import com.liferay.portal.kernel.model.User;
11
12@Component(
13	immediate = true,
14	service = ModelListener.class
15)
16public class CustomUserModelListener extends BaseModelListener<User> {
17	
18	private static final Log log = LogFactoryUtil.getLog(CustomUserMoledListener.class);
19	
20	@Override
21	public void onBeforeCreate(User userModel) throws ModelListenerException {
22		
23		log.info("in onBeforeCreate method");
24		log.info("userModel : " + userModel);
25
26		super.onBeforeCreate(userModel);
27	}
28	
29	@Override
30	public void onAfterCreate(User userModel) throws ModelListenerException {
31		
32		log.info("in onAfterCreate method");
33		log.info("userModel : " + userModel);
34
35		super.onAfterCreate(userModel);
36	}
37}

You can test the given CustomUserModelListener by creating new user from Liferay control panel, You can see logs are getting printed in console when model listener is called.


© 2026 IGNEK. All rights reserved.

Ignek on LinkedInIgnek on InstagramIgnek on FacebookIgnek on YouTubeIgnek on X