Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在对 Spring.NET 中的集合尝试这个问题时,我发现 Spring 可以使用私有 setter 注入依赖项。我有两个问题:
文档说:
基于 Setter 的 DI 是通过在对象上调用 setter 方法来实现的......
诚然,它没有明确说明公共setter 方法,但这是我一直假设的。我在 .NET 3.5 上使用 1.0.2 版。
大学教师,
不,这没有记录在 asfaik - 主要是因为我们不推荐它。该功能用于在处理遗留/第 3 方库时支持罕见的极端情况。
您应该只使用您也可以在单元测试中调用的公共设置器!
hth,埃里希
Spring 的 Java 版本使用反射,它可以获取那些私有设置器。这很好,因为您不会为了满足 Spring 的要求而强迫某人更改不可变对象。
就个人而言,我更喜欢尽可能使用构造函数注入。