Learn Spring AOP in 30 minutes

Learn Spring AOP

Learn Spring AOP in 30 minutes

Übersicht:

Aspect Oriented Programming (AOP) is another approach to programming like Object Oriented Programming (OOP). Jedoch, 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, Geschäftslogikschicht und Datenbankschicht,,en,Um richtig zu funktionieren,,en,Alle Schichten brauchen Unterstützung und Kommunikation,,en,Support und Kommunikation können durch effiziente Protokollierung gewährleistet werden,,en,Transaktionsmanagement,,en,Profiling und mehr,,en,Im AOP-Sprachgebrauch,,en,Die verschiedenen Einheiten, die Unterstützung bieten, sind als Bedenken bekannt,,en,Da diese Bedenken Unterstützung über mehrere Ebenen bieten,,en,Sie werden auch als Querschnittsbelange bezeichnet,,en,Diese Querschnittsthemen sind in sogenannten Aspekten zusammengefasst,,en,Ein Protokollierungsmodul kann zur Protokollierung als AOP-Aspekt bezeichnet werden,,en,Aspekte sind benutzerdefiniert und können auf Laufzeitbasis definiert werden,,en,Die Verwendung von Aspekten bietet viele Vorteile,,en,Sie sind für Softwareanwendungen und Unternehmen wiederverwendbar,,en,da sie Prozeduren oder Funktionen enthalten,,en,Software-Entwickler müssen sie nur in ihrer Programmstruktur verwenden,,en. To function properly, all layers need support and communication. Support and communication can be provided by efficient logging, Sicherheit, transaction management, profiling and more. In AOP parlance, the various units that provide support are known as concerns. 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 Beschreibung
Aspect Aspect is a module that comprises a set of APIs providing one or more cross-cutting concerns. 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.
Einführung Allows you to add classes or methods to existing classes.
Target Object This is the object that is advised by one or more aspects.
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,,en,In der obigen Erklärung,,en,Ein Punkt mit dem Namen aopbusinessService wird deklariert und entspricht der Ausführung von,,en,getSal,,en,Ratschläge erklären,,en,Das folgende Beispiel zeigt, wie alle fünf Empfehlungen erklärt werden können,,en,com.techalpine.aopapp.service. *. *,,pt,DOAOPTask,,fy,eine Definition nach dem Ratschlag,,en,eine Definition nach der Rückkehr von Hinweisen,,en,Die doAOPTask-Methode muss den Parameter aopVal haben,,en,after-returning pointcut-ref =,,en,Rückkehr =,,en,aopVal,,ga,eine Definition nach dem Werfen,,en,Die doAOPTask-Methode muss den Parameter aopex haben,,en,Nach dem Werfen pointcut-ref =,,en,werfen =,,en,eine um Ratschläge Definition,,en,um pointcut-ref =,,en,yBean,,en,Wie aus dem obigen Beispiel ersichtlich,,en,Alle fünf Empfehlungen wurden mit der,,en,Es werden die Punktschnitte und die Ausgabe an diesen Punkten angezeigt,,en,Die Ausgabe hängt von den Schnittdefinitionen und den Hinweisarten ab,,en,Sieh und lern,,en” class=”…”>

</bean>

In the above declaration, a pointcut named aopbusinessService is declared and it will match the execution of getSal() Verfahren.



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> Element.

Wenn dieser Code ausgeführt, 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








Fazit

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

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share