In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to build an AOP testing environment". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Write at the front
The golden period of job-hopping has begun. I believe many friends are also rubbing their hands and want to change a new working environment. However, due to the impact of this year's epidemic, many enterprises have increasingly stringent requirements for recruitment. Before, many knowledge points that were not asked will be asked in the recent interview. This is not, some interviewers even asked the interviewer to build an AOP test environment on the spot. What are we going to do? Then build one for him!
What is AOP?
AOP (Aspect Orient Programming), literally translating means aspect-oriented programming. AOP is a programming idea and a supplement to object-oriented programming (OOP). Object-oriented programming abstracts the program into all levels of objects, while aspect-oriented programming abstracts the program into various aspects.
For example, in Spring practice (4th Edition), there is a picture describing the general model of AOP.
From this picture, we can see that the so-called section is equivalent to the crosscutting point between application objects, which can be abstracted as a separate module.
In a word: AOP refers to the programming mode that dynamically cuts a piece of code into a specified method and a specified location during the running of the program. The underlying AOP is implemented using dynamic proxies.
Build an environment
1. Import AOP dependencies
To build an AOP environment, we first need to introduce AOP dependencies into the project's pom.xml file, as shown below.
5.2.6.RELEASE org.springframework spring-aspects ${spring.version}
two。 Define the target class
Create a MathHandler class under the io.mykit.spring.plugins.register.aop package to handle some logic on mathematical calculations. For example, we define an addition operation in the MathHandler class that returns the sum of two integer type values, as shown below.
Package io.mykit.spring.plugins.register.aop; / * * @ author binghe * @ version 1.0.0 * @ description defines a data handler class to test AOP * / public class MathHandler {public int add (int I, int j) {System.out.println ("target method execution"); return I + j;}}
3. Define a facet class
Create a LogAspect aspect class under the io.mykit.spring.plugins.register.aspect package, and define several methods to print logs in the LogAspect class to sense the operation of the add () method in the MathHandler class. If you need the aspect class to sense the operation of the target class method, you need to use the notification method in Spring AOP.
The notification method in AOP and its comments and meanings are as follows:
Pre-notification (@ Before): runs before the target method runs.
Post notification (@ After): runs after the target method has finished running, whether it ends normally or abnormally.
Return notification (@ AfterReturning): runs after the target method returns normally.
Exception notification (@ AfterThrowing): runs after the target method throws an exception.
Surround notification (@ Around): dynamic agent that manually pushes the target method to run.
To sum up, the specific method definitions in the LogAspect class are as follows.
Package io.mykit.spring.plugins.register.aspect; import org.aspectj.lang.annotation.*; / * * @ author binghe * @ version 1.0.0 * @ description print log aspect class * / @ Aspect public class LogAspect {@ Pointcut ("execution (public int io.mykit.spring.plugins.register.aop.MathHandler.* (..)") Public void pointCut () {} @ Before ("pointCut ()") public void logStart () {System.out.println ("addition start, parameter list is: {}");} @ After ("pointCut ()") public void logEnd () {System.out.println ("addition run ends") } @ AfterReturning ("pointCut ()") public void logReturn () {System.out.println ("addition returned normally, run result: {}");} @ AfterThrowing ("pointCut ()") public void logException () {System.out.println ("addition exception, exception message: {}");}}
The logStart () method: runs before the add () method of the MathHandler class runs.
The logEnd () method: runs after the add () method of the MathHandler class finishes running.
The logReturn () method: runs after the add () method of the MathHandler class returns normally.
The logException () method: the add () method of the MathHandler class throws an exception and executes.
4. Add target classes and aspect classes to the IOC container
In the io.mykit.spring.plugins.register.config package, create a new AopConfig class and use the @ Configuration annotation to mark that this is a Spring configuration class, and use the @ EnableAspectJAutoProxy annotation to turn on the annotation-based AOP mode. In the AopConfig class, add the MathHandler class and the LogAspect class to the IOC container using the @ Bean annotation, as shown below.
Package io.mykit.spring.plugins.register.config; import io.mykit.spring.plugins.register.aop.MathHandler; import io.mykit.spring.plugins.register.aspect.LogAspect; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy / * @ author binghe * @ version 1.0.0 * @ description Test AOP * / @ Configuration @ EnableAspectJAutoProxy public class AopConfig {@ Bean public MathHandler mathHandler () {return new MathHandler ();} @ Bean public LogAspect logAspect () {return new LogAspect ();}}
5. Create a test class
Create the AopTest test class in the io.mykit.spring.test package and the testAop01 () method in the AopTest class, as shown below.
Package io.mykit.spring.test; import io.mykit.spring.plugins.register.aop.MathHandler; import io.mykit.spring.plugins.register.config.AopConfig; import org.junit.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; / * * @ author binghe * @ version 1.0.0 * @ description Test Section * / public class AopTest {@ Test public void testAop01 () {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext (AopConfig.class) MathHandler mathHandler = context.getBean (MathHandler.class); mathHandler.add (1,2); context.close ();}}
Run the testAop01 () method in the AopTest class, and the output information is as follows.
The addition operation starts. The parameter list is as follows: {} the target method executes the addition operation ends and the addition returns normally, and the running result: {}
As you can see, the methods in the aspect class are executed and the relevant information is printed out. However, the parameter list and run results are not printed.
6. Print a list of parameters and return results in the aspect class
What if you need to print out a list of parameters and run the results? Don't worry, let's move on.
To print out the parameter list and run results, you need to optimize the methods in the LogAspect class, and the optimized results are shown below.
Package io.mykit.spring.plugins.register.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; import java.util.Arrays; / * * @ author binghe * @ version 1.0.0 * @ description print log aspect class * / @ Aspect public class LogAspect {@ Pointcut ("execution (public int io.mykit.spring.plugins.register.aop.MathHandler.* (..)") Public void pointCut () {} @ Before ("pointCut ()") public void logStart (JoinPoint joinPoint) {System.out.println (joinPoint.getSignature (). GetName () + "start running, parameter list is: {" + Arrays.asList (joinPoint.getArgs ()) + "}") } @ After ("pointCut ()") public void logEnd (JoinPoint joinPoint) {System.out.println (joinPoint.getSignature (). GetName () + "run end");} @ AfterReturning (value = "pointCut ()", returning = "result") public void logReturn (JoinPoint joinPoint, Object result) {System.out.println (joinPoint.getSignature (). GetName () + "normal return, run result: {" + result+ "}") } @ AfterThrowing (value = "pointCut ()", throwing = "exception") public void logException (JoinPoint joinPoint, Exception exception) {System.out.println (joinPoint.getSignature (). GetName () + "exception, exception message: {" + exception+ "}");}}
Here, it is important to note that the JoinPoint parameter must be placed first.
At this point, we run the testAop01 () method in the AopTest class again, and the output information is as follows.
The add starts to run. The parameter list is as follows: {[1,2]} Target method executes add run ends add returns normally, run result: {3}
7. The target method throws an exception
We test the exception condition by throwing an exception in the add () method of the MathHandler class, as shown below.
Package io.mykit.spring.plugins.register.aop; / * * @ author binghe * @ version 1.0.0 * @ description defines a data handler class to test AOP * / public class MathHandler {public int add (int I, int j) {System.out.println ("Target method execution"); throw new RuntimeException (); / / return I + j;}}
At this point, we run the testAop01 () method in the AopTest class again, and the output information is as follows.
Add starts to run. The parameter list is: {[1,2]} Target method executes add run end add exception. Exception message: {java.lang.RuntimeException}
As you can see, the information printed in the section is output correctly.
This is the end of "how to build an AOP Test Environment". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.