0

我在基于 spring-mvc 的阔叶树上工作。

基于项目模块的不同 xml 文件中有 3-4 个 blCustomPersistenceHandlers bean 定义。

<bean id="blCustomPersistenceHandlers" class="org.springframework.beans.factory.config.ListFactoryBean" scope="prototype">
        <property name="sourceList">
            <list>
                <bean class="org.broadleafcommerce.admin.server.service.handler.CategoryCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.admin.server.service.handler.CustomerPasswordCustomPersistenceHandler"/>                
                <bean class="org.broadleafcommerce.openadmin.server.security.handler.AdminUserCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.admin.server.service.handler.CustomerCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.admin.server.service.handler.ProductCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.admin.server.service.handler.ChildCategoriesCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.admin.server.service.handler.SkuCustomPersistenceHandler"/>
            </list>
        </property>
    </bean>

下面在不同的xml文件中

<bean id="blCustomPersistenceHandlers" class="org.springframework.beans.factory.config.ListFactoryBean" scope="prototype">
        <property name="sourceList">
            <list>
                <bean class="org.broadleafcommerce.cms.admin.server.handler.PageTemplateCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.cms.admin.server.handler.StructuredContentTypeCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.cms.admin.server.handler.SandBoxItemCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.cms.admin.server.handler.PendingSandBoxItemCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.cms.admin.server.handler.TimeDTOCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.cms.admin.server.handler.RequestDTOCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.cms.admin.server.handler.StructuredContentItemCriteriaCustomPersistenceHandler"/>
                <bean class="org.broadleafcommerce.cms.admin.server.handler.PageItemCriteriaCustomPersistenceHandler"/>
            </list>
        </property>
    </bean>

上述定义驻留在我们包含的 jar 文件中。现在我想替换其中一个处理程序,例如 ProductCustomPersistenceHandler,

我需要更改有关该处理程序的一些功能,因此我在我的 xml 文件中更改了该处理程序,如下所示。

<bean id="org.broadleafcommerce.admin.server.service.handler.ProductCustomPersistenceHandler"
        class="com.mycompany.server.service.handler.HCProductCustomPersistenceHandler" />

并将bean定义放入xml文件

<bean id="blCustomPersistenceHandlers" class="org.springframework.beans.factory.config.ListFactoryBean"> <!-- scope="prototype" -->
        <property name="sourceList">
            <list>
                <bean class="com.mycompany.server.service.handler.HCProductCustomPersistenceHandler"/>
            </list>
        </property> 
    </bean>

ProductCustomPersistenceHandler 类

public class ProductCustomPersistenceHandler extends CustomPersistenceHandlerAdapter {

    @Resource(name = "blCatalogService")
    protected CatalogService catalogService;

    private static final Log LOG = LogFactory.getLog(ProductCustomPersistenceHandler.class);

    @Override
    public Boolean canHandleAdd(PersistencePackage persistencePackage) {
        String ceilingEntityFullyQualifiedClassname = persistencePackage.getCeilingEntityFullyQualifiedClassname();
        String[] customCriteria = persistencePackage.getCustomCriteria();
        return !ArrayUtils.isEmpty(customCriteria) && "productDirectEdit".equals(customCriteria[0]) && Product.class.getName().equals(ceilingEntityFullyQualifiedClassname);
    }

    @Override
    public Boolean canHandleUpdate(PersistencePackage persistencePackage) {
        return canHandleAdd(persistencePackage);
    }

