Retrieving a Spring bean programatically from a hybris hMC Action

java, sap-commerce-cloud

Solution

With hybris you can use the class de.hybris.platform.core.Registry for that, like this:

final ModelService modelService = Registry.getApplicationContext().getBean("modelService", ModelService.class);

Problem

I've written a customized SaveAction for my hMC and I'd like to use some Services in this action, for instance the modelService. I'd like to do it programatically, and not by declaring it in my spring xml files, since my custom SaveAction is not a spring bean itself. Here's an example of what I want: ``` public class MySaveAction extends GenericItemSaveAction { @Override protected ActionResult afterSave(final Item item, final DisplayState displayState, final Map currentValues, final Map initialValues, final ActionResult actionResult) { ActionResult result = null; result = super.afterSave(item, displayState, currentValues, initialValues, actionResult); //how do I retrieve the modelService spring bean here? final ModelService modelService = null; final VariantProductModel variantProduct = modelService.get(item.getPK()); return result; } ```

Original source