Aspectj learning notes (2)

xiaoxiao2021-03-05  23

I have taught an example of aspectj, in fact, don't have to be so troublesome, now you can download the latest JAR package in his official website, you can install it directly, there are two places in the sea after installation, need to set up

1.Path plus $ {aspectj home} / bin

2.Classpath plus $ {aspectj home} / lib

This environment is OK, it is easy! Let's implement a classic Hello World! Example:

Class test {public void helloworld () {system.out.println ("Hello World");}

Public static void main (string args []) {test test = new test (); test.helloworld ();}}

This is a pure java file, there is nothing to say, look at Aspect

Public Aspect Testaspect {Pointcut OutputLog (): Call (public void "); before (): OutputLog () {system.out.println (" before call ");}}

Let's talk about the meaning of the above statement.

Call (Public Void HelloWorld ()); Public Void HelloWorld () in AOP is called Join Point is a method of host language, also called observation, writing any Aspect programs to find this observation in the host language. Point, simple to say is the function of the main program, that is, the entry point you want to monitor, called an indicator, a bit of a keyword, which means the execution of the monitoring method, and it is almost the same, it is called Execution, Differences are:

Call: As long as the method of calling its monitors, change PointCut works.

Execution: focus on the implementation of the method, and the entire method is completed to start matching

With join point, you can define Pointcut: PointCut OutputLog (): Call (public void helloworld (); Pointcut's full definition :: = ({parameters}): {Designator [&& ||]}; Subject is a bit like a registration.

When monitoring the execution of the public void helloworld () method, you should make a notification, there is a bit like Fire.

Before (): OutputLog () {System.out.println ("Before Call");}

The grammatical format is inserted:

Advice :: = [Return Type] TYPE OF Advice "(FORMAILS)" [THOWSTYPELIST]: "Pointcut" [Advice Body] "

Compile:

Ajc Test.java Testaspect .java

Java Test

operation result:

Before Callhello World

转载请注明原文地址:https://www.9cbs.com/read-37417.html

New Post(0)