Spring - 所有豆子都经过加工吗?

发布于 2024-10-14 02:36:40 字数 1272 浏览 4 评论 0原文

我有一个正在编写的 LDAP 应用程序的 beans.xml 文件。我允许用户选择多个 LdapContextSource。对于每个 bean,我都有一个不同的 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 日志消息提供有关每个上下文源的信息,即使只依赖一个上下文源。

2011 年 1 月 25 日上午 11:56:36 org.springframework.ldap.core.support.AbstractContextSource afterPropertiesSet 信息:未设置属性“userDn” - 匿名上下文将用于读写操作 一月 25, 2011 11:56:37 AM org.springframework.ldap.core.support.AbstractContextSource afterPropertiesSet 信息:未设置属性“userDn” - 匿名上下文将用于读写操作 一月 25, 2011 11:56:37 AM org.springframework.ldap.core.support.AbstractContextSource afterPropertiesSet 信息:未设置属性“userDn” - 匿名上下文将用于读写操作

我的问题是:

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

(2) 我是否应该注释掉应用程序中未使用的上下文源bean?如果不注释它们会产生什么后果?标准做法是什么?

谢谢,
克特姆

I have a beans.xml file for an LDAP application that I am writing. I am allowing the user the choice of several LdapContextSource(s). For each one I have a different bean, e.g.

<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>

You can see that only one of these context source beans gets instantiated, because only one is referred to by the ldapTemplate bean. However, when I run my application, my Spring log messages in stdout provide information about each context source, even though only one is depended on.

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

My questions are:

(1) What is Spring doing with the context sources that are not referred to / depended on? They should never be instantiated in my application, and it worries me that Spring is providing log information for each of these beans.

(2) Should I comment out the context source beans that are not used in the application? What are the consequences of leaving them uncommented? What is the standard practice?

Thanks,
ktm

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

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

发布评论

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

评论(1

拥醉 2024-10-21 02:36:40

也许你可以看看 延迟加载豆子。这是 Spring 2.5.x 文档的相关解释...

默认行为
ApplicationContext 实现是
热切地预先实例化所有
启动时的单例 bean。
预实例化意味着
ApplicationContext 将急切地创建
并配置其所有单例
beans 作为其初始化的一部分
过程。一般来说,这是一个很好的
的事情,因为这意味着任何
配置错误或
周边环境将会
立即发现(而不是
可能会持续几个小时甚至几天
线)。

但是,有时这
行为不是我们想要的。 如果你
不希望出现单例 bean
使用时预先实例化
应用程序上下文,你可以
通过标记来选择性地控制这一点
bean 定义为延迟初始化
。一个
延迟初始化的 bean 表示
IoC 容器是否
bean 实例应该创建于
启动或首次请求时。

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

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

Maybe you could check out Lazy Loading of Beans. Here is the relevant explanation from the Spring 2.5.x docs...

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.

For the sake of completness here is an example...

<bean id="contextSource1" class="xxx.LdapContextSource" lazy-init="true"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文