    @Override
    public Entity add(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
        Entity entity  = persistencePackage.getEntity();
        try {
            PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
            Product adminInstance = (Product) Class.forName(entity.getType()[0]).newInstance();
            Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
            adminInstance = (Product) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);

            adminInstance = (Product) dynamicEntityDao.merge(adminInstance);

            CategoryProductXref categoryXref = new CategoryProductXrefImpl();
            categoryXref.setCategory(adminInstance.getDefaultCategory());
            categoryXref.setProduct(adminInstance);
            if (adminInstance.getDefaultCategory() != null && !adminInstance.getAllParentCategoryXrefs().contains(categoryXref)) {
                categoryXref = (CategoryProductXref) dynamicEntityDao.merge(categoryXref);
                adminInstance.getAllParentCategoryXrefs().add(categoryXref);
            }

            //Since none of the Sku fields are required, it's possible that the user did not fill out
            //any Sku fields, and thus a Sku would not be created. Product still needs a default Sku so instantiate one
            if (adminInstance.getDefaultSku() == null) {
                Sku newSku = catalogService.createSku();
                adminInstance.setDefaultSku(newSku);
                adminInstance = (Product) dynamicEntityDao.merge(adminInstance);
            }

            //also set the default product for the Sku
            adminInstance.getDefaultSku().setDefaultProduct(adminInstance);
            dynamicEntityDao.merge(adminInstance.getDefaultSku());

            return helper.getRecord(adminProperties, adminInstance, null, null);
        } catch (Exception e) {
            LOG.error("Unable to add entity for " + entity.getType()[0], e);
            throw new ServiceException("Unable to add entity for " + entity.getType()[0], e);
        }
    }

    @Override
    public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException {
        Entity entity = persistencePackage.getEntity();
        try {
            PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective();
            Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties(Product.class.getName(), persistencePerspective);
            Object primaryKey = helper.getPrimaryKey(entity, adminProperties);
            Product adminInstance = (Product) dynamicEntityDao.retrieve(Class.forName(entity.getType()[0]), primaryKey);
            adminInstance = (Product) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false);

            adminInstance = (Product) dynamicEntityDao.merge(adminInstance);

            CategoryProductXref categoryXref = new CategoryProductXrefImpl();
            categoryXref.setCategory(adminInstance.getDefaultCategory());
            categoryXref.setProduct(adminInstance);
            if (adminInstance.getDefaultCategory() != null && !adminInstance.getAllParentCategoryXrefs().contains(categoryXref)) {
                adminInstance.getAllParentCategoryXrefs().add(categoryXref);
            }

            return helper.getRecord(adminProperties, adminInstance, null, null);
        } catch (Exception e) {
            LOG.error("Unable to update entity for " + entity.getType()[0], e);
            throw new ServiceException("Unable to update entity for " + entity.getType()[0], e);
        }
    }
}

我只是扩展这个处理程序并制作我的新处理程序,因为它只运行核心处理程序正在执行,我想执行我的处理程序。

但这不起作用。我不能变成核心部分,所以我只需要用我的处理程序替换处理程序。我怎样才能做到这一点?春天可以吗?

4

1 回答 1

3

特别是对于自定义持久性处理程序,您可以使用 blCustomPersistenceHandlerFilters bean 删除核心处理程序。所以在你的情况下,你会像这样定义你的bean:

<bean id="blCustomPersistenceHandlerFilters" class="org.springframework.beans.factory.config.ListFactoryBean" scope="prototype">
    <property name="sourceList">
        <list>
            <bean class="org.broadleafcommerce.openadmin.server.service.handler.DefaultCustomPersistenceHandlerFilter">
                <property name="filterCustomPersistenceHandlerClassnames">
                    <list>
                        <value>org.broadleafcommerce.admin.server.service.handler.ProductCustomPersistenceHandler</value>
                    </list>
                </property>
            </bean>
        </list>
    </property>
</bean>

然后您可以像以前一样将自己的 CPH 添加到列表中:

<bean id="blCustomPersistenceHandlers" class="org.springframework.beans.factory.config.ListFactoryBean"> <!-- scope="prototype" -->
    <property name="sourceList">
        <list>
            <bean class="com.mycompany.server.service.handler.HCProductCustomPersistenceHandler"/>
        </list>
    </property> 
</bean>

现在 BLC 产品自定义持久性处理程序将不会运行,但您的会。

对于您想要用自定义的替换开箱即用的简单目的而言,这可能有点过于复杂。我们这样做可能有充分的理由,但我添加了一个GitHub 问题以供进一步调查。

于 2013-06-15T18:37:29.480 回答