Grasp the core knowledge of cutting surface -oriented programming in the java class library
Grasp the core knowledge of cutting surface -oriented programming in the Java class library in Spring Framework
Introduction:
In modern software development, ASPECT-Oriented Programming (AOP) is widely used to solve the problem of focusing on cross-business logic.As a powerful Java class library as a powerful Java class library, Spring Framework provides strong AOP support, so that developers can easily realize the concept and mechanism of AOP.This article will introduce the core knowledge and usage of Spring Framework Aop, and explain it in detail through the Java code example.
1. What is AOP?
AOP is a programming paradigm, which aims to separate attention points (that is, horizontal section focusing points) from the main business logic to provide better modularity and reusedability.In traditional object -oriented programming, attention points are usually scattered in the entire application and mixed with core business logic.AOP introduces the concept of horizontal cutting attention points. These focus usually repeats in different categories and methods, and does not have the inherent nature of business logic, such as log records, transaction management and security.By using AOP, we can separate these repeated attention points from the main business logic to enhance the maintenance and reassembly of the code.
2. The basic concept of Spring Framework Aop
The core concepts of Spring Framework Aop include ASPECT, Join Point, ADVICE, PointCut, and Weaving.
-ASPECT: The cutting surface is a modular unit, which separates the cross -sectional attention point from the core business logic.
-Join Point: The connection point is the point that spans the surface of the surface during the application execution.In Java, the connection point is usually method calls or abnormal processing.
-ADVICE: The notification is the code executed at the connection point.The notification can be executed before, after, or before, and after the connection point.
-Caput: The entry point is an expression that defines which connection points should be intercepted by the notification.
-Wats: Weaving is the process of incorporating the process of cutting surface with the target object or target class.In Spring, weaving can occur during compilation, loading or running.
3. Use Spring Framework Aop
Below is a simple example, demonstrating how to use AOP in Spring Framework.
First, AOP needs to be enabled in the configuration file of Spring.It can be achieved by adding a subordinate:
<beans xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<aop:aspectj-autoproxy/>
</beans>
Then, create a cut type, as shown below:
public class LoggingAspect {
public void beforeAdvice() {
System.out.println ("Notice before the method executes");
}
public void afterAdvice() {
System.out.println ("Execute notification after the method executes");
}
public void afterReturningAdvice(Object returnValue) {
System.out.println ("executes notification after the method returns, return value:" + ReturnValue);
}
public void afterThrowingAdvice(Exception ex) {
System.out.println ("After the method throws an abnormality, execute the notification, abnormal information:" + ex.getMessage ());
}
public void aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println ("Notice before the method executes");
Object returnValue = joinPoint.proceed();
System.out.println ("execute notification after the method executes, return value:" + ReturnValue);
}
}
Finally, apply the cut surface to a certain target class or target method, as shown below:
public class UserService {
public void addUser(String username) {
System.out.println ("Add user:" + username);
}
public void updateUser(String username) {
System.out.println ("Update user:" + username);
}
public void deleteUser(String username) {
System.out.println ("Delete user:" + username);
}
}
<beans>
<bean id="userService" class="com.example.UserService"/>
<bean id="loggingAspect" class="com.example.LoggingAspect"/>
<aop:config>
<aop:aspect ref="loggingAspect">
<aop:pointcut id="allMethods" expression="execution(* com.example.UserService.*(..))"/>
<aop:before method="beforeAdvice" pointcut-ref="allMethods"/>
<aop:after method="afterAdvice" pointcut-ref="allMethods"/>
<aop:after-returning method="afterReturningAdvice" pointcut-ref="allMethods" returning="returnValue"/>
<aop:after-throwing method="afterThrowingAdvice" pointcut-ref="allMethods" throwing="ex"/>
<aop:around method="aroundAdvice" pointcut-ref="allMethods"/>
</aop:aspect>
</aop:config>
</beans>
Through the above configuration, we define a point cutting, which intercepts all the methods in the `UserService` class, and associate it with the notification method in the cutting class.In each notification method, corresponding cross -cutting operations can be performed, such as log records and transaction management.
Conclusion:
Spring Framework Aop provides a strong cut -off programming support for Java developers.By mastering the core knowledge and usage method of Spring Framework AOP, developers can better achieve the separation of attention points and improve the maintenance of code and reused.I hope this article will help you understand Spring Framework Aop.