Kev siv siv lub caij nplooj hlav moj khaum AOP

Spring AOP

Caij nplooj ntoos hlav AOP – How to implement Aspect Oriented Programming?

Txheej txheem cej luam

The Aspect Oriented Programming (AOP) muaj ib txoj kev tshiab uas saib cas software programming yuav ua tau. Mas kom tus kwv Oriented Programming (OOP), the AOP does not represent a total or drastic departure to how programming is done. AOP just represents a new philosophy of programming and aims at improving efficiency between the different components of program logic. According to AOP principles, business objects within program logic should not ideally have support methods such as on transaction, security and logging because that not only creates clutter but also reduces efficiency because of excessive and redundant coding. The object methods are too reliant on support objects. AOP prescribes not only separating the support methods from all business objects but also ensure that redundant coding is eliminated. Li ntawd, business objects in the program logic do not need to call separate aspects because that requires separate coding. A configuration representing all aspects does the job of providing support to all the business objects in the program logic. The AOP comprises a number of components that in their own little ways help implement the basic principles of AOP.








Central concepts of AOP

There are a few issues or inefficiencies in how the concept of Object Oriented Programming (OOP) works in real life. Piv txwv, too much coding could be required even if some common support methods such as transaction, logging and security are taken out of the business objects and separate objects for such support tasks are created. Tseem, cross cutting creates too much dependency on the supporting objects. The central idea of AOP is to create separate aspects on transaction, security and logging and a configuration files for the support aspects. The configuration file is configured with scenarios of different methods within the objects calling the aspects. The main difference is that instead of configuring or writing codes for separate aspects and methods for calling the aspects, the programmer needs to just configure the aspect configuration file once. The main advantage with this approach is that the programmer effort at coding is reduced; dependencies on cross cutting objects or aspects are reduced and the main business objects in the program logic can do their core job and leverage the aspects whenever required. AOP comprises a certain number of components that makes managing objects and their relationships with the aspects easier to handle.



Goals of AOP

The main goals of AOP are:

Reducing coding effort

If all the methods in all the business objects of the program logic reference the transaction, security and logging aspects, configuration of such references entails huge coding efforts. The aspect configuration file contains a one-time configuration that allows all methods to reference the aspects. Even if the configuration file needs to be updated multiple times, that still reduces coding significantly.

Reducing clutter in objects

The methods written for transaction, security and logging in the business objects are removed and taken to the dedicated aspects. This reduces the clutter in the business objects within the program logic.

Improving program logic efficiency

When the support methods are written within the business objects, it is reflective of poor design principles and that can impact the performance of the software code. AOP seeks to remove the support methods and create aspects out of them separately so that the business objects can call the aspects whenever required. This allows the business objects to focus on their core tasks and the support aspects do their jobs.

Components of AOP

The main components of AOP are described below:

  • Koom taw tes: In Spring AOP, a join point represents a point during the execution of a program when execution of a method or handling of an exception is done.
  • tswv yim: Advice represents the action taken by an aspect at a join point. There are different types of advises such as ua ntej, after returning, after throwing, after and around. Piv txwv: Tus ua ntej advice acts before a join point happens but cannot prevent the execution.
  • taw tes: It is a predicate that matches all join points. An advice is related with a pointcut expression and executes at any join point that is matched by the pointcut. Piv txwv: the execution of a method with a particular name.
  • Weaving: Weaving is a component that links aspects with other types of applications or objects to create an advised object.









Teeb ib puag ncig:

Teem tau qhov chaw rau lub caij nplooj ntoos hlav raws li txoj kev loj hlob uas yog peb kauj ruam loj.

  • Teem java kev loj hlob Java txoj kev loj hlob

Download JDK Ntawm qhov chaw Oracle thiab ces, nruab nrab thiab configure. Ces cov PATH thiab JAVA_HOME chaw yuav tsum muab teev tseg.

  • Teev ntawm Eclipse tswv yim

Eclipse yuav tsum downloaded ntawm nom website. Ib zaug downloaded, tsis them cov binaries thiab ces muab lub PATH thiab.

  • Teem caij nplooj ntoos hlav lub caij nplooj ntoos hlav

