6.26 spring 创建bean

6.26 spring 创建bean
1,通过构造函数创建bean
  < bean id = " baseDAO "   class = " com.aimy.core.dao.impl.BaseDAOImpl " >
相当于
basedaoimpl baseDAO 
=   new  BaseDAOImpl();
2,通过静态工厂创建bean
< bean id = " driver1 "   class = " com.aimy.driver.driverManager "
factory
- method = " createInstance " />
相当于
Object driver1 
=  driverManager.createInstance();

其中没有指定driverManager.createInstance();返回对象的类型,而且createInstance()必须是static方法
3,通过实例工厂方法创建bean
<bean id = " father "   class = " com.aimy.Father " />
........
< bean id = " son "  factory - bean = " father "  factory - method = " createSon " />
相当于
Father father 
=   new  Father();
Object son 
=  father.createSon();

其中father和son必须在同一个bean工厂或父bean工厂,即在同一个applicationcontext或子applicationcontext


二、如果applicationcontext默认预实例化那些singleton bean,这样就可以在beanfactory装载时就检查出错误,而如果singleton bean设置为lazy-mode,

你可能感兴趣的:(6.26 spring 创建bean)