[关闭]
@gengzhengtao 2015-08-12T08:53:25.000000Z 字数 3938 阅读 1524

合并配置

电商平台broadleafCommerce Broadleaf概念 关键部分和配置


合并配置是通过web.xml文件处理的。合并工具在Boradleaf的主要操作是智能的混合一个或多个spring context文件, 最终合并的application context版本交给spring去处理。

在一个典型的Broadleaf应用中存在两个不同类型的applicationConext文件:在web.xml中的patchconfiglocation context param列出核心的spring文件,Spring MVC文件在contextConfigLocation servlet的init param中列出。这两种类型的文件的合并行为稍有不同。

合并核心的Spring Application Contexts

Broadleaf通过提供的专门的MergecontextLoader能够智能的合并beans。在web.xml中作为一个监听器被调用的配置如下:

  1. <listener>
  2. <listener-class>org.broadleafcommerce.common.web.extensibility.MergeContextLoaderListener</listener-class>
  3. </listener>

这个contextLoader将解析在patchConfigLocation中指定的所有applicationContexts内容,并尽可能的使用两种策略来合并属性

策略1:Set/List/Map FactoryBeans

在Broadleaf的两种策略中这是比较新的一个,所有新的配置写入这个模式,了解该策略最简单的方式就是通过例子,让我们来看看下面一个BroadLeaf applicationContext文件的一些配置:

  1. <bean id="blDialectProcessors" class="org.springframework.beans.factory.config.SetFactoryBean">
  2. <property name="sourceSet">
  3. <set>
  4. <ref bean="blAddSortLinkProcessor" />
  5. <ref bean="blCategoriesProcessor" />
  6. ... other bean references ...
  7. </set>
  8. </property>
  9. </bean>
  10. <bean id="blDialect" class="org.broadleafcommerce.common.web.dialect.BLCDialect">
  11. <property name="processors" ref="blDialectProcessors" />
  12. </bean>

我们这里有一个叫做blDialect的bean,它有一个属性叫做processors,这个属性需要实现Thymeleaf的IProcessor的Set集合,这个applicatonContext文件还定义了另外一个类型为setFactoryBean的叫做blDialectProcessors的bean。这就表示当这个SetFactoryBean被注入到processors属性时,Spring将会对做一些处理,将他转换为一个Set

我们还可以增加额外的processor到这个Set中,不需要完全复制整个BldialectProcessorsbean,配置如下:

  1. <bean id="blDialectAdditionalProcessors" class="org.springframework.beans.factory.config.SetFactoryBean">
  2. <property name="sourceSet">
  3. <set>
  4. <ref bean="blAdditionalProcessorOne"/>
  5. <ref bean="blAdditionalProcessorTwo"/>
  6. </set>
  7. </property>
  8. </bean>
  9. <bean class="org.broadleafcommerce.common.extensibility.context.merge.LateStageMergeBeanPostProcessor">
  10. <property name="collectionRef" value="blDialectAdditionalProcessors" />
  11. <property name="targetRef" value="blDialectProcessors" />
  12. </bean>

这个额外的配置需要创建一个新的bean,blDialectAdditionalProcessors需要定义我们需要添加到blDialect processor属性的两个额外的processor,其次,我们定义了一个LateStrageMergeBeanPostProcessor它将我们新的bean合并到现有的bean中。然后当这个现有的bean注入到bldialect的时候,将包含我们新增加的processor。

这个策略适用于添加条目(entries)到任何预定义的Broadleaf beans

策略2:基于XPath的合并

在系统中没有被迁移到新的风格的部分仍然使用这个旧的合并策略。它利用在 default.properties 中定义的各种基础的 XPath 合并策略。这种方法实际上是在完成Spring文件之前修改需要合并的applicationContexts的XML结构。让我们来看看一下这个在blWebTemplateEngine中注册多个dialect的例子:

  1. <bean id="blWebTemplateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
  2. <property name="dialects">
  3. <set>
  4. <ref bean="thymeleafSpringStandardDialect" />
  5. <ref bean="blDialect" />
  6. </set>
  7. </property>
  8. </bean>

如果我们想添加自定义的dialect,我们可以简单地将下面的bean定义到我们的applicaitonContext文件中:

  1. <bean id="blWebTemplateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
  2. <property name="dialects">
  3. <set>
  4. <ref bean="myCustomDialect" />
  5. </set>
  6. </property>
  7. </bean>

可以这样写,是因为在default.properties中是这样配置的

  1. handler.14=org.broadleafcommerce.common.extensibility.context.merge.handlers.NodeReplaceInsert
  2. priority.14=14
  3. xpath.14=/beans/bean[@id='blWebTemplateEngine']/*
  4. handler.14.1=org.broadleafcommerce.common.extensibility.context.merge.handlers.InsertItems
  5. priority.14.1=1
  6. xpath.14.1=/beans/bean[@id='blWebTemplateEngine']/property[@name='dialects']/set/ref

最后生成的xml将是:

  1. <bean id="blWebTemplateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
  2. <property name="dialects">
  3. <set>
  4. <ref bean="thymeleafSpringStandardDialect" />
  5. <ref bean="blDialect" />
  6. <ref bean="myCustomDialect" />
  7. </set>
  8. </property>
  9. </bean>

我应该使用哪一种策略呢?

如果可能的话,最好使用策略1,当你将目标合并到现有的ListFactoryBean,SetFactoryBean,或者MapFactoryBean,这个是一个正确的做法

如果你想要增加一个条目(entry)到集合中,但是非由上面三个类中的一个处理,下一个步骤是检查在default.properties存在一个条目。如果是的话,你可以使用第二个合并策略。

禁用第二种策略

有时,您可能需要重写 Broadleaf 合并过程。

可以通过在你的classpath中添加一个名字为broadleaf-commerce/skipMergeComponents.txt文件,例如在DemoSite项目中,将文件放在core/src/main/resources目录下。

该文件应包含您不想要Broadleaf Commerce合并进程使用的组件名称的列表。

  1. blAddItemWorkflow
  2. blUpdateItemWorkflow

通过将组件添加到此列表中,将使用默认Spring合并过程。

合并Spring MVC Applicatin Contexts

基于servlet级别的appliction context文件,第二种策略是不适用的。因为没有专门的ContextLoader来处理XML,然而,第一种策略依然适用于任何使用FactoryBean方法的bean

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注