Bean是如何创建的

  • bean本质上就是对象,创建bean使用构造方法完成。

实例化Bean的几种方式

构造方法方式

  • BookDaoImpl实现类:
1
2
3
4
5
6
7
8
public class BookDaoImpl implements BookDao {
public BookDaoImpl() {
System.out.println("book dao constructor is running ....");
}
public void save() {
System.out.println("book dao save ...");
}
}
  • applicationContext.xml配置:
1
2
<!--方式一:构造方法实例化bean-->
<bean id="bookDao" class="edu.heuet.dao.impl.BookDaoImpl"/>
  • AppForInstanceBook测试类:
1
2
3
4
5
6
7
8
9
public class AppForInstanceBook {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

BookDao bookDao = (BookDao) ctx.getBean("bookDao");

bookDao.save();
}
}
  • 运行结果:

content-701

  • 注意:无参构造方法如果不存在,将抛出异常BeanCreationException

静态工厂方式

  • OrderDao接口和OrderDaoImpl实现类:
1
2
3
4
5
6
7
8
public interface OrderDao {
public void save();
}
public class OrderDaoImpl implements OrderDao {
public void save() {
System.out.println("order dao save ...");
}
}
  • OrderDaoFatory工厂类:
1
2
3
4
5
6
7
//静态工厂创建对象
public class OrderDaoFactory {
public static OrderDao getOrderDao(){
System.out.println("factory setup....");
return new OrderDaoImpl();
}
}
  • applicationContext.xml配置:
1
2
<!--方式二:使用静态工厂实例化bean-->
<bean id="orderDao" class="edu.heuet.factory.OrderDaoFactory" factory-method="getOrderDao"/>

content-702

  • AppForInstanceOrder测试类:
1
2
3
4
5
6
7
8
9
public class AppForInstanceOrder {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

OrderDao orderDao = (OrderDao) ctx.getBean("orderDao");

orderDao.save();
}
}
  • 运行结果

content-703

实例工厂方式

  • UserDao接口和UserDaoImpl实现类:
1
2
3
4
5
6
7
8
public interface UserDao {
public void save();
}
public class UserDaoImpl implements UserDao {
public void save() {
System.out.println("user dao save ...");
}
}
  • UserDaoFactory工厂类
1
2
3
4
5
6
//实例工厂创建对象
public class UserDaoFactory {
public UserDao getUserDao(){
return new UserDaoImpl();
}
}
  • applicationContext.xml配置
1
2
3
4
<!--方式三:使用实例工厂实例化bean-->
<bean id="userFactory" class="edu.heuet.factory.UserDaoFactory"/>

<bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>

content-704

  • AppForInstanceUser测试类:
1
2
3
4
5
6
7
8
9
10
11
12
public class AppForInstanceUser {
public static void main(String[] args) {
// //创建实例工厂对象
// UserDaoFactory userDaoFactory = new UserDaoFactory();
// //通过实例工厂对象创建对象
// UserDao userDao = userDaoFactory.getUserDao();
// userDao.save();
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) ctx.getBean("userDao");
userDao.save();
}
}
  • 运行结果:

content-705

实现FactoryBean<T>方式

  • 定义UserDaoFactoryBean实现FactoryBean<UserDao>

UserDaoFactoryBean中实例化什么类型的对象泛型就是该类型。

1
2
3
4
5
6
7
8
9
10
11
//FactoryBean创建对象
public class UserDaoFactoryBean implements FactoryBean<UserDao> {
//代替原始实例工厂中创建对象的方法
public UserDao getObject() throws Exception {
return new UserDaoImpl();
}

public Class<?> getObjectType() {
return UserDao.class;
}
}
  • applicationContext.xml配置:
1
2
<!--方式四:使用FactoryBean实例化bean-->
<bean id="userDao" class="edu.heuet.factory.UserDaoFactoryBean"/>