1.如果bean A依赖bean B,Spring Ioc容器在调用A的setter方法之前,会先实例化B。换句话说,bean先实例化(非单例),然后设置依赖,然后调用相关声明周期方法(比如配置的init方法,或者是初始化回调函数)
2.ApplicationContext
解析上面的配置,在启动时,不会预先初始化这个标记为lazy的bean,为标记lazy的bean则会立刻初始化。
如果一个非延迟的单例bean依赖了lazy延迟bean,ApplicationContext
会在启动时就创建lazy延迟bean,因为它必须满足单例bean依赖。延迟bean注入给单例bean,就意味着,它不会延迟加载的。
3.
package fiona.apple;
// 不再有 Spring imports!
public abstract class CommandManager {
public Object process(Object commandState) {
// grab a new instance of the appropriate Command interface
//
Command command = createCommand();
// set the state on the (hopefully brand new) Command instance
command.setState(commandState);
return command.execute();
}
// okay... but where is the implementation of this method?
//okay....但是方法实现在哪里?
protected abstract Command createCommand();
}
<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
<!-- inject dependencies here as required -->
</bean>
<!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
<lookup-method name="createCommand" bean="command"/>
</bean>
在commandManager
类调用createCommand
方法时,动态代理类将会被识别为commandManager
返回一个command
bean的实例。将command
bean设置成prototype
,一定要小心处理。若被设置成了singleton
,每次调用将返回同一个command
bean。
4.使用<context:component-scan>
将会隐式的启用<context:annotation-config>
。当使用<context:component-scan>
时,一般不需要<context:annotation-config/>
元素