问题描述
为了解决这个问题,我正在查看我们的spring.net配置如何工作.
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>
在用户未登录的情况下,AuthSessionManager.CurrentAuthSession
将为空.在这种情况下,Spring.NET会引发异常:工厂对象返回空对象".
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".
如何告诉Spring在这种情况下可以接受空对象?
How can I tell Spring that the null object is acceptable in this case?
推荐答案
您可以使用表达式,可从构造函数参数中的spring上下文中检索对象,例如:
<object name="someObject" singleton="false"
type="SomeType.someObject, SomeAssembly">
<constructor-arg name="authSession"
expression="@(AuthSessionManager).CurrentAuthSession" />
</object>
表达式被允许计算为null,因此您不必告诉Spring任何东西.这对我来说很简单(没有嵌套上下文).
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).
这篇关于Spring.net-PropertyRetrievingFactoryObject-属性为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!