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:
(1) ABAP Dependency Injection – An implementation approach (2) Shoot Me Up ABAP (3) Dependency Injection for ABAP (4) MockA in github
Thanks a lot for efforts spent by authors of them! 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:
(1) line 8: create lamp instance (2) line 9: create switch instance (3) 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:
(1) My CDS view self study tutorial – Part 3 how is view source in Eclipse converted to ABAP view in the backend (2) My CDS view self study tutorial – Part 4 how does annotation @OData.publish work
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关推荐
接着,编写一个Java类`HelloBody`,其中包含一个方法`SayHelloTo`,用于接收字符串参数并返回一个问候语。 ```java public class HelloBody { public String SayHelloTo(String name) { return "hello: " + ...
JAVA端需要实现一个或多个函数来响应ABAP端的调用,并且处理传递过来的数据。 #### 小结 本案例详细展示了如何通过RFC实现在SAP ABAP与JAVA应用程序之间的数据交换。通过配置JCo服务器程序以及编写相应的ABAP和...
- **理解ABAP程序组件**:熟悉构成一个ABAP程序的不同部分。 - **理解ABAP程序类型**:了解不同类型的ABAP程序及其特点。 - **了解ABAP仓库对象的作用**:掌握ABAP仓库对象的基本概念及使用方法。 - **利用ABAP工作...
1. **Java Connector (JCo)**: JCo是SAP为Java开发者提供的一个API,它允许Java应用程序与SAP NetWeaver系统进行交互。JCo3是JCo的第三个版本,提供了更强大的功能和性能优化。 2. **RFC (Remote Function Call)**:...
为了实现不同系统的集成和数据交互,Java与ABAP之间的通信成为了一个重要的需求。本篇文章将深入探讨如何使用JCo(Java Connector)库来实现Java与ABAP系统通过RFC(Remote Function Call)进行数据互传的实例,并...
SAP ABAP 开发环境和开发工具是 SAP 系统中最重要的组件之一,它提供了一个强大的开发平台,允许开发者创建、测试和部署 ABAP 程序。ABAP 是 SAP 系统中的主要编程语言,用于开发业务应用程序。 在 SAP 系统中,...
标题中的“ABAP-AES-JAVA加密解密”是指在ABAP和JAVA两个不同的编程环境中实现AES(Advanced Encryption Standard)加密算法的互操作性。AES是一种广泛应用的块密码标准,用于保护数据的安全,确保信息不被未经授权...
ABAP Proxy是SAP提供的一个强大的接口技术,它允许开发者创建安全、高效的远程调用机制。接下来,我们将深入探讨这个主题,包括ABAP Proxy的原理、创建过程以及调用方法。 ### ABAP Proxy简介 ABAP Proxy是SAP ...
ABAP的另一个显著特性是其跨平台性,类似于Java,ABAP程序可以在任何操作系统上运行,兼容多种数据库,并能在不同的网络系统中无缝运行。ABAP字典实现了透明表的概念,使得开发者在ABAP层面上操作的表与底层数据库...
"abap xlsx2 demo 程序"是一个示例项目,旨在展示如何在SAP系统中读取和写入Excel文件。在这个程序中,开发者可能使用了特定的库或者自定义开发的函数来实现与Excel的交互。 首先,要理解ABAP如何处理.xlsx文件,...
本文档是一份针对基于Linux系统安装Oracle数据库SAP(ABAP+JAVA)的详细安装手册。SAP(Systems, Applications, and Products in Data Processing)是全球知名的ERP(企业资源规划)解决方案提供商,ABAP(Advanced ...
ABAP 4.7是SAP ABAP的一个重要里程碑,引入了许多新特性,如增强的对象导向编程支持、改进的调试工具、新的开发环境以及对Web服务的支持。这些更新使得开发更加高效,代码更加结构化。 3. ABAP编程基础: - 数据...
abap tips abap tips abap tips abap tips abap tips
ABAP ole下载到多个excel 上和一个excel上新增多个sheet 页 ;ABAP ole下载到多个exce PERFORM 注释了,和一个excel上新增多个sheet 页 ,注 是ABAP 代码 OLE
关于程序的生成,ABAP Query 还提供了一个独特的功能,即可以直接将查询转换为一个完整的 ABAP 报表程序。这样做可以让你进一步定制和优化生成的程序,包括调整性能、增加额外功能或修复潜在问题。这为那些需要更...
ABAP - Keyword Documentation This documentation describes the syntax and meaning of the keywords of the ABAP language and its object-oriented part ABAP Objects. Alongside this, language frameworks ...
例如,可能有一个名为`YTESTSERVICE.*.client.java`的类,其中包含ABAP发布的函数及其所需的参数。调用方式可能是`Port.ytestservice(参数);` 6. **调用方法**: 分析生成的类文件,确定正确的调用顺序和参数类型...
在ABAP程序中,可以将数据选择到内表中,这样做的好处是可以对数据进行更复杂的处理,比如将数据从一个表转移到另一个表。通过使用内表读取数据时,通常会用到loop语句。 查询条件的动态指定允许在程序运行时根据...
ABAP(Advanced Business Application Programming)是SAP公司开发的一种编程语言,主要用于开发SAP系统的企业级业务应用。作为ABAP的学习资料,"ABAP学习资料abap"包含了针对初学者和进阶者的全面教程,旨在帮助...