您好,登錄后才能下訂單哦!
這篇文章主要介紹了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。