Aspect orientation vs object-oriented 3 (AOP method)

Source: Internet
Author: User

By Narayanan A.R. June 15,200 5
Translate zhangv (derekzhangv.at.hotmail.com)
Original article: http://www.devx.com/Java/Article/28422/0/page/3


Figure 3

Figure 3 describes the design of the AOP method and the interaction between classes at a more abstract level. You can better understand AOP by comparing figures 1 and 3.
The purpose of the program is to read the records in the CSV file through the BusinessUnit object and then fill in the map in the class BusinessUnitService. using AOP to fill in this map is a bit similar to the backdoor method-control is delegated to BusinessUnit to read records in the storage media.

AOP defines some point cut (?) And advice. A point cut is an execution point in the source code. the previous example defines a pointcut for the findBusinessUnits method in the BusinessUnitService class. an advice is a piece of code when the point cut is executed. businessUnitPersistentAspect includes the advice method findAllBusinessUnits. This method loads data from the storage medium and then creates a BusinessUnit object using the factory class. then the object is added to the map. The reference of the map object is obtained through the BusinessUnitService object. point cut and advice constitute the so-called "Aspect (Aspect )"

To read data in the storage media, the OOP method uses a DAO class. in AOP, you define a point cut and an advice in the scope class to read data. the AOP framework injects code in the form of advice, which can be executed or compiled.

All in all, when the findAllBusinessUnits method in BusinessUnitService is called, the AOP framework injects the advice method and uses the BusinessUnit object to pre-read data to fill the map object. in this way, Code on the persistence layer can be removed from the business model.

Aspects in the New Method

This section describes how to use AOP to model various aspects of an application.

Operate Resources

The durable method of the class BusinessUnitPersistenceAspect uses a buffered reader. You can even define the aspect, but for simplicity, here we will discuss only the lookup method of the category class.

  1. @ Aspect ("perJVM ")
  2. Public ClassBufferedFileReaderAspect {
  3. @ Expression ("execution (* org. javatechnocrats. aop. withaop. aspects. BusinessUnitPersistenceAspect. find *(..))")
  4. Pointcut businessUnitPersistenceAspect;
  5. // Other point cut definitions
  6. @ Expression ("businessUnitPersistenceAspect |
  7. EmployeePersistenceAspect |
  8. ManagerPersistenceAspect ")
  9. Pointcut allPersistencePointcuts;
  10. PrivateMap <Class,String> FileNames;
  11. PublicBufferedFileReaderAspect (){
  12. System. Out. println ("BufferedFileReaderAspect created ");
  13. FileNames =NewHashMap <Class,String> ();
  14. FillFileNames ();
  15. }
  16. @ Before ("allPersistencePointcuts ")
  17. Public VoidAssignReader (JoinPoint joinPoint)Throws Throwable{
  18. System. Out. println ("assignReader advice called ");
  19. ObjectCallee = joinPoint. getCallee ();
  20. IBufferedFileReaderConsumable bufReaderConsumable = (IBufferedFileReaderConsumable) callee;
  21. ClassPersistenceClass = callee. getClass ();
  22. StringFileName = fileNames. get (persistenceClass );
  23. FileReader fileReader =NewFileReader (fileName );
  24. BufferedReader bufferedReader =NewBufferedReader (fileReader );
  25. BufReaderConsumable. setBufferedReader (bufferedReader );
  26. }
  27. @ AfterFinally ("allPersistencePointcuts ")
  28. Public VoidReleaseReader (JoinPoint joinPoint)Throws Throwable{
  29. // Release buffered reader and other resources
  30. }
  31. // Other methods
  32. }


The code above tries to create a point cut for each method name-all methods starting with find. whenever these methods are called, The assignReader method will be executed in advance. here it gets the called class instance and then sets the newly created buffered reader.

Similarly, in the releaseReader method, the Code closes the buffered reader set in advance. This section only explains @ before and @
AfterFinally the two point cut. (defined by J2SE 5.0 ). in addition, you can declare them in the xml file defined by the aspect. you can view the aop in the source code of the routine. xml file.

Download

Persistence

As mentioned above, the OOP method uses BusinessUnit to fill the Map for the persistent layer of the application. in the highlighted code below (@ before and while LOOP code-Translator's note), when the findAllBusinessUnits method in BusinessUnitService is called, the advice method findAllBusinessUnits will also be called.
  1. @ Aspect ("perJVM ")
  2. Public ClassBusinessUnitPersistenceAspectImplementsIBufferedFileReaderConsumable {
  3. PrivateBufferedReader buffFileReader;
  4. @ Before ("execution (Collection org. javatechnocrats. aop. withaop. BusinessUnitService. findAllBusinessUnits ())")
  5. Public VoidFindAllBusinessUnits (JoinPoint joinPoint)Throws Throwable{
  6. System. Out. println ("findAllBusinessUnits advice called ");
  7. Map <String, BusinessUnit> busin

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.