一、bean实例化——构造方法(常用)
bean本质上就是对象,创建bean使用构造方法完成
BookDao接口:
public interface BookDao { public void save(); }
BookDaoImpl实现类,利用构造方式提供可访问的构造方法,输出相应字符串:
import com.itheima.dao.BookDao; public class BookDaoImpl implements BookDao { public BookDaoImpl() { System.out.println("book dao constructor is running ...."); } public void save() { System.out.println("book dao save ..."); } }
AppForinstanceBook:
import com.itheima.dao.BookDao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppForInstanceBook { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); BookDao bookDao = (BookDao) ctx.getBean("bookDao"); bookDao.save(); } }
applictionContext.xml配置:
执行结果:
无参构造方法如果不存在,将抛出异常BeanCreationException
二、bean实例化——静态工厂(了解)
OrderDao接口:
public interface OrderDao { public void save(); }
OrderDaoImpl实现类:
import com.itheima.dao.OrderDao; public class OrderDaoImpl implements OrderDao { public void save() { System.out.println("order dao save ..."); } }
OrderDaoFactory静态工厂创建对象:
import com.itheima.dao.OrderDao; import com.itheima.dao.impl.OrderDaoImpl; //静态工厂创建对象 public class OrderDaoFactory { public static OrderDao getOrderDao(){ System.out.println("factory setup...."); return new OrderDaoImpl(); } }
AppForinstanceOrder:
import com.itheima.dao.OrderDao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppForInstanceOrder { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); OrderDao orderDao = (OrderDao) ctx.getBean("orderDao"); orderDao.save(); } }
applictionContext.xml配置:
执行结果:
这种方式是为了兼容早期的遗留系统使用,了解即可。
三、bean实例化——实例工厂(了解)
UserDao接口:
public interface UserDao { public void save(); }
UserDaoImpl实现类:
import com.itheima.dao.UserDao; public class UserDaoImpl implements UserDao { public void save() { System.out.println("user dao save ..."); } }
UserDaoFactory实例工厂创建对象:
import com.itheima.dao.UserDao; import com.itheima.dao.impl.UserDaoImpl; //实例工厂创建对象 public class UserDaoFactory { public UserDao getUserDao(){ return new UserDaoImpl(); } }
AppForInstanceUser:
import com.itheima.dao.UserDao; import com.itheima.factory.UserDaoFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppForInstanceUser { public static void main(String[] args) { //创建实例工厂对象 UserDaoFactory userDaoFactory = new UserDaoFactory(); //通过实例工厂对象创建对象 UserDao userDao = userDaoFactory.getUserDao(); userDao.save(); } }
因为不是静态方法,必须用工厂对象对象调用。
也可用Spring方法,不过要在xml文件中进行修改。
AppForInstanceUser:
import com.itheima.dao.UserDao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppForInstanceUser { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) ctx.getBean("userDao"); userDao.save(); } }
applictionContext.xml配置:
userFactory配合使用的,实际无意义
factory-method方法名不固定,每次都需要配置
执行结果:
四、bean实例化——FactoryBean(实用)
UserDao接口和UserDaoImpl实现类和实例化三一样。
UserDaoFactoryBean,代替了原始实例化工厂中创建对象的方法:
import com.itheima.dao.UserDao; import com.itheima.dao.impl.UserDaoImpl; import org.springframework.beans.factory.FactoryBean; //FactoryBean创建对象 public class UserDaoFactoryBean implements FactoryBean{ //代替原始实例工厂中创建对象的方法 public UserDao getObject() throws Exception { return new UserDaoImpl(); } public Class> getObjectType() { return UserDao.class; } }
AppForInstanceUser:
import com.itheima.dao.UserDao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AppForInstanceUser { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) ctx.getBean("userDao"); userDao.save(); } }
applictionContext.xml配置:
执行结果:
对象默认单例,需要改非单例时
在UserDaoFactoryBean重写isSingleton方法,true为单例模式,false为非单例模式
通常为单例,这个方法通常不写
public boolean isSingleton() { return true; }
到此这篇关于Spring实例化bean的四种方式详解的文章就介绍到这了,更多相关Spring实例化bean内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!