Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Aspect's. Aj class correctly

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to correctly use Aspect. Aj class", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to use Aspect correctly. Aj class"!

Catalogue

I. Preface

Second, my mind is full of coquettish operations.

1. Encounter problems

two。 Find a problem

3. Troubleshoot the problem

Third, how to correctly use the .aj class of Aspect

1. Install AspectJ

2. AspectJ plug-in

3. Add dependency aspectjrt.jar

4. Configure the AspectJ compiler

5. Case testing

I. Preface

It is also strange to say that as long as you are learning to program, from first contact with Java to installation of JDK, IDEA, MYSQL, and then to contact with Spring, MyBatis, RPC, MQ, even if you are in a shallow pit, you will jump in and have a taste, clutching your hair and clicking the mouse in one hand. There's always a lot of shredded hair on your keyboard!

Second, my mind is full of coquettish operations. Encounter problems

Last week, Xie Ji (pseudonym) sent his hand-masturbated middleware source code, saying that there is nothing in this code, why can't it be cut?

At first I was careless and asked Xie to send some code screenshots. Look at the code in the screenshot, it's exactly the same as the code in the middleware I wrote, there's no problem, the package path can also be scanned, why can't it be cut? I say you make a breakpoint to debug and see why you can't cut it. Hey, debugged, passed directly, but did not reach the section. At this point I thought about the JDK version, the environment configuration, the Spring context, the definition of the aspect, the path of the package, and whether this kid was fooling me? Finally, I held this kid in my heart and asked for the source code. two。 Find a problem

After looking at the source code for several times, I didn't find anything wrong, and I started debugging, and it really didn't enter this section. Next.

Thank you for keeping the source code of the aircraft and copying a new one. My aim is to get him running and studying. Then I copied and pasted the DoJoinPoint in my project and checked it. There was no hint to replace it. Although there was an error, the two classes could coexist, as follows:

This is amazing, ha, I wondered at that time that DoJoinPoint is not a serious Java class, the path is not correct? Are there any invisible special characters? Since it is found that this category is wrong, delete the line first. Let the program run first to make sure there is nothing wrong with anything other than this class, so that you can troubleshoot the problem. Not to mention, without this error class, the program can run normally and intercept the aspect content. Now that the program can run, I thought I could see what the problem was. Unexpectedly, I just opened a folder and found a magical AJ! This product is not Java at all! 3. Troubleshoot the problem

If IDEA hadn't displayed the .aj product as a class C icon, it would have found the problem. Then I sent a screenshot of this error class to Xie and asked it how did you create it? He told the truth.

Xie Ji first said that he was lazy, , it was embarrassing! He said that when creating a DoJoinPoint, he saw an option for Aspect and thought it was a shortcut to create a section, as shown in the figure. After the creation was completed, he found something wrong. It was not a class type, but an aspect, so he manually changed aspect to class, as shown in the figure. Therefore, what Xie actually created is an aspect class that ends with .aj, not a proper Java class, so there is no aspect, and there is no corresponding class file at all. Third, how to correctly use the .aj class of Aspect

AspectJ, AJ for short. I said it myself.

AspectJ is also an implementation technology of AOP, which is similar to interceptor and is integrated into IntelliJ IDEA development tools. You need to install the AspectJ toolkit natively before writing AspectJ code in IntelliJ IDEA. Otherwise, your .aj class will not run, and the .aj class displayed by the IDEA class is also the logo of C.

Next, let's talk about how to use this thing. Don't be fooled by .aj again.

1. Install AspectJ

Before using AspectJ, you need to go to the official website to download an installation package, address: https://www.eclipse.org/aspectj/downloads.php. If the official website downloads slowly, you can get it from the source code provided by me, or you can search and download aspectj-1.9.4.jar from other sources.

Download and complete installation

Double-click the installation command to install java-jar aspectj-1.9.4.jar configuration instructions: if there is no configuration, you can do it by default. After the installation is completed, a folder C:\ aspectj1.9 will be created on disk C, including: bin, doc, lib, etc. We will use these later. 2. AspectJ plug-in

To develop AspectJ in Professional IDEA, you need to install the following two plug-ins:

Spring AOP/@AspectJAspectJ Support3. Add dependency aspectjrt.jar

Before you start, you need to add aspectjrt.jar dependencies to your project. Aspectjrt.jar is the jar package under the lib directory in the AspectJ installation directory. You can copy it into the project, or you can introduce it directly.

Right mouse button on the project, click Open Module Setting to open Project Structure, click the Libraries tab, and the + sign above, create New Project Library, select C:\ aspectj1.9\ lib\ aspectjrt.jar path, and click to complete the configuration 4. Configure the AspectJ compiler

IDEA uses the javac compiler by default. Here, you need to configure the AspectJ compiler ajc and configure it in IDEA.

Open the IDEA-> File-> Settings dialog and select Build,Execution,Deployment-> Compiler-> Java CompilerUse complier: select Ajc to configure the path C:\ aspectj1.9\ lib\ aspectjtools.jar5 in Path to aspectjtools.jar. Case testing

Create an Aspect class

Public aspect DoAspect {

Pointcut logPointcut (): call (* ApiTest.hi (..))

Void around (): call (void ApiTest.hi (..)) {

System.out.println ("call start...")

Proceed ()

System.out.println ("call ends...")

}

Before (): logPointcut () {

System.out.println ("method executes before")

}

After (): logPointcut () {

System.out.println ("method executes after")

}

}

Test class

Public class ApiTest {

Public void hi () {

System.out.println ("Hi Aspect")

}

Public static void main (String [] args) {

ApiTest apiTest = new ApiTest ()

ApiTest.hi ()

}

}

Test result

Call, start.

Method to execute before

Hi Aspect

The end of call...

Method to execute after

Process finished with exit code 0

At this point, it is a correct way to open the Aspect class, and you can also try to do it about the use of Aspect. This article is just an introduction to this kind of faceted writing. Fourth, the more rugged, bold and flirtatious your code is, the more problems you will encounter, probably because you do not comply with certain R & D implementation norms, so these silly problems encountered will almost waste your morning or day. But sometimes if you can take your bug seriously, take an in-depth analysis of how it is produced, and reproduce it a little bit in-depth study, you may also get unexpected gains, maybe. So if you are serious, there is nothing bad about it. With regard to aspects, source code, and development, you probably shouldn't just focus on functional implementation, and sometimes even find ways to escape from work that doesn't grow day after day. Make more efforts on those valuable technologies, and you will gain the most.

At this point, I believe you have a deeper understanding of "how to correctly use Aspect. Aj class". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report