Spring自动装配

先在cn.csdn.hr.dao包中建立几个文件
public interface BaseDao {
}
public class BaseHibernateDaoImpl implements BaseDao {
}
public interface CustomerDao {
}
public class CustomerDaoImpl implements CustomerDao {
}
cn.csdn.hr.service包中
public interface CustomerService {
}
public class CustomerServiceImpl implements CustomerService {
	private CustomerDao customerDao;
	private BaseDao baseDao;
	//set注入方式
	public void setCustomerDao(CustomerDao customerDao) {
		this.customerDao = customerDao;
	}
	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}
	public CustomerDao getCustomerDao() {
		return customerDao;
	}
	public BaseDao getBaseDao() {
		return baseDao;
	}
	public CustomerServiceImpl(CustomerDao customerDao, BaseDao baseDao) {
		super();
		System.out.println("----------------------------------------");
		this.customerDao = customerDao;
		this.baseDao = baseDao;
	}	
}
自动装配分为五种类型
1、	Byname:根据属性名自动装配。此选项将检查容器并根据名字查找与属性完全一致的bean,并将其与属性自定装配。例如,在bean定义中将autowire设置为byName,而该bean包含master属性(同时提供setMaster(。。)方法),spring就会自动查找名为master的bean定义,并用它来装配给master属性
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">
  <!-- autowire 自动装配bean的配置属性
        byName 根据名称自动装配
        cn.csdn.hr.service.CustomerServiceImpl 属性名称 与  bean id的名称一致的时候 就自动装配
   -->
  <bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="byName"/>   
  <bean id="customerDao" class="cn.csdn.hr.dao.CustomerDaoImpl"/> 
  <bean id="baseDao" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/> 
</beans>
2、	byType:如果容器中存在一个指定属性类型相同的bean,那么将与该属性自动装配。如果存在多个该类型的bean,或者缺少该类型的bean,注入的全部是null
<?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">
  <!-- autowire 自动装配bean的配置属性
        byType根据类型自动装配
        cn.csdn.hr.service.CustomerServiceImpl 属性 类型  与  bean 中有相同类型的时候 就自动装配
   -->
  <bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="byType"/>   
  <bean id="customerDaoImpl" class="cn.csdn.hr.dao.CustomerDaoImpl"/> 
  <bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/>   
3、	constructor:在容器中自动查找与需要自动装配的bean的构造方法参数一致的一个或多个bean。如存在不确定的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">
  <!-- autowire 自动装配bean的配置属性
        constructor  根据类型自动装配  是构造器的参数
        cn.csdn.hr.service.CustomerServiceImpl 构造器参数类型  与  bean 中有相同类型的时候 就自动装配
   -->
  <bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="constructor"/>   
  <bean id="customerDaoImpl" class="cn.csdn.hr.dao.CustomerDaoImpl"/> 
  <bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/> 
</beans>
4、	autodetect:首先尝试使用constructor来自动装配,然后使用byType方式。不确定性的处理与constructor和byType方式一样
<?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">
  <!-- autowire 自动装配bean的配置属性
        autodetect  
        cn.csdn.hr.service.CustomerServiceImpl 有没有默认的构造  默认的构造器 就采用byType类型
        如果没有则采用constructor
   -->
  <bean id="customerServiceImpl" class="cn.csdn.hr.service.CustomerServiceImpl" autowire="autodetect"/>   
  <bean id="customerDaoImpl" class="cn.csdn.hr.dao.CustomerDaoImpl"/> 
  <bean id="baseDaoImpl" class="cn.csdn.hr.dao.BaseHibernateDaoImpl"/> 
</beans>
5、第五种是空值的情况

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值