In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you the "sample analysis of Spring AOP", which is easy to understand and well-organized. I hope it can help you solve your doubts. Let the editor lead you to study and learn the article "sample Analysis of Spring AOP".
Support for AOP in Spring
The AOP agent in Spring is generated and managed by the IoC container of Spring, and its dependency is also managed by the IoC container. Therefore, the AOP agent can directly use other Bean instances in the container as targets, and this relationship can be provided by the dependency injection of the IoC container. By default, Spring uses Java dynamic proxies to create AOP proxies, so you can create proxies for any interface instance. When the class that needs a proxy is not a proxy interface, Spring automatically switches to using a CGLIB proxy, or you can force the use of CGLIB.
The logic of this example is as follows: there is a Car class (business class) that has logging before and after the go method in the Car class runs, but the Car class itself does not know any logic of the log.
Create a Maven project and add dependencies
First, create a new Maven project, use the maven-archetype-quickstart template, then open the pom.xml file and add the dependency packages needed for Spring AOP to run
Org.springframework spring-core 4.0.5.RELEASE org.springframework spring-beans 4.0.5.RELEASE org.springframework spring-context 4.0.5.RELEASE org.springframework spring-aop 4.0.5.RELEASE org.aspectj aspectjweaver 1.8.1
Write business code
Add a new business class Car, including a go () method
Package com.wowo.spring_aop_demo1;public class Car {public void go () {System.out.println ("go go go!");}}
Write aspect classes
The log class records the operation of the system, but the logic of the log is not written everywhere in the business class, but exists as an aspect class.
Package com.wowo.spring_aop_demo1;public class CarLogger {public void beforeRun () {System.out.println ("car is going to run");} public void afterRun () {System.out.println ("car is running");}}
The aspect class contains two methods, which are pre-notification and post-notification.
Configure the association through bean
Add a new configuration file, named bean.xml in this example, to associate aspects with notifications in the configuration file
Note: the namespace of aop and several addresses contained in xsi:schemaLocation are required in this configuration file.
Execution (* com.wowo.spring_aop_demo1.Car.go (..)) Is an AspectJ pointcut expression, execution indicates that it is triggered on execution, followed by * indicates any type of return value, com.wowo.spring_aop_demo1.Car refers to the class where the pointcut is located, go (..) It's the name of the method. Represents any parameter.
There are five types of notifications that can be applied to Spring sections:
Before-- calls notification before the method is called
After-- calls the notification after the method completes, regardless of whether the method executes successfully or not
After-returning-- calls notification after the method executes successfully
After-throwing-- calls notification after the method throws an exception
The Around-- notification wraps the notified method and performs custom behavior before and after the notified method call
Run the business code
Create a class that contains the main () method to run the business code
Package com.wowo.spring_aop_demo1;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {public static void main (String [] args) {ApplicationContext context = new ClassPathXmlApplicationContext ("bean.xml"); Car car= (Car) context.getBean ("car"); car.go ();}}
In the above code, a car object is created by Spring. When Spring creates the object, it finds that one of its methods is configured as pointcut, so when instantiating the object, a proxy object is created. When the tangent method go () is executed, it is intercepted by the proxy object created by Spring. Before running the go method, the leading method beforeRun () of the corresponding aspect class CarLogger is called, and then the Car.go () method is called. Then call the post method afterRun () of the aspect class CarLogger.
Note: you must use Spring to create an object that contains pointcuts. If you create it yourself, Spring will not be monitored and no notification will be applied to its operation.
The output of the project is
Car is going to rungo go go!car is running
Use surround notification
If we want to use surround notifications, we need to modify the notification methods and configuration files in the aspect class, and the business classes do not need to make any changes because they are completely decoupled. First modify the aspect class CarLogger
Import org.aspectj.lang.ProceedingJoinPoint;public class CarLogger {public void aroundRun (ProceedingJoinPoint joinpoint) {System.out.println ("car is going to run"); try {/ / calls the target method of the proxied object, in this case pointing to the Car.go () method joinpoint.proceed ();} catch (Throwable e) {e.printStackTrace ();} System.out.println ("car is running");}}
The method that surrounds the notification needs to accept a parameter of type ProceedingJoinPoint, and its proceed () method will call the target method of the proxied object, so normally, this method must be called. We can also organize the operation of the proxied object by not calling the method.
Next, modify the aop:config section of the configuration file to look like this
Note: surround notifications cannot exist at the same time as pre / post notifications. After running the code, the output remains the same.
The above is all the content of this article "sample Analysis of Spring AOP". 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.