spring:bean的细节之三种创建Bean对象的方式

<!--创建Bean的三种方式-->
<!--第一种方式,使用默认构造函数创建
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有属性和标签时。
采用的就是默认构造函数创建bean对象,此时如果类没有默认构造函数,则对象无法创建
-->
<bean id="accountService" class="cn.flypig666.service.impl.AccountServiceImpl"></bean>

 
<!--第二种方式:使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)------------创建jar包中的类时可使用此方法
factory-bean:指定工厂bean
factory-method:指定哪个方法来获取对象
-->
<bean id="instanceFactory" class="cn.flypig666.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>

 
<!--第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)-->
<bean id="accountService" class="cn.flypig666.factory.StaticInstanceFactory" factory-method="getAccountService"></bean>

猜你喜欢

转载自www.cnblogs.com/flypig666/p/11511512.html