1.<bean>
2.FactroyBean
创建EnitityManger为列
Persist类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.jiedada._08bean._02FactroyBean; public class Persist { public static EntityManger createEntityManger(){ return new EntityManger(); } }
EntityManger
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.jiedada._08bean._02FactroyBean; public class EntityManger { }
要实现FactroyBean接口,及使用其中的三种方法,这样我们就能在创建的时候生成EnitityManger
EntityMangerBean类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package cn.jiedada._08bean._02FactroyBean; import org.springframework.beans.factory.FactoryBean; public class EntityMangerBean implements FactoryBean<EntityManger> { public EntityManger getObject() throws Exception { return Persist.createEntityManger(); } public Class<?> getObjectType() { return EntityManger.class; } public boolean isSingleton() { return true; } }
3.静态工厂
需要继承调用静态方法
<bean id="phone" class="cn.jiedada._08bean._03staticFactroy.PhoneFactroy" factory-method="createPhone">
4.其他工厂
也一样的在工厂调用普通方法
<!--因为不是静态方法所以需要使用对象调用方法-->
<bean id="phoneFactroy" class="cn.jiedada._08bean.Factroy.PhoneFactroy" ></bean>
<bean id="phone" factory-bean="phoneFactroy" factory-method="createPhone"></bean>