这篇文章主要介绍了Spring实例化bean的方式有哪些的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Spring实例化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配置:
<!--方式一:构造方法实例化bean--> <bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"/>
执行结果:
无参构造方法如果不存在,将抛出异常BeanCreationException
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--> <bean id="orderDao" class="com.itheima.factory.OrderDaoFactory" factory-method="getOrderDao"/>
执行结果:
这种方式是为了兼容早期的遗留系统使用,了解即可。
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--> <bean id="userFactory" class="com.itheima.factory.UserDaoFactory"/> <bean id="userDao" factory-method="getUserDao" factory-bean="userFactory"/>
执行结果:
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<UserDao> { //代替原始实例工厂中创建对象的方法 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配置:
<!--方式四:使用FactoryBean实例化bean--> <bean id="userDao" class="com.itheima.factory.UserDaoFactoryBean"/>
执行结果:
对象默认单例,需要改非单例时
在UserDaoFactoryBean重写isSingleton方法,true为单例模式,false为非单例模式
通常为单例,这个方法通常不写
public boolean isSingleton() { return true; }
关于“Spring实例化bean的方式有哪些”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“Spring实例化bean的方式有哪些”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。