Pick an item type and which lifecycle phases to hook into -- get the interceptor class, its Spring bean, and the InterceptorMapping registration.
🔒 Your code never leaves your browser — everything is generated locally from templates, no AI, no server round-trip.
trainingcore/src/com/prospect2p/core/interceptors/MyInterceptor.javapackage com.prospect2p.core.interceptors;
import de.hybris.platform.servicelayer.interceptor.InterceptorContext;
import de.hybris.platform.servicelayer.interceptor.InterceptorException;
import de.hybris.platform.servicelayer.interceptor.LoadInterceptor;
import de.hybris.platform.core.model.product.ProductModel;
public class MyInterceptor implements LoadInterceptor {
@Override
public void onLoad(final Object model, final InterceptorContext ctx) throws InterceptorException {
// Runs every time an item of this type is loaded from the database.
final ProductModel product = (ProductModel) model;
// TODO: implement onLoad logic for Product
}
}
An Interceptor in SAP Commerce (Hybris) is a class that plugs into an item type's persistence lifecycle -- loading, initializing defaults, preparing to save, validating, and removing. Each phase has its own interface (LoadInterceptor, InitDefaultsInterceptor, PrepareInterceptor, ValidateInterceptor, RemoveInterceptor), and a single class can implement as many of them as it needs.
Interceptors are registered against a specific item type (via typeCode) with an InterceptorMapping, and the servicelayer invokes the matching method automatically whenever an item of that type (or a subtype) goes through that lifecycle phase -- no explicit call site in your business code.
Use an interceptor for cross-cutting logic that must run no matter how an item is saved -- from Backoffice, an ImpEx import, a REST call, or a batch job. Common uses: defaulting attributes on create (InitDefaults), deriving/normalizing values before save (Prepare), enforcing business rules (Validate), or blocking/cleaning up on delete (Remove).
Prefer a Populator or service-layer code instead when the logic only needs to run from one specific code path (e.g. one OCC endpoint) -- an interceptor runs for every save of that type, everywhere, which is powerful but also easy to misuse for something that should be narrower.
Yes -- that's the normal pattern. Implement LoadInterceptor, PrepareInterceptor, ValidateInterceptor etc. on the same class and register it with one InterceptorMapping; the servicelayer calls whichever method matches the phase it's in.
Both run just before persisting, but PrepareInterceptor is for deriving/normalizing attribute values (it can still change the item), while ValidateInterceptor is for rejecting an invalid save by throwing InterceptorException -- by convention it shouldn't mutate the item, only check it.
Yes. Interceptors run at the servicelayer persistence level, so they fire for saves coming from Backoffice, ImpEx, REST/OCC, and custom business code alike -- that's exactly what makes them useful for rules that must never be bypassed.
Check the concrete type inside the method (e.g. model.getItemtype() or an instanceof check against the subtype's Model class) and return early, since InterceptorMapping's typeCode match includes all subtypes by design.
Throwing InterceptorException from onRemove is what blocks the removal -- if the method just logs or returns normally, the delete proceeds. Make sure the exception is actually thrown on the condition you want to block.