0

我有一个关于对di.xml 中使用的接口<preference>和属性的角色的理解的查询。<type>在哪些情况下我们应该使用<preference>and <type>?在逻辑实现方面是否有任何实质性差异。

<preference>例如,转到 app/code/Magento/Catalog/etc/di.xml ,会有很多<type>使用标签的实例。我们如何准确地区分它们?

期待听到想法。

4

1 回答 1

1

<preference>用于指示您希望 DI 框架在请求该类型时提供哪个具体类

<type>用于几个不同的事情,其中​​之一是提供或覆盖注入到具体类型的构造函数中的参数。该<type>声明还用于声明插件

<preference>您在和中看到接口的时间<type>是为该接口定义插件的时间。 <type>允许您在该对象类型上声明插件,无论它是具体类还是接口。

示例来自app/code/Magento/Catalog/etc/di.xml

<preference for="Magento\Catalog\Api\ProductRepositoryInterface" type="Magento\Catalog\Model\ProductRepository" />
...
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
    <plugin name="transactionWrapper" type="\Magento\Catalog\Model\Plugin\ProductRepository\TransactionWrapper" sortOrder="-1"/>
</type>
于 2015-09-28T13:52:03.160 回答