Spring.net - PropertyRetreeringFactoryObject - 属性为空

发布于 2024-11-25 01:54:58 字数 858 浏览 6 评论 0原文

为了解决这个问题,我正在采取看看我们的 spring.net 配置是如何工作的。

根本问题来自此代码片段:

<object name="someObject" singleton="false" type="SomeType.someObject, SomeAssembly">
  <constructor-arg name="authSession">
    <object type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">
      <property name="TargetObject" ref="AuthSessionManager" />
      <property name="TargetProperty" value="CurrentAuthSession" />
    </object>
  </constructor-arg>
</object>

在用户未登录的情况下,AuthSessionManager.CurrentAuthSession将为空。在这种情况下,Spring.NET 会抛出异常:“Factory object returned null object”。

在这种情况下,我如何告诉 Spring 空对象是可以接受的?

In an attempt to resolve this question, I'm taking a look at how our spring.net configuration works.

The root problem comes from this snippet:

<object name="someObject" singleton="false" type="SomeType.someObject, SomeAssembly">
  <constructor-arg name="authSession">
    <object type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">
      <property name="TargetObject" ref="AuthSessionManager" />
      <property name="TargetProperty" value="CurrentAuthSession" />
    </object>
  </constructor-arg>
</object>

In a case where a user is not logged in, AuthSessionManager.CurrentAuthSession will be null. When that is the case, Spring.NET throws an exception: "Factory object returned null object".

How can I tell Spring that the null object is acceptable in this case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2024-12-02 01:54:58

您可以使用 表达式从spring context 在你的构造函数参数中,类似于:

<object name="someObject" singleton="false" 
        type="SomeType.someObject, SomeAssembly">
  <constructor-arg name="authSession" 
                   expression="@(AuthSessionManager).CurrentAuthSession" />
</object>

允许表达式计算为 null,所以你不必告诉 Spring 任何东西。
这在一个简单的情况下对我有用(没有嵌套上下文)。

You can use an expression to retrieve an object from the spring context in your constructor argument, something like:

<object name="someObject" singleton="false" 
        type="SomeType.someObject, SomeAssembly">
  <constructor-arg name="authSession" 
                   expression="@(AuthSessionManager).CurrentAuthSession" />
</object>

Expressions are allowed to evaluate to null, so you don't have to tell Spring anything.
This worked for me in a simple case (no nested contexts).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文