Lub caij nplooj ntoos hlav lub caij nplooj ntoos hlav (Spring libraries). http://repo.spring.io/release/org/springframework/spring/. Dua, tseem ceeb heev uas yuav tau teem caij rau CLASSPATH kom yog.

  • Set up AOP specific libraries

Download aspectjrt.jar, aspectjweaver.jar and aspectj.jar and place them in the lib folder where other spring Jars are kept.

Tam sim no koj ib puag ncig yuav pib lub caij nplooj ntoos hlav Java.

Coj daim ntawv:

In this section we will work with a Spring AOP example where the aspects are set based on a XML file.

Qhia 1: This is the bean file

package com.dashboardapp.aopdemo;

public class Employee {

txheej kauj ruam;

pej xeem void setEmpname(hlua kauj ruam) {

qhov no..empname = empname;

}

Pej xeem string tauEmpname() {

Tsim.tawm.println(“Emp name is : ” + empname );

npaj siab rov qab los;

}

}

Following is the advice file. We will have only two advice which will be executed before and after a particular method execution.








Qhia 2: This is the file defining the advice

package com.dashboardapp.aopdemo;

public class LogAOP {

//Execute before a selected method execution

public void beforeAdvice(){

System.out.println(“Before AdviceStart employee name setup”);

}

//Execute after a selected method execution

public void afterAdvice(){

System.out.println(“After AdviceEnd employee name setup”);

}

}

Following is the most important file which will have declaration of beans, point-cuts, and aspects. This is the XML file for configuration of the entire aspect.

Qhia 3: XML file for configuration

<?xml version =”1.0″ encoding =”UTF-8″?>

<taum xmlns =”http://www.springframework.org/schema/beans”

xmlns:xsi =”http://www.w3.org/2001/XMLSchema-instance”

xmlns:aop=”http://www.springframework.org/schema/aop

xsi:schemaLocation =”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd “>

<aop:config>

<aop:aspect id=”cav” nyob =”aoplog”>

<aop:pointcut id=”xaiv”

expression=”tiav(* com.dashboardapp.aopdemo.Employee.getEmpname(..))”/>

<aop:ua ntej pointcut-ref=”xaiv” txujci =”beforeAdvice”/>

<aop:tom qab pointcut-ref=”xaiv” txujci =”afterAdvice”/>

</aop:aspect>

</aop:config>

<!– Definition for Employee bean –>

<taum daim id =”emp” hoob =”com.dashboardapp.aopdemo.Employee”>

<lub npe ntawm tus kheej =”empname” tus nqi =”Nicholas” />

</taum>

<!– Definition for logging aspect –>

<taum daim id =”aoplog” hoob =”com.dashboardapp.aopdemo.LogAOP”/>

</taum>

Qhia 4: Qhov no yog lub ntsiab Java ntaub ntawv

package com.dashboardapp.aopdemo;

ntshuam org.springframework.context.ApplicationContext;

ntshuam org.springframework.context.support.FileSystemXmlApplicationContext;

public class MainAOP {

pej xeem tsis muaj dabtsis loj zoo li qub(Txoj hlua[] args) {

ApplicationContext lub ntsiab lus teb = FileSystemXmlApplicationContext tshiab(

WebContent/WEB-INF/aopBean.xml”);

Employee emp = (Neeg ua hauj lwm) context.getBean(“emp”);

emp.getEmpname();

}

}

Tam sim no, if we run the main file the following output will be displayed.

AOP output

Duab 1: Uas qhia tso zis

Xaus

Raws li twb tau teev, AOP is not a drastically different programming philosophy from OOP concepts. It retains the structure of OOP largely and mainly focuses on the support objects. Li ntawd mas, the program logic can also be said to be a blend of AOP and OOP. Txawm li cas los, one critique about AOP is that it does not do much in improving design efficiency when an OOP program logic design becomes messy or cluttered because of too any interrelated objects.


 

============================================= ============================================== 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