2013-06-13 2 views
0

저는 spring-mvc를 기반으로하는 broadleaf에서 일하고 있습니다.봄의 빈 속성 목록에서 하나의 클래스를 대체하십시오.

프로젝트 모듈을 기반으로 다른 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 파일에서 변경했습니다. 그것은 단지의 핵심 핸들러가 실행되는 실행으로 난 그냥,이 핸들러를 확장하고 나의 새로운 핸들러를 만들

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

XML 파일에

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

도 넣어 콩 해상력

<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 클래스 내 처리기를 실행하고 싶습니다.

하지만 작동하지 않습니다. 핵심 부분으로 변경할 수 없으므로 처리기를 처리기로 교체해야합니다. 어떻게하면됩니까? 봄에도 가능합니까?

답변

3

사용자 지정 지속성 처리기의 경우 특히 blCustomPersistenceHandlerFilters bean을 사용하여 핵심 처리기를 제거 할 수 있습니다. 이처럼 콩을 정의하는 것입니다 귀하의 경우 그래서 지금

<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 제품 사용자 정의 지속성 처리기 : 당신이 전에하던 것처럼

<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를 추가 할 수 있습니다 너의 것이 실행되지 않을 것이다.

이 기능은 기본적으로 기본 설정으로 교체하려는 간단한 목적으로는 너무 복잡 할 수 있습니다. 우리가 이런 식으로했던 이유가있을 수도 있지만 더 조사하려면 GitHub Issue을 추가했습니다.

+0

감사합니다. 그 일. – Ankit

관련 문제