Learn Spring AOP in 30 minutes

Learn Spring AOP

Learn Spring AOP in 30 minutes

Overview:

Aspect Oriented Programming (AOP) is another approach to programming like Object Oriented Programming (OOP). However, AOP can complement OOP by providing another way to think about the program structure with which a software application is developed. There are certain similarities in how OOP and AOP are used to build software code although they are named differently – in OOP the unit of modularity is known as class while that in AOP is known as Aspect. AOP applications are developed based on the Spring framework though the Spring framework can also be used without the AOP. This article discusses how to work with Spring AOP applications.







What is AOP?

AOP, as the name suggests, is another approach to software programming. Let us understand AOP with the help of an example. A program structure for a software application contains three layers: UI layer, business logic layer and database layer. To function properly, all layers need support and communication. Support and communication can be provided by efficient logging, անվտանգություն, transaction management, profiling and more. In AOP parlance, różne jednostki zapewniające wsparcie są znane jako obawy,,en,Ponieważ obawy te zapewniają wsparcie między warstwami,,en,są one również nazywane zagadnieniami przekrojowymi,,en,Te przekrojowe problemy są zawarte w tak zwanych aspektach,,en,moduł logowania można nazwać aspektem AOP do logowania,,en,Aspekty są definiowane przez użytkownika i mogą być definiowane w czasie wykonywania,,en,Istnieje wiele zalet korzystania z Aspektów,,en,są wielokrotnego użytku w aplikacjach i przedsiębiorstwach,,en,ponieważ zawierają procedury lub funkcje,,en,programiści muszą po prostu wykorzystać je w swojej strukturze programu,,en,Aspekty zmniejszają koszty ogólne,,en,Koncepcje AOP,,en,Koncepcje AOP,,en,napędzaj strukturę AOP i korzystaj z AOP,,en,wymagane jest poznanie pojęć,,en,Główne pojęcia wyjaśniono poniżej,,en,Nazwa pojęcia,,en,Aspekt,,en. Since these concerns provide support across layers, they are also referred to as cross-cutting concerns. These cross-cutting concerns are encapsulated in what are known as Aspects. For example, a logging module can be called an AOP Aspect for logging. Aspects are user-defined and can be defined on run time basis. There are many advantages of using Aspects. One, they are reusable across software applications and enterprises; two, since they contain procedures or functions, software developers just need to use them in their program structure; three, Aspects reduce development overhead.

Must read – Learn Spring Boot in 30 Minutes

Must read – Steps to learn Spring Batch Service



AOP Concepts

AOP concepts drive the AOP structure and to use AOP, it is required to learn the concepts. The main concepts are explained below.

Concept name Նկարագրություն
Aspect Aspekt to moduł zawierający zestaw interfejsów API zapewniających jedno lub więcej zagadnień przekrojowych,,en,jeśli bezpieczeństwo musi zostać zaimplementowane w aplikacji,,en,Aspekt bezpieczeństwa może zapewnić bezpieczeństwo w różnych warstwach aplikacji,,en,To jest miejsce w aplikacji, w którym zostanie podjęta akcja lub krok przy użyciu Spring AOP,,en,Jest to akcja, która ma być wykonana przed lub po wykonaniu metody,,en,jest to fragment kodu wywoływany podczas wykonywania programu,,en,Istnieje pięć rodzajów porad,,en,po,,en,po powrocie,,en,po rzucie i dookoła,,en,definicja przed poradą,,en,newbusinessService,,en,doRequiredTask,,en,oznacza, że ​​rada,,en,zostanie uruchomiony przed wykonaniem kodu,,en,Umieść w kodzie, gdzie należy wykonać poradę,,en,Umożliwia dodawanie klas lub metod do istniejących klas,,en,Obiekt docelowy,,en. For example, if the security needs to be implemented in a software application, the security Aspect can provide security concerns across application layers.
Join Point This is the place in the software application where an action or step will be taken using the Spring AOP framework.
Advice This is the action to be executed before or after the method execution. Actually, it is the piece of code invoked during the program execution. There are five types of advices: before, after, after-returning, after-throwing and around. For example, <!– a before advice definition –>

<aop:before pointcut-ref=”newbusinessService”

method=”doRequiredTask”/> means that the advice before will be run before the code is executed.

