原文链接: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的生命周期