2013-06-13 28 views
0

我在基於spring-mvc的broadleaf中工作。春季在bean屬性列表中替換一個類

根據項目模塊,在不同的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" /> 

,並把豆認定中成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); 
     } 
    } 
} 

我只是擴展這個處理程序,並讓我的新的處理程序,因爲它只能運行核處理器在執行,我想執行我的處理程序。

但這不起作用。 我不能變成核心部分,所以我只需要用我的處理程序替換處理程序。 我該怎麼做? 春天有可能嗎?

回答

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 Issue來進一步調查。

+0

謝謝!它的工作。 – Ankit