Pointcut Place in the code where an advice should be executed.
Ներածություն Allows you to add classes or methods to existing classes.
Target Object Jest to obiekt, któremu doradza jeden lub więcej aspektów,,en,Jest to proces łączenia aspektów z obiektami aplikacji lub typami w celu utworzenia zalecanego obiektu,,en,Można to zrobić w czasie wykonywania,,en,czas ładowania lub w czasie kompilacji,,en,Budowanie aplikacji za pomocą AOP,,en,Teraz, gdy mamy trochę wiedzy o podstawach AOP,,en,zobaczymy poniżej, w jaki sposób można wykorzystać koncepcje do zbudowania kodu z jednym lub kilkoma przykładami,,en,Zadeklaruj aspekt,,en,Poniższy przykład pokazuje, jak można zadeklarować aspekt,,en,aopAspect,,en,aopBean,,gd,Zadeklaruj wycinek,,en,Przykład nacięcia podano poniżej,,en,aopbusinessService,,da,com.techalpine.Emp.getSal,,pt,bean id = ”yBean,,en,W powyższej deklaracji,,en,deklaracja o nazwie aopbusinessService jest zadeklarowana i pasuje do wykonania,,en,getSal,,en,Zadeklaruj radę,,en,Poniższy przykład pokazuje, jak można zadeklarować wszystkie pięć porad,,en.
Weaving It is the process of linking aspects with application objects or types to create an advised object. This can be done at run time, loading time or at the time of compilation.

Building an application with AOP








Now that we have some knowledge of the basics of AOP, we will see below how the concepts can be used to build a code with one or more examples:

Declare an aspect –

The example below shows how an aspect can be declared:

<aop:config>

<aop:aspect id=”aopAspect” ref=”aopBean”>

</aop:aspect>

</aop:config>

<bean id=”aopBean” class=”…”>

</bean>

Declare a pointcut –

The example of pointcut is given below:

<aop:config>

<aop:aspect id=”aopAspect” ref=”aopBean”>

<aop:pointcut id=”aopbusinessService”

expression=”execution(* com.techalpine.Emp.getSal(..))”/>

</aop:aspect>

</aop:config>

<bean id=”yBean” class=”…”>

</bean>

In the above declaration, a pointcut named aopbusinessService is declared and it will match the execution of getSal() մեթոդ.



Declare Advice –

The example below shows how all five advices can declared:

<aop:config>

<aop:aspect id=”aopAspect” ref=”aopBean”>

<aop:pointcut id=”aopbusinessService”

expression=”execution(* com.techalpine.aopapp.service.*.*(..))”/>

<!– a before advice definition –>

<aop:before pointcut-ref=”aopbusinessService”

method=”doAOPTask”/>

<!– an after advice definition –>

<aop:after pointcut-ref=”aopbusinessService”

method=”doAOPTask”/>

<!– an after-returning advice definition –>

<!–The doAOPTask method must have parameter named aopVal –>

<aop:after-returning pointcut-ref=”aopbusinessService”

returning=”aopVal”

method=”doAOPTask”/>

<!– an after-throwing advice definition –>

<!–The doAOPTask method must have parameter named aopex –>

<aop:after-throwing pointcut-ref=”aopbusinessService”

throwing=”ex”

method=”doAOPTask”/>

<!– an around advice definition –>

<aop:around pointcut-ref=”aopbusinessService”

method=”doAOPTask”/> …

</aop:aspect>

</aop:config>

<bean id=”yBean” class=”…”>…

</bean>

As can be seen from the above example, all five advices were declared using the <aop:aspect> տարր.

Երբ այս կոդը մահապատժի, it will display the point cuts and the output at those points. The output will depend on the point cut definitions and the advice types.

You will also like to read – Spring with Testing frameworks – How does it work?

Spring video tutorials – Watch and learn








Ամփոփում

While AOP seems to be a pretty good option because of the re-usability aspect of its concerns, it is not without its problems. One, it can be an issue debugging an AOP framework-based code because the business classes are advised only after the aspects are configured. Two, Advise can be given only for public methods and not for methods with default, private or public visibility. Still, AOP can be a good option because it complements OOP so well.



Spring Archive – Explore more interesting articles on Spring framework

============================================= ============================================== Գնեք լավագույն տեխնոլոգիական գրքերը Amazon- ում,en,Էլեկտրականություն CT շագանակագույն էլեկտրաէներգիա,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share