看我们配置的beans.xml:
<?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有以下几个参数可以设定:
no、byType、byName、default(常用的是这四个)
默认的是no,即需要手动来设定。
byName是按照名字去匹配。
byType是按照类型去匹配。
default是指定你在<beans...中设定的 default-autowire="XXX"
byName测试:
beans.xml中设置:
<bean id="userService" class="cn.edu.hpu.service.UserService" scope="prototype" autowire="byName"> </bean>
UserDaoImpl.java:
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()); }
结果:
daoid=1
也就是spring按照名字匹配上了<bean id="userDao"...那一项。(大概是按测试类里的UserDao类名来匹配的)
byType测试:
beans.xml中设置:
<?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>
UserDaoImpl.java:
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()); }
结果:
daoid=2
也就是spring按照类型匹配上了<bean id="userDao2"...那一项。(大概是按测试类里的UserDao类型匹配的)
转载请注明出处:http://blog.csdn.net/acmman
时间: 2024-09-23 18:30:31