Caij nplooj ntoos hlav AOP kawm tau nyob rau hauv 30 feeb

Learn Spring AOP

Caij nplooj ntoos hlav AOP kawm tau nyob rau hauv 30 feeb

Txheej txheem cej luam:

Npaj Kev Pab Cuam (AOP) yog lwm txoj kev mus kom ze rau qhov kev pab cuam xws li Object Oriented Programming (OOP). Txawm li cas los, AOP yuav txheeb xyuas tau OOP los ntawm kev muab lwm txoj kev xav txog qhov kev pab cuam uas muaj ib daim ntawv thov software yog tsim. 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 caij nplooj ntoos hlav 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, raws li qhia lub npe, 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, ruaj, 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. Piv txwv, 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. Ib, they are reusable across software applications and enterprises; Ob, since they contain procedures or functions, software developers just need to use them in their program structure; Peb, Aspects reduce development overhead.

Yuav tsum nyeem – Kawm khau raj caij nplooj ntoos hlav hauv 30 Feeb

Yuav tsum nyeem – Nrhiav kom paub cov kev pab Batch caij nplooj ntoos hlav



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 Hauj lwm lawm
Aspect Nam yog ib tug module uas comprises ib txheej APIs muab ib los yog ntau dua khaub lig-tej kev txhawj xeeb. Piv txwv, Yog hais tias qhov kev ruaj ntseg yuav tsum tau DVR nyob rau hauv ib daim ntawv thov software, Kev ruaj ntseg nam yuav muab kev txhawj xeeb txog kev ruaj ntseg nyob rau txheej txheem.
Koom taw tes Qhov no yog qhov chaw nyob rau hauv daim ntawv thov software uas ib tes hauj lwm los yog kauj ruam yuav tau siv lub caij nplooj ntoos hlav AOP framework.
tswv yim Qhov no yog qhov txiav txim kom tiav ua ntej los yog tom qab txoj kev tiav. Ua tau, Nws yog ib daim code invoked thaum qhov kev pab cuam tiav. Muaj tsib hom tswv yim: ua ntej, Tom qab, rov qab mus, ntuav thiab nyob ib ncig ntawm. Piv txwv, <!– ua ntej lub ntsiab txhais –>

<aop:ua ntej pointcut-ref=”cov tswv yim tshiabservice”

txujci =”doRequiredTask”/> txhais tau tias cov tswv yim ua ntej yuav khiav ua ntej qhov chaws yog tseg.

taw tes Muab qhov chaw nyob rau hauv qhov chaws uas ib lub tswv yim yuav tsum tau tseg.
Taw qhia 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” nyob =”aopBean”>

</aop:aspect>

</aop:config>

<taum daim id =”aopBean” hoob =”…”>

</taum>

Declare a pointcut

The example of pointcut is given below:

<aop:config>

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

<aop:pointcut id=aopbusinessService

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

</aop:aspect>

</aop:config>

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

</taum>

In the above declaration, Ib pointcut hu ua aopbusinessService yog txais thiab nws yuav phim tiav ntawm tau txais() txujci.



Txis tswv yim –

Cov piv txwv hauv qab no qhia li cas tag nrho tsib tswv yim yuav txais:

<aop:config>

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

<aop:pointcut id=aopbusinessService

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

<!– ua ntej lub ntsiab txhais –>

<aop:ua ntej pointcut-ref=”aopbusinessService

txujci =”doAOPTask”/>

<!– ib lub ntsiab txhais lus tom qab lub ntsiab txhais –>

<aop:tom qab pointcut-ref=”aopbusinessService

txujci =”doAOPTask”/>

<!– ib lub ntsiab lus tom qab rov qab mus txhais cov ntsiab lus –>

<!–Txoj kev doAOPTask yuav tsum muaj parameter hu ua aopVal –>

<aop:tom qab rov qab pointcut-ref=”aopbusinessService

rov qab=”aopVal”

txujci =”doAOPTask”/>

<!– ib lub ntsiab lus tom qab ntuav lub ntsiab txhais –>

<!–Txoj kev doAOPTask yuav tsum muaj parameter hu ua aopex –>

<aop:tom qab ntuav pointcut-ref=”aopbusinessService

ntuav=”ex”

txujci =”doAOPTask”/>

<!– ib ncig lub ntsiab txhais –>

<aop:Nyob ib ncig ntawm pointcut-ref=”aopbusinessService

txujci =”doAOPTask”/> …

</aop:aspect>

</aop:config>

<taum daim id =”yBean” hoob =”…”>…

</taum>

Raws li pom los ntawm cov piv txwv saum toj no, Tag nrho tsib tswv yim tau txais siv cov <aop:aspect> caij.

Thaum twg txoj no tseg, Nws yuav tso cov taw tes tej qhov nqaij ntuag thiab cov zis ntawm cov ntsiab lus. Cov zis yuav nyob ntawm tus taw tes txiav txhais thiab lub tswv yim pab.

Koj yuav tau xav nyeem ntawv – Dhia uas xeem coj – nws ua haujlwm li cas?

Caij nplooj ntoos hlav video tutorials – Saib thiab kawm








Xaus

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



Caij nplooj ntoos hlav Archive – Explore more interesting articles on Spring framework

============================================= ============================================== Yuav zoo TechAlpine phau ntawv rau Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Txaus siab rau qhov blog? Tshaj tawm lus thov :)

Follow by Email
LinkedIn
LinkedIn
Share