Spring 1.3 DI依賴注入
3.1 DI :依賴注入 Dependency Injection
is a 繼承
has a 依賴 類A依賴于另一個B(類)對象
依賴 :在一個類中的屬性使用另一個類
注入:setter方法指派
3.2 案例解析
之前:
Class UserServiceImpl{
//以前
private UserDao userDao=new UserServiceImpl();
//依賴
private UserDao userDao;
//注入
Public setUserDao(UserDao userDao)
this.userDao= userDao;
}
3.3 案例過程
1、建立UserDao接口
public interface UserDao {
void saveUser();
}
2、建立UserDaoImpl實作類
public class UserDaoImpl implements UserDao {
@Override
public void saveUser() {
System.out.println(“spring建立了執行個體,注入了”);
}
}
3.建立UserService接口
public interface UerService {
void saveUser();
}
4.建立UserServiceImpl 實作類
public class UserServiceImpl implements UerService {
// 以前需要建立對象
// private UserDao userDao=new UserDaoImpl();
//現在:進行依賴注入
private UserDao userDao;
//可以使用setter快捷鍵導入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public void saveUser() {
userDao.saveUser();
}
}
5.resources檔案下建立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.xsd">
<!--
bean :配置要被建立對象的類/配置由spring容器管理的類
id:給bean取名,用于擷取
class:類的全限定名
-->
<!--配置UseServiceImpl-->
<bean id="userServiceId2" class="com.whxh.test2.UserServiceImpl">
<!--setter注入:property配置依賴,name為依賴名稱,ref注入的另一個bean的id-->
<property name="userDao" ref="userDaoId"></property>
</bean>
<!--配置UseDaoImpl-->
<bean id="userDaoId" class="com.whxh.test2.UserDaoImpl"></bean>
</beans>
6.測試類
public class DITest {
@Test
public void iocTest(){
//建立spring容器
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“test2/beans2.xml”);
//擷取對象
UserServiceImpl userServiceId2 = applicationContext.getBean(“userServiceId2”, UserServiceImpl.class);
userServiceId2.saveUser();
}
}
運作測試類。
3.4 注入方式
1 建立address類
public class Address {
private String tel;
private String addrName;
}
2 建立person類
public class Person {
private String name;
private int age;
private Address homeAddr;
private Address companyAddr;
}
關系圖
3 注入: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.xsd">
<!--
bean :配置要被建立對象的類/配置由spring容器管理的類
id:給bean取名,用于擷取
class:類的全限定名
-->
<bean id="personId" class="com.whxh.test3.Person">
<property name="name" value="趙敏"></property>
<property name="age" value="18"></property>
<!--引用類型:ref-->
<property name="homeAddr" ref="homeAddressId"></property>
<property name="companyAddr" ref="comAddressId"></property>
</bean>
<bean id="homeAddressId" class="com.whxh.test3.Address">
<property name="addrName" value="北京朝陽區"></property>
<property name="tel" value="110"></property>
</bean>
<bean id="comAddressId" class="com.whxh.test3.Address">
<property name="addrName" value="北京中關村"></property>
<property name="tel" value="027"></property>
</bean>
</beans>
4 測試類
public class SetTest {
@Test
public void iocTest(){
//建立spring容器
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“test3/beans3.xml”);
//擷取對象
Person person =(Person) applicationContext.getBean(“personId”);
System.out.println("person = " + person);
}
}
運作測試類。
3.5 list注入案例
1、建立AllData 類 含數組、集合等類型
public class AllData {
private String[] arrayData;
private List listData;
private Set setData;
private Map<String,String> mapData;
private Properties propData;
}
2、注入檔案: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.xsd">
<bean id="allDataId" class="com.whxh.test4.AllData">
<!--
集合注入(标簽):數組 <array>
list <list>
map <map>
set <set>
properties <props>
-->
<property name="arrayData">
<array>
<value>CXK</value>
<value>蔡徐坤</value>
</array>
</property>
<property name="listData">
<list>
<value>JNTM</value>
<value>雞你太美</value>
</list>
</property>
<property name="setData">
<set>
<value>DS</value>
<value>屌絲</value>
</set>
</property>
<property name="mapData">
<map>
<entry key="王寶強" value="馬蓉"></entry>
<entry key="聽風" value="白字"></entry>
</map>
</property>
<property name="propData">
<props>
<prop key="張三">18</prop>
<prop key="李四">28</prop>
</props>
</property>
</bean>
</beans>
3 測試類
public class AllDataTest {
@Test
public void iocTest(){
//建立spring容器
ApplicationContext applicationContext=new ClassPathXmlApplicationContext(“test4/beans4.xml”);
//擷取對象
AllData allData =(AllData) applicationContext.getBean(“allDataId”);
System.out.println("allData = " + allData);
}
}
4.運作測試類。