In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "non-Spring management Bean how to add AOP", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "non-Spring management Bean how to add AOP" this article.
The problem is shown in the following figure
Basically, this is a historical project, and there are a lot of classes that are not managed by spring, but now we need to add logs uniformly.
Faced with such a problem, in fact, as long as you understand the principle of AOP, there will be a variety of ways. AOP is implemented based on dynamic agents, and dynamic agents are common in cglib and java. If you don't know anything about it, you can take a look at the articles written by you.
Why do java dynamic agents need to be interface-based
Doesn't the cglib dynamic proxy have any restrictions on classes?
But these two methods seem to be difficult to implement in such a scenario and need to modify a lot of code, so is there any good solution?
Of course the answer is yes.
First of all, it should be clear that the underlying implementation principle of AOP is bytecode, and we only need to solve this problem from the bytecode level. As a result, compile-time enhancements and run-time enhancements can be used. There are two common scenarios, one for Java Agent technology and the other for AspectJ.
Java Agent
Java Agent Chinese name is java probe, you can specify the probe program when running java, there is no intrusion to the original program, some common APM tools will be like this, such as skywalking, later have the opportunity to introduce to you. The figure below is as follows
The main principle of java agent is to use JVMTI (JVM Tool Interface), JVM to expose a collection of interfaces for users to extend, so you can do some run-time bytecode enhancements here.
Java Agent has a lot of content, and many familiar tools are based on it, such as Ali's arthas. This article will not be introduced, later will give you a detailed introduction of Java Agent.
AspectJ scheme
You can use aspectj + javac to compile weaving code, you can also use the maven plug-in aspectj-maven-plugin, the following use of AspectJ annotations + aspectj-maven-plugin to actual combat.
Aspectj-maven-plugin official website http://www.mojohaus.org/aspectj-maven-plugin/usage.html
Introduce dependency
Compilation enhancements that depend on this jar
Import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; @ Aspect public class Aop {@ Pointcut ("execution (* com.ganhuojun.gracefulshutdown.controller..*.* (..)") Public void pointcut1 () {} @ Before ("pointcut1 ()") public void before () {System.out.println ("controller before");}}
Definition annotation
Note: this comment should not be handed over to spring management.
Org.codehaus.mojo aspectj-maven-plugin 1.11 1.8 1.8 UTF-8 src/main/java * * / ServiceAop.java compile
Configure the maven plug-in
Org.codehaus.mojo aspectj-maven-plugin 1.11 1.8 1.8 UTF-8 src/main/java * * / ServiceAop.java compile
Exclude aop for spring
If you are familiar with spring aop, spring's aop is also based on AspectJ, so it needs exclude and has been configured to mavn.
Compile & run & Test
The compiled class file has been woven into the relevant code, as shown in the following figure
The output of running related logs is as follows
Indicates that the function has been implemented.
The above is all the content of the article "how to add AOP to non-Spring Management Bean". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.