Spring Framework学习之路(12)--- 使用依赖与懒加载

1.4.3. Using depends-on

1.4.3. 使用依赖

  If a bean is a dependency of another that usually means that one bean is set as a property of another. Typically you accomplish this with the <ref/> element in XML-based configuration metadata. However, sometimes dependencies between beans are less direct; for example, a static initializer in a class needs to be triggered, such as database driver registration. The depends-onattribute can explicitly force one or more beans to be initialized before the bean using this element is initialized. The following example uses the depends-on attribute to express a dependency on a single bean:
  如果一个bean是另一个bean的依赖,那通常意味着一个bean被设置为另一个bean的属性。通常,您可以使用基于XML的配置元数据中的**<ref />**元素来完成此操作。但是,有时bean类之间的依赖性不那么直接;例如,类中的静态初始化器需要被触发,例如数据库驱动程序注册。 depends-on属性可以在使用此元素的bean初始化之前明确强制一个或多个bean被初始化。以下示例使用depends-on属性来表示对单个bean的依赖关系:

<bean id="beanOne" class="ExampleBean" depends-on="manager"/>
<bean id="manager" class="ManagerBean" />

  To express a dependency on multiple beans, supply a list of bean names as the value of the depends-on attribute, with commas, whitespace and semicolons, used as valid delimiters:
  要表示对多个bean的依赖关系,请提供一个bean名称列表作为depends-on属性的值,并使用逗号,空格和分号作为有效分隔符:

<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
    <property name="manager" ref="manager" />
</bean>

<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />

  The depends-on attribute in the bean definition can specify both an initialization time dependency and, in the case of singleton beans only, a corresponding destroy time dependency. Dependent beans that define adepends-on relationship with a given bean are destroyed first, prior to the given bean itself being destroyed. Thus depends-on can also control shutdown order.
  bean定义中的depends-on属性可以指定一个初始化时间依赖项,并且在单例bean的情况下,可以指定相应的销毁时间依赖项。定义依赖于给定bean的依赖关系的依赖bean在销毁给定bean之前首先销毁。因此,依赖也可以控制关闭顺序。

1.4.4. Lazy-initialized beans
1.4.4. 懒加载的 beans
  By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process. Generally, this pre-instantiation is desirable, because errors in the configuration or surrounding environment are discovered immediately, as opposed to hours or even days later. When this behavior is not desirable, you can prevent pre-instantiation of a singleton bean by marking the bean definition as lazy-initialized. A lazy-initialized bean tells the IoC container to create a bean instance when it is first requested, rather than at startup.
  默认情况下,ApplicationContext实现作为初始化过程的一部分,急切地创建和配置所有的singleton bean。通常,这种预先实例化是可取的,因为配置或周围环境中的错误是立即发现的,而不是几小时甚至几天后。当这种行为不可取时,可以通过将bean定义标记为lazy-initialized来防止单例bean的预先实例化。一个惰性初始化bean告诉IoC容器在第一次请求时创建一个bean实例,而不是在启动时。
  In XML, this behavior is controlled by the lazy-init attribute on the <bean/> element; for example:
  在XML中,此行为由**<bean />**元素上的lazy-init属性控制;例如:

<bean id="lazy" class="com.foo.ExpensiveToCreateBean" lazy-init="true"/>
<bean name="not.lazy" class="com.foo.AnotherBean"/>

  When the preceding configuration is consumed by an ApplicationContext, the bean named lazy is not eagerly pre-instantiated when the ApplicationContext is starting up, whereas the not.lazy bean is eagerly pre-instantiated.
  当一个ApplicationContext使用前面的配置时,名为lazy的bean在ApplicationContext启动时并不急于预先实例化,而not.lazy bean则急切地预先实例化。

  However, when a lazy-initialized bean is a dependency of a singleton bean that is not lazy-initialized, the ApplicationContextcreates the lazy-initialized bean at startup, because it must satisfy the singleton’s dependencies. The lazy-initialized bean is injected into a singleton bean elsewhere that is not lazy-initialized.
  然而,当一个懒惰初始化bean是一个未经过延迟初始化的单例bean的依赖时,ApplicationContext在启动时创建了懒惰初始化bean,因为它必须满足单例的依赖关系。懒惰初始化的bean被注入到其他地方的单身bean中,而不是延迟初始化的。

  You can also control lazy-initialization at the container level by using the default-lazy-init attribute on the <bean/> element; for example:
  您还可以通过在 <bean/> 元素上使用default-lazy-init属性来控制容器级别的延迟初始化;例如:

<beans default-lazy-init="true">
    <!-- no beans will be pre-instantiated... -->
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

工地码哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值