Detailed explanation of actual combat cases of AspectJ framework

Time:2020-11-29

This paper describes the AspectJ framework. For your reference, the details are as follows:

Environment variable configuration

Classpath is configured as:

.;d:\aspectj1.8\lib\aspectjrt.jar;D:\Program\Java\jdk1.8.0_162\lib\dt.jar;D:\Program\Java\jdk1.8.0_162\lib\tools.jar;

Path to be configured as:

d:\aspectj1.8\bin

Two codes

1 JavaBean

Hello.java

package org.crazyit.app.service;
public class Hello
{
  //Define a simple method to simulate the business logic method in application
  public void foo()
  {
    System.out.println ("execute the foo() method of the Hello component");
  }
  //Define an adduser() method to simulate the method of adding users in the application
  public int addUser(String name , String pass)
  {
    System.out.println ("addUser adding user to execute Hello component: + name)";
    return 20;
  }
}

World.java

package org.crazyit.app.service;
public class World
{
  //Define a simple method to simulate the business logic method in application
  public void bar()
  {
    System.out.println ("execute the bar () method of the world component");
  }
}

2 AOP

AuthAspect.java

package org.crazyit.app.aspect;
public aspect AuthAspect
{
  //Specifies that the org.crazyit.app Execute the following code block before any method of any class in. Service package
  //The first asterisk indicates that the return value is unlimited; the second asterisk indicates that the class name is unlimited;
  //The third asterisk indicates that there is no limit to the method name; the... In parentheses represents any parameter with unlimited number and type
  before(): execution(* org.crazyit.app.service.*.*(..))
  {
    System.out.println ("simulate permission check...");
  }
}

LogAspect.java

package org.crazyit.app.aspect;
public aspect LogAspect
{
  //Define a pointcut named logpointcut,
  //The pointcut represents the pointcut expression given later, which can be reused
  pointcut logPointcut()
    :execution(* org.crazyit.app.service.*.*(..));
  after():logPointcut()
  {
    System.out.println (simulation log...);
  }
}

TxAspect.java

package org.crazyit.app.aspect;
public aspect TxAspect
{
  //Designated execution Hello.sayHello The following code block is executed when () method
  Object around():call(* org.crazyit.app.service.*.*(..))
  {
    System.out.println ("simulate open transaction...");
    //Call back the original target method
    Object rvt = proceed();
    System.out.println ("simulate end transaction...");
    return rvt;
  }
}

3 test class

package lee;
import org.crazyit.app.service.Hello;
import org.crazyit.app.service.World;
public class AspectJTest
{
  public static void main(String[] args)
  {
    Hello hello = new Hello();
    hello.foo();
    hello.addUser ("Monkey King", "7788");
    World world = new World();
    world.bar();
  }
}

Three compilations

G:\test\AspectJQs>ajc -d . *.java

4. Compiled structure

G:\test\AspectJQs>tree /f
Folder list of VirtualPath’s volumes
The volume serial number is 8600-758f
G:.
│  AspectJTest.java
│  AuthAspect.java
│  Hello.java
│  LogAspect.java
│  TxAspect.java
│  World.java

├─lee
│      AspectJTest.class

└─org
    └─crazyit
        └─app
            ├─aspect
            │      AuthAspect.class
            │      LogAspect.class
            │      TxAspect.class
            │
            └─service
                    Hello.class
                    World.class

5. Operation

G:\test\AspectJQs>java lee.AspectJTest
Simulate open transaction
Simulate permission check
Execute the foo () method of the Hello component
Simulate logging
Impersonate end transaction
Simulate open transaction
Simulate permission check
Add user to add user: Monkey King
Simulate logging
Impersonate end transaction
Simulate open transaction
Simulate permission check
Execute the bar () method of the world component
Simulate logging
Impersonate end transaction

I hope that this paper will be helpful to the Java program design based on AspectJ framework.

Recommended Today

Api: tiktok: user video list

Tiktok tiktok, tiktok, tiktok, Api, Api, jitter, voice, bullet screen comments, jitter, jitter, and jitter. Tiktok tiktok tiktok data, jitter data acquisition, live broadcast of shaking sound Titodata: professional short video data acquisition and processing platform. For more information, please contact:TiToData Massive data collection Collect 500 million pieces of data for customers every day Tiktok […]