小程序原生组件—提升你的小程序体验
1177
2022-10-01
Spring实例化bean的四种方式详解
目录一、bean实例化——构造方法(常用)二、bean实例化——静态工厂(了解)三、bean实例化——实例工厂(了解)四、bean实例化——FactoryBean(实用)
一、bean实例化——构造方法(常用)
bean本质上就是对象,创建bean使用构造方法完成
BookDao接口:
public interface BookDao {
public void save();
}
BookDaoImpl实现类,利用构造方式提供可访问的构造方法,输出相应字符串:
import com.itheima.dao.BookDao;
public class BookDaoImpl implements BookDao {
jhvQy
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) {
http:// 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 AppForInsthttp://anceUser {
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;
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~