原文鏈接:http://www.orlion.ga/189/
一、scope
bean的scope屬性中常用的有兩種:singleton(單例,默認)和prototype(原型,每次創建新對象)
例:beans.xml
<bean id="userService" class="ml.orlion.service.UserService" scope="prototype"> <property name="userDao" ref="userDao"/> </bean>
在java文件中:
BeanFactory appContext = new ClassPathXmlApplicationContext("beans.xml"); UserService userService = (UserService)appContext.getBean("userService"); UserService userServic2 = (UserService)appContext.getBean("userService"); System.out.println(userService == userServic2);// 輸出false
二、集合注入
UserDAOImpl.java:
package ml.orlion.dao.impl; import java.util.List; import java.util.Map; import java.util.Set; import ml.orlion.dao.UserDAO; import ml.orlion.model.User; public class UserDAOImpl implements UserDAO{ private Set<String> sets; private List<String> lists; private Map<String , String> maps; public void saveUser(User user){ System.out.println("save usering"); } public Set<String> getSets() { return sets; } public void setSets(Set<String> sets) { this.sets = sets; } public List<String> getLists() { return lists; } public void setLists(List<String> lists) { this.lists = lists; } public Map<String, String> getMaps() { return maps; } public void setMaps(Map<String, String> maps) { this.maps = maps; } public String toString(){ return sets.size() +"|"+ lists.size() +"|"+ maps.size(); } }
beans.xml中配置:
<bean id="userDao" class="ml.orlion.dao.impl.UserDAOImpl"> <property name="sets"> <set> <value>1</value> </set> </property> <property name="lists"> <list> <value>1</value> <value>2</value> </list> </property> <property name="maps"> <map> <entry key="1" value="1"></entry> <entry key="2" value="2"></entry> <entry key="3" value="3"></entry> </map> </property> </bean>
然後測試一下:
BeanFactory appContext = new ClassPathXmlApplicationContext("beans.xml"); UserDAO userDao = (UserDAO) appContext.getBean("userDao"); System.out.println(userDao);// 輸出 1|2|3
三、自動裝配AutoWire
autowire屬性有六種值:autodatact、byName、byType、constructor、default、no
byName:按照名字自動去匹配
byType:按照類型自動匹配
UserDAOImpl.java:
package ml.orlion.dao.impl; import ml.orlion.dao.UserDAO; import ml.orlion.model.User; public class UserDAOImpl implements UserDAO{ // 添加id以區分 private int daoId; public int getDaoId() { return daoId; } public void setDaoId(int daoId) { this.daoId = daoId; } public void saveUser(User user){ System.out.println("save usering"); } public String toString(){ return "daoId" + daoId; } }
beans.xml:
<bean id="userDao" class="ml.orlion.dao.impl.UserDAOImpl"> <property name="daoId" value="1"></property> </bean> <bean id="userDao2" class="ml.orlion.dao.impl.UserDAOImpl"> <property name="daoId" value="2"></property> </bean> <bean id="userService" class="ml.orlion.service.UserService" scope="prototype" autowire="byName"> </bean>
測試一下:
BeanFactory appContext = new ClassPathXmlApplicationContext("beans.xml"); UserService userService = (UserService) appContext.getBean("userService"); System.out.println(userService.getUserDao());
這時候會輸出daoId=1
注:如果所有的bean都用同一種,可以使用beans的屬性:defalut-autowire
四、生命周期
1、lazy-init
lazy-init="true"則bean在Ioc容器初始化的時候不進行初始化
注:可以beans標簽上設置default-lazy-init="true"
2、init-method(bean初始化的時候執行)、destroy-method(bean銷毀的時候執行)
UserService.java
package ml.orlion.service; import ml.orlion.dao.UserDAO; import ml.orlion.dao.impl.UserDAOImpl; import ml.orlion.model.User; public class UserService { private UserDAO userDAO = new UserDAOImpl(); public void init(){ System.out.println("init"); } public void destroy(){ System.out.println("destroy"); } public UserDAO getUserDao() { return userDAO; } public void setUserDao(UserDAO userDAO) { this.userDAO = userDAO; } public void saveUser(User user){ this.userDAO.saveUser(user); } }
beans.xml:
<bean id="userDao" class="ml.orlion.dao.impl.UserDAOImpl"> <property name="daoId" value="1"/> </bean> <bean id="userService" class="ml.orlion.service.UserService" destroy-method="destroy" init-method="init"> <property name="userDao" ref="userDao"/> </bean>
測試:
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml"); UserService userService = (UserService) appContext.getBean("userService"); appContext.destroy();// 需要主動調用該方法才會銷毀bean
注:scope="prototype"時,調用destroy不會銷毀bean。Ioc容器不會管理bean的生命周期