项目作者: deecarneiro

项目描述 :
AspectJ practices from AOP practical classes.
高级语言: AspectJ
项目地址: git://github.com/deecarneiro/AOP.git
创建时间: 2021-02-27T14:30:39Z
项目社区:https://github.com/deecarneiro/AOP

开源协议:

下载


AOP

A simple introduction to Aspect Oriented Programming with AspectJ.

What’s AOP?

AOP (aspect-oriented programming) is a programming style that can be adopted to define certain policies that in turn are used to define and manage the cross-cutting concerns in an application. In essence, it’s a programming paradigm that enables your application to be adaptable to changes.

Libs/Frameworks

  • AspectJ: an aspect-oriented programming (AOP) extension created at PARC for the Java programming language. It is available in Eclipse Foundation open-source projects, both stand-alone and integrated into Eclipse. AspectJ has become a widely used de facto standard for AOP by emphasizing simplicity and usability for end users. It uses Java-like syntax, and included IDE integrations for displaying crosscutting structure since its initial public release in 2001.

Code Snippet

Hello World

  • Class Exe01, print World on screen
  1. public class Exe01 {
  2. public static void printHelloWorld(){
  3. System.out.print(" World");
  4. }
  5. public static void main(String[] args){
  6. Exe01.printHelloWorld();
  7. }
  8. }
  • AspectJ class with pointcuts and advices that will be applied on function printHelloWorld()
  1. public aspect Exe01Aspect {
  2. pointcut nome(): execution (* Exe01.printHelloWorld());
  3. before() : nome() {
  4. System.out.print("Hello");
  5. }
  6. void around() : nome(){
  7. //proceed(); Advice is executed after pointcut
  8. System.out.print(" Nobody!"); //without proceed method, advice replaces pointcut
  9. // proceed(); Advice is executed before pointcut
  10. }
  11. after() : nome(){
  12. System.out.print(", DeeCarneiro!!");
  13. }
  14. }

Pointcuts

They allow a programmer to specify join points (well-defined moments in the execution of a program, like method call, object instantiation, or variable access). All pointcuts are expressions (quantifications) that determine whether a given join point matches. For example, this point-cut matches the execution of printHelloWorld() method in an object of type Exe01:

  1. pointcut nome(): execution (* Exe01.printHelloWorld());

Advices

They allow a programmer to specify code to run at a join point matched by a pointcut. The actions can be performed before, after, or around the specified join point. Here, the advice print on the screen a string after printHelloWorld() method execution:

  1. after() : nome(){
  2. System.out.print(", DeeCarneiro!!");
  3. }

More examples

  1. //Intercepts the execution of the method Solve with int type parameter
  2. public pointcut point01() : execution ( void PointcutExample.Solve(int));
  3. //Intercepts the calling of the constructor of the PointcutExample
  4. public pointcut point02() : call (PointcutExample.new());
  5. //Intercepts the calling of the constructor of the PointcutExample with parameters
  6. public pointcut point03() : call (PointcutExample.new(String, int));
  7. //Intercepts the getting of the diff attribute from PointExample class
  8. public pointcut point04() : get (* PointcutExample.diff);
  9. //Intercepts the setting of any attribute from PointExample class
  10. public pointcut point05() : set( * PointcutExample.*);
  11. //Intercepts the calling of any method with int type parameter from PointExample class
  12. public pointcut point06() : call ( * PointcutExample.*(int));
  13. //Intercepts the calling of any method that starts with S on any part of system
  14. public pointcut point07() : call (* *.S*(..));
  15. //Intercepts the execution of any method from PointcutExample method
  16. public pointcut point08() : execution (* PointcutExample.*(..));
  17. //Intercepts the handler of any method within code PointExample.
  18. public pointcut point09() : handler (Exception) && withincode(* PointcutExample.*(..) );

Runnig

AspectJ Project Eclipse

In Eclipse IDE install AspectJ:

— Help > Eclipse Marketplace and search for AspectJ

— Install AspectJ