当我定义一个' MethodInvokingFactory'带有' scope = step'的bean,我得到一个错误,即bean的类型无法确定。当我更换' scope = step'与' lazy-init = true'。据我所知,两者都用于豆的后期绑定,除了那个差异。这两种方式之间还有其他差异吗?另外,我的用法是否正确?
请让我知道你对此的看法。
答案 0 :(得分:3)
从低级别的角度回答你的问题:
lazy-init="true"
表示在创建上下文时不会实例化bean,但是在引用上下文时将创建bean。由另一个豆。我认为这很清楚,也来自@AravindA的评论。
Scoped bean以不同的方式工作。创建上下文时,此bean将被包装到其他代理对象(默认情况下由CGLIB创建)中,该对象将传递给引用它的bean(此代理默认为singleton,例如shared)。因此,每次在运行时在代理上调用方法时,Spring都会与调用相交,请求工厂返回bean的实例并调用该bean上的方法。工厂轮流可以查找“真正的”bean实例,例如在HTTP请求(“请求”范围)或HTTP会话(“会话”范围)和/或必要时创建新实例。延迟实例化允许使用“运行时”(范围)值初始化作用域bean,例如, HTTP请求/会话中的值,在创建上下文时显然是未定义的。特别是“step”-scoped bean绑定到本地线程(请记住,并行运行步骤以进行分区)。因此,当您对它们调用方法时,将取消引用作用域bean。最后,通过在scoped bean上设置为另一个bean(例如在setter中)之后调用任何方法,可以轻松地打破这个优雅的Spring“意识形态”:)
答案 1 :(得分:1)
One thing to understand about lazy-initialization is that even though a bean
definition may be marked up as being lazy-initialized, if the lazy-initialized
bean is the dependency of a singleton bean that is not lazy-initialized, when the
ApplicationContext is eagerly pre-instantiating the singleton, it will have to
satisfy all of the singletons dependencies, one of which will be the
lazy-initialized bean!
Using a scope of Step is required in order to use late binding since the bean
cannot actually be instantiated until the Step starts, which allows the
attributes to be found. Because it is not part of the Spring container by
default, the scope must be added explicitly, either by using the batch namespace
or by including a bean definition explicitly for the StepScope (but not both):
<bean class="org.springframework.batch.core.scope.StepScope" />
scope =“step”与Lazy初始化无关。它用于在“Step”内部对参数进行后期绑定。
答案 2 :(得分:1)
步骤范围专门针对latebinding of job/step attributes,而不是真正用于bean的后期绑定,这意味着spring bean context / factory将增强stepcoped bean并查找要设置的属性,例如:
value="#{jobParameters[input.file.name]}