<?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 id="userDao" class="cn.edu.hpu.dao.Impl.UserDaoImpl"> <property name="daoId" value="1"></property> </bean> <bean id="userDao2" class="cn.edu.hpu.dao.Impl.UserDaoImpl"> <property name="daoId" value="2"></property> </bean> <bean id="userService" class="cn.edu.hpu.service.UserService" > <property name="userDao" ref="u" /> </bean> </beans>在默认的情况下,我们会手动设定userService所引用的bean:<property name="userDao" ref="u" />,但是我们也可以不指定,由spring来帮我们自动确定,这个叫做AutoWire,自动装配。
<bean id="userService" class="cn.edu.hpu.service.UserService" scope="prototype" autowire="default"> </bean>即在bean属性栏里加 autowire="XXX"的属性, autowire有以下几个参数可以设定:
<bean id="userService" class="cn.edu.hpu.service.UserService" scope="prototype" autowire="byName"> </bean>
package cn.edu.hpu.dao.Impl; import java.util.List; import java.util.Map; import java.util.Set; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserDaoImpl implements UserDao{ private int daoId; public int getDaoId() { return daoId; } public void setDaoId(int daoId) { this.daoId = daoId; } public void save(User u) { System.out.println("add success!!"); } @Override public String toString() { return "daoid="+daoId; } }改写了toString方法,打印daoId,看看下面注入了哪个UserDao
@Test public void testAdd() throws Exception{ BeanFactory ctx=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ctx.getBean("userService"); System.out.println(userService.getUserDao()); }结果:
<?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 id="userDao2" class="cn.edu.hpu.dao.Impl.UserDaoImpl"> <property name="daoId" value="2"></property> </bean> <bean id="userService" class="cn.edu.hpu.service.UserService" autowire="byType"> </bean> </beans>
package cn.edu.hpu.dao.Impl; import java.util.List; import java.util.Map; import java.util.Set; import cn.edu.hpu.dao.UserDao; import cn.edu.hpu.model.User; public class UserDaoImpl implements UserDao{ private int daoId; public int getDaoId() { return daoId; } public void setDaoId(int daoId) { this.daoId = daoId; } public void save(User u) { System.out.println("add success!!"); } @Override public String toString() { return "daoid="+daoId; } }改写了toString方法,打印daoId,看看下面注入了哪个UserDao
@Test public void testAdd() throws Exception{ BeanFactory ctx=new ClassPathXmlApplicationContext("beans.xml"); UserService userService=(UserService)ctx.getBean("userService"); System.out.println(userService.getUserDao()); }结果:
也就是spring按照类型匹配上了<bean id="userDao2"...那一项。(大概是按测试类里的UserDao类型匹配的)
转载请注明出处:http://blog.csdn.net/acmman