gpt4 book ai didi

java - Spring - 所有的 bean 都被处理了吗?

转载 作者:行者123 更新时间:2023-12-04 02:00:13 24 4
gpt4 key购买 nike

我有一个用于我正在编写的 LDAP 应用程序的 beans.xml 文件。我允许用户选择多个 LdapContextSource。对于每一个,我都有一个不同的 bean,例如

<bean id="ldapTemplate" class="yyy.LdapTemplate">
<constructor-arg ref="contextSource1" />
</bean>
<bean id="contextSource1" class="xxx.LdapContextSource">
...
</bean>
<bean id="contextSource2" class="xxx.LdapContextSource">
...
</bean>
<bean id="contextSource3" class="xxx.LdapContextSource">
...
</bean>

您可以看到这些上下文源 bean 中只有一个被实例化,因为只有一个被 ldapTemplate bean 引用。但是,当我运行我的应用程序时,stdout 中的 Spring 日志消息提供了有关每个上下文源的信息,即使只依赖一个上下文源也是如此。

Jan 25, 2011 11:56:36 AM org.springframework.ldap.core.support.AbstractContextSource afterPropertiesSet INFO: Property 'userDn' not set - anonymous context will be used for read-write operations Jan 25, 2011 11:56:37 AM org.springframework.ldap.core.support.AbstractContextSource afterPropertiesSet INFO: Property 'userDn' not set - anonymous context will be used for read-write operations Jan 25, 2011 11:56:37 AM org.springframework.ldap.core.support.AbstractContextSource afterPropertiesSet INFO: Property 'userDn' not set - anonymous context will be used for read-write operations

我的问题是:

(1) Spring 对未被引用/依赖的上下文源做了什么?它们永远不应该在我的应用程序中实例化,让我担心的是 Spring 正在为每个 bean 提供日志信息。

(2) 我应该注释掉应用程序中未使用的上下文源 bean 吗?不加评论会有什么后果?标准做法是什么?

谢谢,
公里数

最佳答案

也许你可以看看 Lazy Loading of Beans .这是 Spring 2.5.x 文档中的相关解释...

The default behavior for ApplicationContext implementations is to eagerly pre-instantiate all singleton beans at startup. Pre-instantiation means that an ApplicationContext will eagerly create and configure all of its singleton beans as part of its initialization process. Generally this is a good thing, because it means that any errors in the configuration or in the surrounding environment will be discovered immediately (as opposed to possibly hours or even days down the line).

However, there are times when this behavior is not what is wanted. If you do not want a singleton bean to be pre-instantiated when using an ApplicationContext, you can selectively control this by marking a bean definition as lazy-initialized. A lazily-initialized bean indicates to the IoC container whether or not a bean instance should be created at startup or when it is first requested.

为了完整起见,这里有一个例子......

<bean id="contextSource1" class="xxx.LdapContextSource" lazy-init="true"/>

关于java - Spring - 所有的 bean 都被处理了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4797220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com