Recently I will deliver a session regarding dependency inversion principle to my team.
As Java Spring is already widely used in all other Java development teams in my site, some ABAPers are not well aware of its idea and implementation under the hood. In order for ABAPers to easily understand the mechanism of Java Spring dependency inversion, I wrote a prototype in ABAP after going through related Java source code of Spring.
Before I start, I perform the search in SCN. There are already several excellent blogs written regarding dependency injection in ABAP:
- ABAP Dependency Injection – An implementation approach
- Shoot Me Up ABAP
- Dependency Injection for ABAP
- MockA in github
Compared with those blogs, the advantage of my prototype is: it follows exactly the design of Java Spring, it is not needed for users to do any other manual dependency registration except a single annotation @Inject. So it is useful for ABAPers to understand Spring dependency internal implementation.
Let me begin with a simple example. In real world I have a switch. By pressing it, the lamp connected by that switch is turned on. With switch pressed for second time, the lamp is turned off. That’s all.
Implementation without using Dependency injection
I have an interface ZIF_SWITCHABLE with two simple methods:
And a ZCL_LAMP which simply implements this interface:
CLASS ZCL_LAMP IMPLEMENTATION.
method ZIF_SWITCHABLE~OFF.
WRITE: / 'lamp off'.
endmethod.
method ZIF_SWITCHABLE~ON.
WRITE: / 'lamp on'.
endmethod.
ENDCLASS.
And a switch which internally maintains the current switch status and a reference to ZIF_SWITCHABLE:
The switch has a push method to toggle:
METHOD push.
IF isswitchon = abap_true.
mo_switchable->off( ).
isswitchon = abap_false.
ELSE.
mo_switchable->on( ).
isswitchon = abap_true.
ENDIF.
ENDMETHOD.
And a setter method is needed to inject the switchable instance:
method SET_SWITCHABLE.
mo_switchable = io_switchable.
endmethod.
These two classes and one interface are put to the following package:
Consumer code – version one without using dependency injection
Here the ZCL_SWITCH has tight dependency on ZCL_LAMP: it has to manually inject this dependency via setter method in line 11.
Let’s summarize how many manual / unnecessary operations are done by consumer:
- line 8: create lamp instance
- line 9: create switch instance
- line 11: connect switch with lamp
Implementation using ABAP Summer
I call my prototype as ABAP Summer just to show my admire on Java Spring
When the same requirement is implemented in Java Spring, the logic in line 11 could completely be avoided, with help of various powerful annotation like@Autowired, @Named, @Inject etc. Thanks to Java Spring container, lots of labor work has been done by it under the hood, saving lots of routine effort from application developers so that they can only concentrate on the core business logic. For example, in Java using Spring, all developers need to do is to add annotation @Inject on top of attribute switchable – in the runtime Spring will guarantee that the annotated implementation for this interface is instantiated automatically.
How can we simulate the similar logic of Spring now in ABAP Summer?
(1) Add the annotation @Inject to attribute mo_switchable, which tells ABAP summer “hey, I would like this attribute to be automatically injected with proper implementation in the runtime”.
Since I have no other way to add metadata in class attribute in ABAP – there is no first class annotation supported in ABAP – I have to use description field for simulation.
(2) And below is my consumer code, no more manual instance initialization and manual setter call. Very clean, isn’t it?
data(summer) = zcl_summer=>get_instance( ).
data(lo_switch) = cast zcl_switch( summer->get_bean( EXPORTING iv_bean_name = 'ZCL_SWITCH' ) ).
lo_switch->push( ).
lo_switch->push( ).
Let’s make comparison. By using ABAP summer, consumer can simply get switch instance from container by passing bean technical name ( here again, I use Spring terminology “bean” to name those ABAP classes which owns an injected member attribute with @Inject ). This is exactly the way a Java developer doing daily work using Java Spring:
How does ABAP summer work?
There are lots of books written to illustrate the design of Java Spring, I read one of them listed below, and wrote this ABAP summer based on my understanding.
I draw a diagram below to explain the core method init of ABAP summer:
Last by not least, when you try this demo, after you copy the source code of ZCL_SWITCH to your system and activate it, NEVER forget to add this annotation in description field manually, as in ABAP, the attribute description is not stored in source code but in DB table.
More thought on ABAP annotation
The annotation in Java is a form of metadata which could be defined by application developer and are available in the runtime by Java reflection. It is a built-in language feature supported by JVM.
In ABAP there is no such first-class annotation supported. In CDS view, there are some grammar which have annotation-like style. You can append lots of annotation defined in SAP help to a CDS view.
However those annotation are not first-class annotation supported by ABAP language itself as well. It is just CDS view framework which parses the source code of these annotation and react accordingly. For details please see these two blogs of mine:
- My CDS view self study tutorial – Part 3 how is view source in Eclipse converted to ABAP view in the backend
- My CDS view self study tutorial – Part 4 how does annotation @OData.publish work
Further reading
I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:
- Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP
- Functional programming – Simulate Curry in ABAP
- Functional Programming – Try Reduce in JavaScript and in ABAP
- Simulate Mockito in ABAP
- A simulation of Java Spring dependency injection annotation @Inject in ABAP
- Singleton bypass – ABAP and Java
- Weak reference in ABAP and Java
- Fibonacci Sequence in ES5, ES6 and ABAP
- Java byte code and ABAP Load
- How to write a correct program rejected by compiler: Exception handling in Java and in ABAP
- An small example to learn Garbage collection in Java and in ABAP
- String Template in ABAP, ES6, Angular and React
- Try to access static private attribute via ABAP RTTI and Java Reflection
- Local class in ABAP, Java and JavaScript
- Integer in ABAP, Java and JavaScript
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关推荐
资源名称:sap abap开发从入门到精通 内容简介: 《SAP ABAP开发从入门到精通》以应用实例的形式,讲解了SAPABAP开发的相关知识点,详细介绍了SAPABAP开发者必须掌握的包与变更传输系统(CTS)、数据类型、数据库...
从实例学sap abap编程,从实例学sap abap编程,从实例学sap abap编程
SAP ABAP 数据库操作,快速学会ABAP!
《SAP ABAP开发技术详解(实例篇)(第二版) [1] 》除了对对SAP ABAP开发所需的常用技术(SAP报表、批量导入、对话程序、函数)做了详尽的介绍,包括基本报表、交互式报表、复杂报表、ALV(SAP List Viewer)报表、...
SAP ABAP开发学习——第9课:ALV2(视频教程) 1.SAP ABAP开发入门学习 2.含视频教程学习、PPT、作业 3.视频非常清晰
文档详细介绍了用myeclipse生成webservice的方法步骤,以及abap调用webservice 第一步:myeclipse生成webservice ...第三步:在sap系统生成webservice代理 第四步:生成port 第五步:用实例调用生成的webservice
ABAP开发从入门到精通_高清自学版 SAP+ABAP开发从入门到精通 SAP开发自学必读 SAP SAP开发自学入门到精通完整版HANA开发
详细讲述SAP ABAP的BADI增强的概述及使用。详细讲述SAP ABAP的BADI增强的概述及使用。详细讲述SAP ABAP的BADI增强的概述及使用。详细讲述SAP ABAP的BADI增强的概述及使用。详细讲述SAP ABAP的BADI增强的概述及使用。...
《SAP ABAP开发从入门到精通》以应用实例的形式,讲解了SAP ABAP开发的相关知识点,详细介绍了SAP ABAP开发者必须掌握的包与变更传输系统(CTS)、数据类型、数据库、模块化程序、内表、调试、ABAP数据字典、锁对象...
从零开始,教授如何配置 Eclipse 开发 SAP ABAP 程序环境。2020 年最新版本操作...ABAP Development Tool的Java端实现就是Eclipse的一个扩展,使用JCO(Java Connector)连接ABAP后台的adt服务,实现对ABAP程序的操作。
sap abap经典中文帮助 sap abap经典中文帮助
SAP ABAP 代码备份下载到本地程序代码
SAP ABAP开发学习——第19课:实战讲解(视频教程) 1.SAP ABAP开发入门学习 2.含视频教程学习、PPT、作业 3.视频非常清晰
sap abap程序 举例大全 包含所以ABAP开发程序类型
SAp abap 非常清晰的abap学习资料
SAP ABAP程序性能调优介绍.pptx
全面介绍SAP ABAP语言编程与开发(SAP官方文档)
SAP ABAP 常用T-CODE SAP ABAP
sap-abap
网上收集整理的SAP abap PA 试题2,还有部分资料