实例化Bean的三种方式
一、构造方法方式
1.1 BookDaoImpl
package com.lin.dao.daoimpl;
import com.lin.dao.BookDao;
public class BookDaoImpl implements BookDao {
public BookDaoImpl() {
System.out.println("BookDao的无参构造器");
}
/**
* 数据层实现
*/
public String queryBooks() {
System.out.println("数据层查询书本方法");
return "数据层查询书本方法";
}
}
1.2 applicationContext.xml配置
<!--
bean标签:表示配置bean,
id属性:bean的id,使用容器可以通过id值获取对应的bean,在一个容器中id值唯一
class属性:表示给bean定义类型,即配置的bean的全路径类名
-->
<!--方式一:构造方法实例化bean-->
<bean id="bookDao" class="com.lin.dao.daoimpl.BookDaoImpl"></bean>
1.3 ConstructorMethodForInstance
package com.lin.beaninstance;
import com.lin.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* bean实例化方式一:构造方法方式
*/
public class ConstructorMethodForInstance {
public static void main(String[] args) {
//1.创建IoC容器对象,加载spring核心配置文件
ApplicationContext springcontext = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 从IOC容器中获取Bean对象(BookDao对象)
BookDao bookDao = (BookDao) springcontext.getBean("bookDao");
//3 调用Bean对象(BookDao对象)的方法
bookDao.queryBooks();
}
}
运行结果
注意:无参构造方法如果不存在,将抛出异常BeanCreationException
二、静态工厂方式
2.1 BookDaoStaticFactory静态工厂
package com.lin.factory;
import com.lin.dao.BookDao;
import com.lin.dao.daoimpl.BookDaoImpl;
public class BookDaoStaticFactory {
//静态工厂创建对象
public static BookDao getBookDao(){
return new BookDaoImpl();
}
}
2.2 applicationContext.xml配置
<!--方式二:使用静态工厂实例化bean-->
<bean id="bookDao" class="com.lin.factory.BookDaoStaticFactory" factory-method="getBookDao"></bean>
2.3 关系图
2.4 StaticFactoryForInstance
package com.lin.beaninstance;
import com.lin.dao.BookDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* bean实例化方式二:静态工厂方式
*/
public class StaticFactoryForInstance {
public static void main(String[] args) {
//1.创建IoC容器对象,加载spring核心配置文件
ApplicationContext springContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//2 从IOC容器中获取Bean对象(BookDao对象)
BookDao bookDao = (BookDao) springContext.getBean("bookDao");
//3 调用Bean对象(BookDao对象)的方法
bookDao.queryBooks();
}
}
2.4 运行结果
三 、实例工厂方式
3.1 BookDaoFactory工厂类
package com.lin.factory;
import com.lin.dao.BookDao;
import com.lin.dao.daoimpl.BookDaoImpl;
public class BookDaoFactory {
//实例工厂创建对象
public BookDao getBookDao(){
return new BookDaoImpl();
}
}
3.2 applicationContext.xml配置
<!--方式三:使用实例工厂实例化bean-->
<bean id="bookDaoFactory" class="com.lin.factory.BookDaoFactory"></bean>
<bean id="bookDao" factory-bean="bookDaoFactory" factory-method="getBookDao"></bean>
3.3 关系图
3.4 实现FactoryBean<T>方式
定义BookDaoFactoryBean实现FactoryBean
package com.lin.beaninstance;
import com.lin.dao.BookDao;
import com.lin.dao.daoimpl.BookDaoImpl;
import org.springframework.beans.factory.FactoryBean;
public class BookDaoFactoryBean implements FactoryBean<BookDao> {
@Override
public BookDao getObject() throws Exception {
return new BookDaoImpl();
}
@Override
public Class<?> getObjectType() {
return BookDao.class;
}
}
applicationContext.xml配置
<bean id="bookDao" class="com.lin.beaninstance.BookDaoFactoryBean"></bean>
getObjectType() {
return BookDao.class;
}
}
#### applicationContext.xml配置
~~~xml
<bean id="bookDao" class="com.lin.beaninstance.BookDaoFactoryBean"></bean>