集合装配
如果bean中有一些集合属性,配置文件的配置如下
package com.bjsxt.dao.impl; import java.util.List; import java.util.Map; import java.util.Set; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; public class UserDAOImpl implements UserDAO { /* (non-Javadoc) * @see com.bjsxt.dao.UserDAO#save(com.bjsxt.model.User) */ private Set<String> sets; private List<String> lists; private Map<String , String> maps; 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 UserDAOImpl() { } public void save(User user) { System.out.println("user saved!"); } @Override public String toString() { return "sets size:" + sets.size() + "| lists size:" + lists.size() + "| maps size:" + maps.size() ; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean name="userDAO" class="com.bjsxt.dao.impl.UserDAOImpl"> <property name="sets"> <set> <value>1</value> <value>2</value> </set> </property> <property name="lists"> <list> <value>1</value> <value>2</value> <value>3</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> <entry key="4" value="4"></entry> </map> </property> </bean> <bean id="userService" class="com.bjsxt.service.UserService"> <!-- <property name="userDAO"> <ref bean="userDAO"/> </property> --> <constructor-arg> <ref bean="userDAO"/> </constructor-arg> </bean> </beans>
自动装配
如果每个属性都使用ref配置,一个大项目的Spring配置会十分庞大,可以通过<bean>的autowire属性设置自动装配规则。使用后不需要再用<property>子标签设置该bean的属性、依赖关系。Spring会根据反射,自动寻找符合该条件的属性,设置到该bean属性上。如果autowire设置为byType,将会按照属性的类型自动装配,注意如果找到多个该属性类型的bean,会抛出异常。如果是byName,根据名称自动装配,例如如果bean中有个名为datasource的属性,Spring会把id为datasource的bean设置到该bean中
Bean的生命周期
lazy-init:spring默认的是在启动时创建所有的bean的对象,如果一个项目很大,有500个bean,这些bean会一次性全部加载到内存中,会造成启动速度特别慢,将这个属性设置为true,那么beanfactory在调用getbean方法的时候该bean才会被创建
init-method:bean在实例化之后执行的方法
destroy-method:bean使用完毕后执行的方法
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <bean lazy-init="true" id="u" class="com.bjsxt.dao.impl.UserDAOImpl"> </bean> <bean id="userService" class="com.bjsxt.service.UserService" init-method="init" destroy-method="destroy" scope="singleton"> <!-- <property name="userDAO" ref="u" /> --> <constructor-arg> <ref bean="u"/> </constructor-arg> </bean> </beans>
package com.bjsxt.service; import com.bjsxt.dao.UserDAO; import com.bjsxt.model.User; public class UserService { private UserDAO userDAO; public void init() { System.out.println("init"); } public void add(User user) { userDAO.save(user); } public UserDAO getUserDAO() { return userDAO; } public void setUserDAO(UserDAO userDAO) { this.userDAO = userDAO; } public UserService(UserDAO userDAO) { super(); this.userDAO = userDAO; } public void destroy() { System.out.println("destroy"); } }
package com.bjsxt.service; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bjsxt.model.User; //Dependency Injection //Inverse of Control public class UserServiceTest { @Test public void testAdd() throws Exception { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)ctx.getBean("userService"); UserService service2 = (UserService)ctx.getBean("userService"); ctx.destroy(); } }
执行结果
注:因为我们配置文件中bean的scope属性设置为了singleton,表示该对象单例,所以实例化两次其实还是同一个对象,如果改为prototype,会输出两个init,不会执行detroy-method指定的方法,具体原因不明,可以查阅相关资料