Spring——IOC(三)
六、集合注入
(1)假设UserDAOImpl中有sets,lists,maps三个集合属性
package com.zgy.impl;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.zgy.dao.UserDAO;
import com.zgy.model.User;
public class UserDAOImpl implements UserDAO{
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 void save(User u) {
System.out.println("user saved");
}
public String toString(){
return "sets size"+sets.size()+"| lists size:"+lists.size()+"| maps size"+maps.size();
}
}
(2)配置文件中配集合元素的值
<?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来标识 -->
<bean name="userDAO" class="com.zgy.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 name="userService" class="com.zgy.service.UserService" scope="prototype">
<!-- <property name="userDAO" ref="u" /> -->
<constructor-arg>
<ref bean="u"/>
</constructor-arg>
</bean>
</beans>
(3)验证:
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zgy.dao.UserDAO;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserDAO userDAO = (UserDAO) applicationContext.getBean("userDAO");
System.out.println(userDAO);
}
}
(4)结果:
sets size2| lists size:3| maps size4
七、自动装配
自动装配的方式有如下几种实现形式:
byName、byType、autodetect、constructor、default、no
以下测试byName的形式的自动装配
(1)假设有如下的配置文件,配置了如下的三个bean:
userDAO、userDAO2、userService
配置文件如下:
<?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来标识 -->
<bean name="userDAO" class="com.zgy.impl.UserDAOImpl">
<property name="daoId" value="1"></property>
</bean>
<bean name="userDAO2" class="com.zgy.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean name="userService" class="com.zgy.service.UserService" autowire="byName">
</bean>
</beans>
以上的配置中,userService这个bean是由按名称自动装配的,在UserService中含有UserDAO这个对象,所以,按照byName的方式可以找到userDAO这个bean,完成自动装配。
(2)UserService类如下:
package com.zgy.service;
import com.zgy.dao.UserDAO;
import com.zgy.model.User;
public class UserService {
private UserDAO userDAO;
public void add(User u){
this.userDAO.save(u);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
}
(3)UserDAOImpl类如下:
package com.zgy.impl;
import com.zgy.dao.UserDAO;
import com.zgy.model.User;
public class UserDAOImpl implements UserDAO{
private int daoId;
public int getDaoId() {
return daoId;
}
public void setDaoId(int daoId) {
this.daoId = daoId;
}
@Override
public void save(User u) {
System.out.println("user saved");
}
@Override
public String toString(){
return "daoId="+daoId;
}
}
(4)测试:
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zgy.dao.UserDAO;
import com.zgy.service.UserService;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserService userService= (UserService) applicationContext.getBean("userService");
System.out.println(userService.getUserDAO());
}
}
(5)结果:
daoId=1
以下测试byType的形式的自动装配
(1)修改配置文件成如下形式;
<?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来标识 -->
<!--
<bean name="userDAO" class="com.zgy.impl.UserDAOImpl">
<property name="daoId" value="1"></property>
</bean>
-->
<bean name="userDAO2" class="com.zgy.impl.UserDAOImpl">
<property name="daoId" value="2"></property>
</bean>
<bean name="userService" class="com.zgy.service.UserService" autowire="byType">
</bean>
</beans>
(2)再次测试
(3)结果:
daoId=2
八、生命周期
A)在配置文件中,配置一个bean的时候,可以指定一个lazy-init属性,该属性设置为true的时候,指明该bean先不进行初始化,而是在用到的时候,在使用getBean的时候才会初始化该bean。使用的场景:应用启动时间过长,可以使用此方法。
B)init-method和destory-method不要和prototype一起使用
(1)UserService类中增加init()和destory()方法:
package com.zgy.service;
import com.zgy.dao.UserDAO;
import com.zgy.model.User;
public class UserService {
private UserDAO userDAO;
public void init(){
System.out.println("init");
}
public void add(User u){
this.userDAO.save(u);
}
public UserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(UserDAO userDAO) {
this.userDAO = userDAO;
}
public void destory(){
System.out.println("destroy");
}
}
(2)配置文件中的配置如下:
<?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来标识 -->
<bean name="u" class="com.zgy.impl.UserDAOImpl">
</bean>
<bean name="userService" class="com.zgy.service.UserService" init-method="init" destroy-method="destory">
</bean>
</beans>
(3)测试:
package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zgy.dao.UserDAO;
import com.zgy.service.UserService;
public class UserServiceTest {
@Test
public void testAdd() throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserService userService= (UserService) applicationContext.getBean("userService");
applicationContext.destroy();
}
}
(4)结果:
init
Destroy
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。