Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to simulate and implement dependency injection of Java Spring in ABAP

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to simulate Java Spring dependency injection in ABAP, Xiaobian thinks it is quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

Try the scene

In real life, every lamp has a switch control. Press the switch and the light turns on; press it again and the light goes out.

Let's start with a regular implementation that doesn't use dependency injection:

Design an ABAP interface ZIF_SWITCH that provides two methods corresponding to ON and OFF.

Naturally, I have an ABAP class ZCL_LAMP that implements the above interface. Each instance of ZCL_LAMP is a lamp.

CLASS ZCL_LAMP IMPLEMENTATION.method ZIF_SWITCHABLE~OFF.WRITE: / 'lamp off'.endmethod.method ZIF_SWITCHABLE~ON.WRITE: / 'lamp on'.endmethod.ENDCLASS.

Then design a switch class that has a member variable mo_switchable pointing to the ZIF_SWITCH interface.

There's a "push" way to switch it. After pressing, if the current state is on, call the mo_switchable member's off method to turn it off and set the switch state to off. And vice versa.

METHOD push.IF isswitchon = abap_true.mo_switchable->off( ).isswitchon = abap_false.ELSE.mo_switchable->on( ).isswitchon = abap_true.ENDIF.ENDMETHOD.

Provide a setter method that injects the incoming variable of type ZIF_SWITCH into the member variable mo_switchable.

method SET_SWITCHABLE.mo_switchable = io_switchable.endmethod.

I put the two classes created so far, ZCL_LAMP and ZCL_SWITCH, into package $ZDEV_INVERSION.

ABAP Summer framework consumption code

As you can see from the code below, ZCL_SWITCH and ZCL_LAMP create strong dependencies. This dependency is manually injected by the application developer calling the set method.

To summarize, what is in the code above that Java Spring can completely avoid through the idea of dependency injection?

Line 8: Manually create an instance of ZCL_LAMP.

Line 9: Manually create an instance of ZCL_SWITCH.

Line 11: Call the set method to manually inject lamp and switch dependencies.

Dependency Injection Using ABAP Summer Framework

I myself modeled Java Spring's dependency injection framework with ABAP and developed a prototype called ABAP Summer, which echoes Java Spring.

Consider how this simple example can be implemented in Java Spring. A Java programmer can easily write the following code, using the Spring annotation @Inject, without having to manually instantiate ISwitch and call the set method to establish dependencies. The Spring Framework helps us achieve this.

Now, how do you do this magic with ABAP?

1. Adding the annotation @Inject to the description field of the mo_switchable member variable of the ZCL_SWITCH class is intended to tell the ABAP Summer framework that I want the mo_switchable member to automatically be injected with a correct dependency. What kind of dependency is right? How does the Summer Framework know how to inject? Please continue reading.

Note: ABAP is a different language than Java and cannot support annotations at the language level, so the @Inject maintained in the Description field here is just a simulation.

2. First look at the ABAP consumption code after using dependency injection, is it a lot of refreshing at once?

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( ).

The following figure is a comparison of two sets of consumption code based on ABAP conventional implementation and ABAP dependency injection idea. It can be clearly found that after ABAP dependency injection,

The three manual operations mentioned above are completely avoided. The switch instance returned by GET_BEAN method, in which the member variable mo_switchable contains an instance of the ZCL_LAMP class that is automatically injected.

Line 8: Manually create an instance of ZCL_LAMP.

Line 9: Manually create an instance of ZCL_SWITCH.

Line 11: Call the set method to manually inject lamp and switch dependencies.

Let's look at the actual consumption code in Java Spring again to make sure that our ABAP Summer is indeed the original dependency injection.

ABAP Summer Dependency Injection Implementation Principle

The implementation of this ABAP dependency injection framework is on my github:

https://github.com/i042416/jerryslide/tree/master/ABAP/summer

About "how to simulate Java Spring dependency injection in ABAP" This article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report