先在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、第五种是空值的情况
Spring自动装配
最新推荐文章于 2024-04-24 10:07:18 发布