问题描述
我需要使用spring.net通过属性注入为实现接口注入依赖项。我不是很熟悉,有人可以向我解释一下吗?
因此在config xml文件中,该对象是注入所需依赖项的类。所以id是该类的名称,但是为什么不使用name =“ PetStore”呢?另外,类型是该类的完整路径,那么它后面的附加参数“ PetStore”是什么?
关于属性,名称和引用指的是什么?该名称是否引用了在“ PetStore”类中声明的属性,而ref引用了该接口的实现?
<object id="PetStore" type="PetStore.Services.PetStoreService,PetStore">
<property name="AccountDao" ref="AccountDao"/>
<property name="ItemDao" ref="ItemDao"/>
</object>
public class PetStore : IPetStore
{
public IAccountDao AccountDao { get; set; }
public IItemDao ItemDao{ get; set; }
}
解决方法
<property name="AccountDao" <!-- The name of the property you want to inject into -->
ref="AccountDao"/> <!-- The name of the object definition you want to inject -->
请参见quickstart中的第37.2.3节“ setter注入”。
在type="PetStore.Services.PetStoreService,PetStore"
中,PetStore.Services.PetStoreService
是类的完整类型,包括名称空间。 PetStore
是程序集的名称。