方法一:用构造方法实例化bean
提供可靠的构造方法,不写默认调用无参构造方法
//配置 <bean id="bookDao" class="org.wcx.dao.impl.BookDaoImpl"/>
方法二:用静态工厂实例化bean
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class OrderDaoFactory { 2 public static OrderDao getOrderDao(){ 3 return new OrderDaoImpl(); 4 } 5 }静态工厂
//配置 <bean id="orderDao" class="org.wcx.dao.factory.OrderDaoFactory" factory-method="getOrderDao"/>
方法三:用实例工厂实例化bean
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class UserDaoFactory { public UserDao getUserOrder(){ return new UserDaoImpl(); } }实例工厂
//配置 <bean id="userDaoFactory" class="org.wcx.dao.factory.UserDaoFactory" /> <bean id="userDao" factory-bean="userDaoFactory" factory-method="getUserOrder"/>
方法四:FactoryBean
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class UserDaoFactoryBean implements FactoryBean<UserDao> { 2 3 @Override 4 public UserDao getObject() throws Exception { 5 return new UserDaoImpl(); 6 } 7 8 @Override 9 public Class<?> getObjectType() { 10 return UserDao.class; 11 }FactoryBean
//配置 <bean id="userDao" class="org.wcx.dao.factory.UserDaoFactoryBean"/>
标签:return,构造方法,class,bean,实例,public,四种 From: https://www.cnblogs.com/wahahahhh/p/17081535.html