Question

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;

}
Was it helpful?

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);

OTHER TIPS

If I understand correctly, the crux of the issue is that you want to access Spring managed components from within an object outside of the Spring container.

To do this you should look at this class: SingletonBeanFactoryLocator

Once configured (see below) you will be able to access the beans in the application context like this:

BeanFactoryLocator locator = SingletonBeanFactoryLocator.getInstance();
BeanFactoryReference factory = locator.useBeanFactory("applicationContextWrapper");
MyService mySpringService= (MyService)factory.getFactory().getBean("mySpringService");

Configuration:

<bean id="applicationContextWrapper" class="org.springframework.context.support.ClassPathXmlApplicationContext">
     <constructor-arg>
        <list>
            <value>applicationContext.xml</value> <!-- context files go here -->
        </list>
     </constructor-arg>
 </bean>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top