【问题标题】:Retrieving a Spring bean programatically from a hybris hMC Action从 hybris hMC Action 以编程方式检索 Spring bean
【发布时间】:2013-08-11 05:05:02
【问题描述】:

我已经为我的 hMC 编写了一个定制的 SaveAction,我想在这个操作中使用一些服务,例如 modelService。

我想以编程方式进行,而不是通过在我的 spring xml 文件中声明它,因为我的自定义 SaveAction 本身不是一个 spring bean。

这是我想要的一个例子:

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;

}

【问题讨论】:

    标签: java hybris


    【解决方案1】:

    使用 hybris,您可以为此使用类 de.hybris.platform.core.Registry,如下所示:

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

    【讨论】:

      【解决方案2】:

      如果我理解正确,问题的症结在于您想从 Spring 容器外部的对象中访问 Spring 托管组件。

      要做到这一点,你应该看看这个类:SingletonBeanFactoryLocator

      配置后(见下文),您将能够像这样访问应用程序上下文中的 bean:

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

      配置

      <bean id="applicationContextWrapper" class="org.springframework.context.support.ClassPathXmlApplicationContext">
           <constructor-arg>
              <list>
                  <value>applicationContext.xml</value> <!-- context files go here -->
              </list>
           </constructor-arg>
       </bean>
      

      【讨论】:

        猜你喜欢
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-15
        • 2015-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多