转载请注明来源-作者@loongshawn:http://blog.youkuaiyun.com/loongshawn/article/details/71680184,建议读者阅读原文,确保获得完整的信息
1.前言说明
本文仅对Spring依赖注入做简要说明,通过一个实例来对比使用前后的代码差异在哪里,其实不管是Spring或依赖注入等,其最根本的诉求就是简化Java开发人员的负担,让开发人员更专注业务逻辑本身,即需要什么组件就定义一个什么,至于这个组件是啥来头,咱不去关注。
所以,本文就先捋捋思路,看看依赖注入究竟实现效果如何。本文讲的还比较浅显,至于更加详细和深入的说明,网络上很多资源,可以进行详细了解。
2.现状说明
原始代码如下,没有使用依赖注入,TransitIssueUpdate.action方法中需要引入PoiToRoadStationMapper、PoiToRoadStation两个接口类。
public class TransitIssueUpdate {
private static final Logger logger = LoggerFactory.getLogger(TransitIssueUpdate.class);
public Integer action(String poiid,Integer poi_status){
PoiToRoadStationMapper poiToRoadStationMapper = MyApplicationContextUtil.getBean("poiToRoadStationMapper");
Integer count = 0;
if (poiid != null && ((poi_status == 1) || (poi_status == 2))){
PoiToRoadStation issue = new PoiToRoadStation();
issue.setPoi_id(poiid);
issue.setPoi_status(poi_status);
count = poiToRoadStationMapper.update(issue);
} else {
logger.info("TransitIssueUpdate.method [Result]: 参数不符合规范 POIID-" + poiid + ",POI_STATUS-" + poi_status);
}
logger.info("TransitIssueUpdate.method [Result]: " + count);
return count;
}
}
以上代码实现方式也能够正常执行,唯一的问题就是没有进行依赖管理,PoiToRoadStation对象仍然随意new出。具体业务对象仍然需要自己管理所需对象。
3.依赖注入
我们通过setter方法注入依赖项,也是最常用的DI注入bean方式。其中有两对象需要注入:PoiToRoadStation、PoiToRoadStationMapper。
具体的注入方式包括两步:
- 1、Java类中通过属性注入的方式,自定义组件。
- 2、Spring配置文档中添加bean及其property和ref属性。
public class TransitIssueUpdateNew {
private static final Logger logger = LoggerFactory.getLogger(TransitIssueUpdateNew.class);
// 自定义组件
private PoiToRoadStation poiToRoadStation;
private PoiToRoadStationMapper poiToRoadStationMapper;
// DI via setter method 依赖注入
public void setPoiToRoadStation(PoiToRoadStation poiToRoadStation) {
this.poiToRoadStation = poiToRoadStation;
}
public void setPoiToRoadStationMapper(
PoiToRoadStationMapper poiToRoadStationMapper) {
this.poiToRoadStationMapper = poiToRoadStationMapper;
}
// update method
public Integer action(String poiid,Integer poi_status){
Integer count = 0;
if (poiid != null && ((poi_status == 1) || (poi_status == 2))){
poiToRoadStation.setPoi_id(poiid);
poiToRoadStation.setPoi_status(poi_status);
count = poiToRoadStationMapper.update(poiToRoadStation);
} else {
logger.info("TransitIssueUpdate.method [Result]: 参数不符合规范 POIID-" + poiid + ",POI_STATUS-" + poi_status);
}
logger.info("TransitIssueUpdate.method [Result]: " + count);
return count;
}
}
Spring配置文档如下,定义了两个bean:transitIssueUpdateNew、poiToRoadStation。其中transitIssueUpdateNew配置了2个参数,及需要注入的参数。
<!--transitIssueUpdateNewz注入依赖 -->
<bean id="transitIssueUpdateNew" class="com.xxxx.method.ces.TransitIssueUpdateNew">
<property name="poiToRoadStation" ref="poiToRoadStation"/>
<property name="poiToRoadStationMapper" ref="poiToRoadStationMapper"/>
</bean>
<bean id="poiToRoadStation" class="com.xxxx.domain.ces.PoiToRoadStation"/>
通过依赖注入这种技术手段,可以有效的管理类与类间的依赖关系。不用依赖注入前,类需要自己管理自己的依赖类,长久下去一个是耦合度上升,另一个是后期维护费劲。
引入依赖注入后,依赖关系交给Spring容器管理,具体业务对象无需自己负责查找或者创建与其关联的其他对象,这个工作交给容器,由容器来负责创建各个对象间的协作关系,进行装配(wiring)。业务对象只需定义自己所需其他对象即可。
4.运行结果
正常运行。