`
JerryWang_SAP
  • 浏览: 1042220 次
  • 性别: Icon_minigender_1
  • 来自: 成都
文章分类
社区版块
存档分类
最新评论

在SAP ABAP里使用注解@Inject模拟Java Spring

阅读更多

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:

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:

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:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

0
0
分享到:
评论

相关推荐

    sap abap调用java生成的webservice手册

    【SAP ABAP调用Java生成的Web Service手册】 在SAP系统中,与外部服务进行交互时,经常需要调用由Java等其他语言编写的Web Service。本手册详细介绍了如何在MyEclipse环境中生成Web Service,并在SAP ABAP环境中...

    sapabap开发从入门到精通

    资源名称:sap abap开发从入门到精通 内容简介: 《SAP ABAP开发从入门到精通》以应用实例的形式,讲解了SAPABAP开发的相关知识点,详细介绍了SAPABAP开发者必须掌握的包与变更传输系统(CTS)、数据类型、数据库...

    SAP ABAP与JAVA之间通过RFC传递数据实例

    本文档主要介绍了如何在SAP ABAP系统与JAVA应用程序之间通过RFC(Remote Function Call远程函数调用)实现数据交互的具体实现方法。核心需求是在给定特定的选择条件下,从SAP系统的数据库表T001中提取数据,并将这些...

    SAP ABAP 电子书

    SAP ABAP(Advanced Business Application Programming)是SAP公司推出的一种高级业务应用编程语言,用于开发在SAP R/3和SAP NetWeaver平台上的应用程序。这些电子书提供了全面的资源,帮助学习者深入理解并掌握SAP ...

    SAP ABAP 开发环境和开发工具介绍

    在 SAP 系统中,开发环境和开发工具是必不可少的组件,开发者可以使用这些工具来创建、测试和部署 ABAP 程序。ABAP WORKBENCH 是 SAP 系统中最常用的开发环境之一,它提供了一个集成的开发环境,包括编辑器、调试器...

    ABAP开发从入门到精通-高清自学版 SAP+ABAP开发从入门到精通 SAP开发自学必读 SAP SAP开发自学入门到精通

    SAP系统在全球企业资源规划(ERP)领域有着广泛的应用,而ABAP则是其核心开发语言,使得企业能够根据自身需求定制化SAP系统。 首先,"ABAP开发从入门到精通"这一主题涵盖了学习ABAP的基础到高级概念。对于初学者,...

    实战SAP程序开发——从实例学SAP ABAP编程.rar

    SAP是一种全球领先的业务软件系统,它用于企业管理各种复杂流程,包括财务、供应链、人力资源等。...实践是学习编程的最佳途径,这个资源提供了一个良好的起点,帮助你在SAP ABAP的世界里稳步前行。

    SAP ABAP 1.4.4代码下载

    在实际应用中,SAP ABAP的开发者可能会使用SE80交易码来访问和编辑代码,或者通过ABAP Development Tools(ADT),这是一个基于Eclipse的现代开发环境,提供了更直观的界面和更强大的代码编辑、调试功能。在1.4.4...

    SAP ABAP开发从入门到精通

    《SAP ABAP开发从入门到精通》以应用实例的形式,讲解了SAP ABAP开发的相关知识点,详细介绍了SAP ABAP开发者必须掌握的包与变更传输系统(CTS)、数据类型、数据库、模块化程序、内表、调试、ABAP数据字典、锁对象...

    SAP ABAP启动OUTLOOK发邮件.docx

    在SAP系统中,有时需要实现自定义的邮件发送功能以满足特定的业务需求,例如在用户操作SAP采购订单报表时,直接通过Outlook发送邮件到供应商。针对这种需求,我们可以利用SAP的ABAP编程环境,通过OLE(Object ...

    sap abap query高级功能

    SAP ABAP Query 是 SAP 系统中一种用于创建自定义报表的强大工具,尤其适合那些对 SQL 不太熟悉或者没有数据库直接访问权限的用户。它提供了丰富的功能,使得开发人员可以构建复杂的数据查询,而无需编写大量的 ABAP...

    SAP ABAP官方教程

    《SAP ABAP官方教程》是一份针对SAP ABAP编程语言的重要学习资源,适合对这个领域感兴趣的初学者和有经验的开发者。ABAP(Advanced Business Application Programming)是SAP系统中的主要编程语言,用于开发企业级...

    《SAP ABAP开发技术详解(实例篇)(第二版)mobi 版

    《SAP ABAP开发技术详解(实例篇)(第二版) [1] 》除了对对SAP ABAP开发所需的常用技术(SAP报表、批量导入、对话程序、函数)做了详尽的介绍,包括基本报表、交互式报表、复杂报表、ALV(SAP List Viewer)报表、...

    手把手教你配置Eclipse开发SAP ABAP程序环境.docx

    从零开始,教授如何配置 Eclipse 开发 SAP ABAP 程序环境。2020 年最新版本操作...ABAP Development Tool的Java端实现就是Eclipse的一个扩展,使用JCO(Java Connector)连接ABAP后台的adt服务,实现对ABAP程序的操作。

    SAP ABAP 代码备份下载到本地程序代码

    SAP ABAP 代码备份下载到本地程序代码

    《SAP ABAP开发详解与高端应用》.zip

    《SAP ABAP开发详解与高端应用》这本书深入探讨了SAP ABAP(Advanced Business Application Programming)编程语言,这是SAP系统中用于开发业务应用程序的关键技术。ABAP是SAP平台上的核心编程语言,用于创建自定义...

    SAP ABAP应用程序中"锁"的介绍

    在SAP ABAP应用程序中,锁机制是保证数据一致性、防止并发访问时产生数据不一致性的关键工具。本文将深入探讨SAP锁的概念、重要系统参数、锁管理以及锁的应用。 首先,SAP锁概念的核心在于防止多个进程同时修改相同...

    SAP ABAP培训练习题

    在IT领域,SAP ABAP(Advanced Business Application Programming)是一种专为SAP系统设计的编程语言,用于开发企业级的应用程序。本资料包是针对SAP ABAP初学者的培训练习题,旨在帮助学习者掌握基本概念和技能,...

    SAP ABAP开发学习——第9课:ALV2(视频教程)

    SAP ABAP开发学习——第9课:ALV2(视频教程) 1.SAP ABAP开发入门学习 2.含视频教程学习、PPT、作业 3.视频非常清晰

Global site tag (gtag.js) - Google Analytics