Dependency Injection,依赖注入
生活案例:
Usb接口

程序世界:
DI思想:
需求:服务层需要依赖持久层的对象,这个时候需要靠容器给服务层。
本质:赋值
赋值的方式:
1)set方法
2)构造方法
依赖对象注入
DIServiceImpl 依赖于 IocDaoImpl
IocDaoImpl
bean 声明如下所示:
<bean id="iocDaoBean" class="com.zyh.dao.impl.IocDaoImpl" init-method="init" destroy-method="destory" scope="prototype">
</bean>
DIServiceImpl
bean 声明如下所示:
<bean id="diService" class="com.zyh.service.impl.DIServiceImpl" >
<property name="iocDao" ref="iocDaoBean">
</property>
</bean>
UserDaoImpl 实现类如下:
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
Log.info("我是新添加的用户");
}
}
DIServiceImpl 实现类如下:
public class DIServiceImpl implements DIService {
private IocDaoImpl iocDao;
//set 注入
public void setIocDao(IocDaoImpl iocDao) {
this.iocDao = iocDao;
}
}
测试方法如下所示 :
@Test
public void testDIServiceImpl(){
ClassPathXmlApplicationContext classPathXmlApplicationContext=new ClassPathXmlApplicationContext("applicationContext2.xml");
DIServiceImpl diService = (DIServiceImpl) classPathXmlApplicationContext.getBean("diService");
diService.addService();
}
输出如下所示:

互相依赖如下所示 :

DI的本质就是赋值
赋值的方式:
1)set方法
2)构造方法
基本属性依赖注入:
这个是构造方法注入: 里面的index 属性 代表 构造方法里面的第n个参数 下标从 0 开始
<constructor-arg value="false" index="0"/>
实现类中加入构造方法 :
public DIServiceImpl(boolean isShow) {
this.isShow = isShow;
}
这个是set方法注入:
<property name="show" value="false"></property>
实体类中加入如下属性 以及 set 方法
private boolean isShow;
public void setShow(boolean show) {
isShow = show;
}
集合属性
数组: <property name="strs"><array><value>…
List :<property name="list"><list><value>…
Set: <property name="set"><set><value>…
Map: <property name="map"><map><entry key="username" value="qf"></entry>…
Properties: <property name="properties"><props><prop key="uname">admin</prop>…
属性文件没有value属性,是在节点之间赋值。
数组 :
<property name="arrays">
<array>
<value>张先生</value>
<value>魏女士</value>
</array>
</property>
private String[] arrays;
public void setArrays(String[] arrays) {
this.arrays = arrays;
}
Log.info( "数组====="+Arrays.toString(getArrays()));
List :
<property name="lists">
<list>
<ref bean="zyh" ></ref>
<ref bean="wx"></ref>
</list>
</property>
需要注意的是上面的 ref 里面是用的bean 属性 这也就说明了 我们 list 存入的是User对象 ,
所以这时候 我们需要同时注入 User对象
<bean id="zyh" class="com.zyh.pojo.User">
<property name="age">
<value>12</value>
</property>
<property name="name">
<value>张颖豪</value>
</property>
</bean>
<bean id="wx" class="com.zyh.pojo.User">
<property name="age">
<value>14</value>
</property>
<property name="name">
<value>魏雪</value>
</property>
</bean>
private List<User> lists;
public void setLists(List<User> lists) {
this.lists = lists;
}
Log.info("List===="+getLists());
Set :
<property name="sets">
<set>
<ref bean="zyh"></ref>
<ref bean="wx"></ref>
</set>
</property>
同样 在 set 里面 注入的 也是对象 所以 也需要 将 ref 标签内的 ref bean 属性 指向 上面的 User 的
bean
private Set<User> sets;
public void setSets(Set<User> sets) {
this.sets = sets;
}
Log.info("Set===="+getSets());
Map :
<property name="maps">
<map>
<entry key="zyh" value-ref="zyh"></entry>
<entry key="wx" value-ref="wx"></entry>
</map>
</property>
引用的还是对象 不多说 。
private Map<String,User> maps;
public void setMaps(Map<String, User> maps) {
this.maps = maps;
}
遍历 Map :
Map<String, User> maps = getMaps();
Set<Map.Entry<String, User>> entries = maps.entrySet();
for (Map.Entry<String, User> entry : entries) {
Log.info("Map"+entry.getKey()+"======"+entry.getValue());
}
Properties:
<property name="properties">
<props>
<prop key="zyh">张颖豪</prop>
<prop key="wx">魏雪</prop>
</props>
</property>
private Properties properties;
public void setProperties(Properties properties) {
this.properties = properties;
}
Properties 的遍历需要特别注意 :
Properties properties = getProperties();
Set<String> strings = properties.stringPropertyNames();
for (String string : strings) {
Log.info("Properties"+string+"====="+properties.getProperty(string));
}
所有的集合输出如下所示 :

需要注意的是 : 我们操作集合的时候都选择使用的是set方法注入的。还有集合有的特殊的标签以及写法需要特别注意。
网友评论