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

ktm*_*124 2 java spring ldap spring-ldap

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

<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>
Run Code Online (Sandbox Code Playgroud)

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

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

我的问题是:

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

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

谢谢,
ktm

And*_*ite 9

也许你可以查看Lazy Loading of Beans.以下是Spring 2.5.x文档中的相关说明......

ApplicationContext实现的默认行为是在启动时急切地预先实例化所有单例bean.预实例化意味着ApplicationContext将急切地创建和配置其所有单例bean作为其初始化过程的一部分.通常这是一件好事,因为这意味着将立即发现配置或周​​围环境中的任何错误(而不是可能是几小时甚至几天).

但是,有时候这种行为不是想要的.如果您不希望在使用ApplicationContext时预先实例化单例bean,则可以通过将bean定义标记为延迟初始化来有选择地控制它.一个延迟初始化的bean向IoC容器指示是否应该在启动时或首次请求时创建bean实例.

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

<bean id="contextSource1" class="xxx.LdapContextSource" lazy-init="true"/>
Run Code Online (Sandbox Code Playgroud)