【spring框架】AutoWire自动装配

看我们配置的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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

光仔December

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值