bean基本配置
别名配置
bean基本范围(就是说可以创建一个对象还是多个对象)
bean的实例化:
1、提供可访问的构造方法
Spring创建bean的时候调用的是无参的构造方法
无参构造方法如果不存在,将抛出异常BeanCreationException
2、使用静态工厂实例化bean(早些年使用率高)
public class OrderDaoFactory {
public static OrderDao getOrderDao(){
return new OrderDaoImpl();
}
}
配置
<bean
id="orderDao"
factory-method="getOrderDao"
class="com.itheima.factory. orderDaoFactory"/>
3、使用实例工厂实例化bean(了解)
实例工厂
public class UserDaoFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
配置
<bean id="userDaoFactory' class="com.itheima.factory.UserDaoFactory" />
<bean
id="userDao"
factory-method="getUserDao"
factory-bean="userDaoFactory"/>
3、FactoryBean
FactoryBean
public class UserDaoFactoryBean implements FactoryBean<UserDao>{
//代替原始实例工厂中创建对象的方法
public UserDao getobject( ) throws Exception {
return new UserDaoImp1();
}
public class< ?> getobjectType() {
return UserDao.class;
}
}
配置
<bean
id="userDao"
class="com.itheima.factory.UserDaoFactoryBean"/ >