@gengzhengtao
2015-08-12T08:53:25.000000Z
字数 3938
阅读 1791
电商平台broadleafCommerce Broadleaf概念 关键部分和配置
合并配置是通过web.xml文件处理的。合并工具在Boradleaf的主要操作是智能的混合一个或多个spring context文件, 最终合并的application context版本交给spring去处理。
在一个典型的Broadleaf应用中存在两个不同类型的applicationConext文件:在web.xml中的patchconfiglocation context param列出核心的spring文件,Spring MVC文件在contextConfigLocation servlet的init param中列出。这两种类型的文件的合并行为稍有不同。
Broadleaf通过提供的专门的MergecontextLoader能够智能的合并beans。在web.xml中作为一个监听器被调用的配置如下:
<listener><listener-class>org.broadleafcommerce.common.web.extensibility.MergeContextLoaderListener</listener-class></listener>
这个contextLoader将解析在patchConfigLocation中指定的所有applicationContexts内容,并尽可能的使用两种策略来合并属性
在Broadleaf的两种策略中这是比较新的一个,所有新的配置写入这个模式,了解该策略最简单的方式就是通过例子,让我们来看看下面一个BroadLeaf applicationContext文件的一些配置:
<bean id="blDialectProcessors" class="org.springframework.beans.factory.config.SetFactoryBean"><property name="sourceSet"><set><ref bean="blAddSortLinkProcessor" /><ref bean="blCategoriesProcessor" />... other bean references ...</set></property></bean><bean id="blDialect" class="org.broadleafcommerce.common.web.dialect.BLCDialect"><property name="processors" ref="blDialectProcessors" /></bean>
我们这里有一个叫做blDialect的bean,它有一个属性叫做processors,这个属性需要实现Thymeleaf的IProcessor的Set集合,这个applicatonContext文件还定义了另外一个类型为setFactoryBean的叫做blDialectProcessors的bean。这就表示当这个SetFactoryBean被注入到processors属性时,Spring将会对做一些处理,将他转换为一个Set
我们还可以增加额外的processor到这个Set中,不需要完全复制整个BldialectProcessorsbean,配置如下:
<bean id="blDialectAdditionalProcessors" class="org.springframework.beans.factory.config.SetFactoryBean"><property name="sourceSet"><set><ref bean="blAdditionalProcessorOne"/><ref bean="blAdditionalProcessorTwo"/></set></property></bean><bean class="org.broadleafcommerce.common.extensibility.context.merge.LateStageMergeBeanPostProcessor"><property name="collectionRef" value="blDialectAdditionalProcessors" /><property name="targetRef" value="blDialectProcessors" /></bean>
这个额外的配置需要创建一个新的bean,blDialectAdditionalProcessors需要定义我们需要添加到blDialect processor属性的两个额外的processor,其次,我们定义了一个LateStrageMergeBeanPostProcessor它将我们新的bean合并到现有的bean中。然后当这个现有的bean注入到bldialect的时候,将包含我们新增加的processor。
这个策略适用于添加条目(entries)到任何预定义的Broadleaf beans
在系统中没有被迁移到新的风格的部分仍然使用这个旧的合并策略。它利用在 default.properties 中定义的各种基础的 XPath 合并策略。这种方法实际上是在完成Spring文件之前修改需要合并的applicationContexts的XML结构。让我们来看看一下这个在blWebTemplateEngine中注册多个dialect的例子:
<bean id="blWebTemplateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"><property name="dialects"><set><ref bean="thymeleafSpringStandardDialect" /><ref bean="blDialect" /></set></property></bean>
如果我们想添加自定义的dialect,我们可以简单地将下面的bean定义到我们的applicaitonContext文件中:
<bean id="blWebTemplateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"><property name="dialects"><set><ref bean="myCustomDialect" /></set></property></bean>
可以这样写,是因为在default.properties中是这样配置的
handler.14=org.broadleafcommerce.common.extensibility.context.merge.handlers.NodeReplaceInsertpriority.14=14xpath.14=/beans/bean[@id='blWebTemplateEngine']/*handler.14.1=org.broadleafcommerce.common.extensibility.context.merge.handlers.InsertItemspriority.14.1=1xpath.14.1=/beans/bean[@id='blWebTemplateEngine']/property[@name='dialects']/set/ref
最后生成的xml将是:
<bean id="blWebTemplateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine"><property name="dialects"><set><ref bean="thymeleafSpringStandardDialect" /><ref bean="blDialect" /><ref bean="myCustomDialect" /></set></property></bean>
如果可能的话,最好使用策略1,当你将目标合并到现有的ListFactoryBean,SetFactoryBean,或者MapFactoryBean,这个是一个正确的做法
如果你想要增加一个条目(entry)到集合中,但是非由上面三个类中的一个处理,下一个步骤是检查在default.properties存在一个条目。如果是的话,你可以使用第二个合并策略。
有时,您可能需要重写 Broadleaf 合并过程。
可以通过在你的classpath中添加一个名字为broadleaf-commerce/skipMergeComponents.txt文件,例如在DemoSite项目中,将文件放在core/src/main/resources目录下。
该文件应包含您不想要Broadleaf Commerce合并进程使用的组件名称的列表。
blAddItemWorkflowblUpdateItemWorkflow
通过将组件添加到此列表中,将使用默认Spring合并过程。
基于servlet级别的appliction context文件,第二种策略是不适用的。因为没有专门的ContextLoader来处理XML,然而,第一种策略依然适用于任何使用FactoryBean方法的bean