ABAP模擬Java Spring依賴注入(Dependency injection)的一個嘗試
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:
And a switch which internally maintains the current switch status and a reference to ZIF_SWITCHABLE:
The switch has a push method to toggle:
And a setter method is needed to inject the switchable instance:
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?
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的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2703483/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Dependency Injection-依賴注入詳解依賴注入
- .Net DI(Dependency Injection)依賴注入機制依賴注入
- Laravel Dependency Injection (依賴注入) 概念詳解Laravel依賴注入
- SAP Spartacus 中的依賴注入 Dependency Injection 介紹依賴注入
- .net core 原始碼分析(9) 依賴注入(DI)-Dependency Injection原始碼依賴注入
- Go語言:一文看懂什麼是DI依賴注入(dependency injection)設計模式Go依賴注入設計模式
- spring 的依賴注入Spring依賴注入
- Spring依賴注入Spring依賴注入
- Spring依賴注入---Spring依賴注入
- Spring 依賴注入的理解Spring依賴注入
- Spring IOC——依賴注入Spring依賴注入
- Spring 依賴注入 DISpring依賴注入
- Spring的依賴注入的方式Spring依賴注入
- Java Web系列:Spring依賴注入基礎JavaWebSpring依賴注入
- spring 依賴注入的學習Spring依賴注入
- Spring系列.依賴注入配置Spring依賴注入
- Spring.Net 依賴注入Spring依賴注入
- 大白話spring依賴注入Spring依賴注入
- Spring學習:簡單實現一個依賴注入和迴圈依賴的解決Spring依賴注入
- Spring原始碼系列:依賴注入(一)getBeanSpring原始碼依賴注入Bean
- Java Spring各種依賴注入註解的區別JavaSpring依賴注入
- 依賴注入的很好的一個部落格例子依賴注入
- spring框架學習 依賴注入Spring框架依賴注入
- Spring依賴注入原理學習Spring依賴注入
- Spring學習(三)依賴注入Spring依賴注入
- Spring的三種依賴注入的方式Spring依賴注入
- SpringIOC通過註解實現DI(dependence injection,依賴注入)Spring依賴注入
- spring常用的三種依賴注入方式Spring依賴注入
- 2.3 Spring的核心機制:依賴注入Spring依賴注入
- 依賴注入?依賴注入是如何實現解耦的?依賴注入解耦
- 死磕Spring原始碼-依賴注入Spring原始碼依賴注入
- Spring原始碼解析——依賴注入(二)Spring原始碼依賴注入
- Spring 控制反轉和依賴注入Spring依賴注入
- Spring原始碼系列:依賴注入-引言Spring原始碼依賴注入
- 一文徹底弄懂Spring IOC 依賴注入Spring依賴注入
- Spring系列第八講 依賴注入之手動注入Spring依賴注入
- Spring原始碼系列:依賴注入(三)-屬性注入Spring原始碼依賴注入
- ASP.NET Core中的依賴注入(2):依賴注入(DI)ASP.NET依賴注入