Persist in DataBase with AspectJ (AOP)






2.75/5 (4 votes)
Aug 30, 2004
6 min read

43022

583
One of the multiple forms to give persistence in data base to an object of a class with oriented programming to aspects (AOP) in AspectJ
Persist in Database with AspectJ (AOP)Sumary: Author: Fecha: Level: Enviromment: |
|
![]() ![]() ![]() ![]() ![]() Introduction
The power to in a while provide dice to an object of a class persistence, can make see the capacity us that has the
Aspects. Since we can give this behavior to the objects of a class, that does not have it, if to have to change its
codification. Aspect abstract
In the first place first that has been developed it has been an abstract aspect called Persistence that goes has
to allow to extend it with other aspects. This abstract aspect contains all the points of abstract cuts that define the behavior of the aspect in a while given. Figura 1. Persistencia.aj package persistencia_Aspect; import java.sql.*; /** * @author mlorente@programemos.com */ abstract aspect Persistencia extends CapaDatos { declare parents : FormularioCliente implements ObjetosRelacion; Persistencia() { super(); System.out.println("constructor Persistencia"); } abstract pointcut quieroLeer(); abstract pointcut quieroGrabar(); abstract pointcut quieroBorrar(); after() : quieroLeer() { leer(); } before() : quieroGrabar() { grabar(); } before() : quieroBorrar() { borrar(); } public void FormularioCliente.addObjetoRelacion(Object obj) { addObjeto((Cliente)obj); } }
The firts we can see is the following code: declare parents : FormularioCliente implements ObjetosRelacion. Here this indicating itself to him that the FormularioCliente class (In one more a developed implementation a form would be a class representing bases) goes has to implement the ObjetosRelacion interface. At the end of the declaration of the aspect the following code can be seen: public void FormularioCliente.addObjetoRelacion(Object obj) { addObjeto((Cliente)obj); }
Where the method has been implemented that contains the ObjetosRelacion interface. abstract pointcut quieroLeer(); abstract pointcut quieroGrabar(); abstract pointcut quieroBorrar(); after() : quieroLeer() { leer(); } before() : quieroGrabar() { grabar(); } before() : quieroBorrar() { borrar(); }
Three points of abstract cuts are declared, that will be due to sobrewrite in the aspects that the present one extends. Aspect ClienteOne of the idea at the time of carrying out this small project, has been to have to write the smaller code possible to give persistence to the objects of a certain class. And a code similar to the PersistenciaCliente aspect will be the one that we will have to write to give persistence to the objects that we wish. Figura 2. PersistenciaCliente.aj package persistencia_Aspect; import java.sql.*; /** * @author mlorente@programemos.com */ aspect PersistenciaCliente extends Persistencia { PersistenciaCliente() { System.out.println("constructor PersistenciaCliente"); } pointcut quieroLeer() : execution(* *.leer_click()); pointcut quieroGrabar() : execution(* *.grabar_click()); pointcut quieroBorrar() : execution(* *.borrar_click()); public String[] clave() { String[] a = new String[] {"IdCliente={$getId}"}; return a; } public String sqlIdentityField() { return "{$setId}"; } public String sqlSelect() { return "SELECT IdCliente{$setId}, Nombre{$setNombre}, Email{$setEmail} FROM Cliente"; } public String sqlUpdate() { return "UPDATE Cliente SET Nombre='{$getNombre}', Email='{$getEmail}'"; } public String sqlInsert() { return "INSERT INTO Cliente(Nombre, Email) VALUES('{$getNombre}', '{$getEmail}')"; } public String sqlDelete() { return "DELETE FROM Cliente"; } }
Once sight all the code that composes the PersistenciaCliente aspect we are going to enter to develop the detail of
the same one. pointcut quieroLeer() : execution(* *.leer_click()); pointcut quieroGrabar() : execution(* *.grabar_click()); pointcut quieroBorrar() : execution(* *.borrar_click());
we can suspect that it is the code that on writes the points of abstract cuts defined in the aspect Persistence.
And we suspected well because he is that what does. As it indicates the first point of cut we want that the cut
point is executed when the execution of the method is captured leer_click. TestFinally single it is to see the class that goes has to allow to us to make the opportune tests. It is necessary to mention the following code formulario.addObjetoRelacion(cliente) since object relates them to the FormularioCliente class. Account is necessary to occur that the FormularioCliente class does not contain the method at issue and that this provides through the abstract aspect Persistence to him. package persistencia_Aspect; /** * @author mlorente@programemos.com */ import java.lang.reflect.*; public class Test { public static void main(String[] args) { FormularioCliente formulario=new FormularioCliente(); Cliente cliente=new Cliente(); cliente.setId(1); Cliente cliente2=new Cliente(); cliente2.setId(2); formulario.addObjetoRelacion(cliente); formulario.addObjetoRelacion(cliente2); formulario.leer_click(); System.out.println("nombre del 1: " + cliente.getNombre() + " ruta: " + cliente.getEmail()); System.out.println("nombre del 2: " + cliente2.getNombre() + " ruta: " + cliente2.getEmail()); cliente.setNombre("nombre del 1 cambiado"); formulario.grabar_click(); cliente2.setNombre("nombre del 2 cambiado"); Cliente cliente3=new Cliente(); cliente3.setNombre("nuevo"); cliente3.setEmail("Nueva"); formulario.addObjetoRelacion(cliente3); formulario.borrar_click(); } } Conclusion
Evidently this is not the definitive solution to give persistence to an object of a class, but it approaches us one
of the multiple forms to do it. |