Apache Servicemix :: Bundles :: Spring AOP framework core function and characteristics
The core function and characteristics of the Spring AOP framework
Spring AOP (facing surface programming) is a core module of the Spring framework, which provides a way to cut (splitting or link) to the code when the program is runtime.Through Spring AOP, developers can abstract cross -sectional attention points (such as log records, performance monitoring, transaction management, etc.) that are not related to business goals, so as to separate them from business logic.The following is the core function and characteristics of the Spring AOP framework:
1. cut surface (aspect):
The cut surface is a modular code unit written by developers, which is used to define cross -cutting attention points and their behaviors.It includes a set of points and corresponding notifications.
2. PointCut:
The cut point is used to specify the method that needs to be intercepted during the program execution process.By using a cut point expression language, developers can flexibly define the mating rules of the cut point.
3. Notification (advice):
The notification defines the logic to be executed at the cut point.The Spring AOP framework provides commonly used notification types, including BeFore Advices, After Advices, After Returning Advice, after Throwing Advice, and Around Advice e).
4. Joinpoint:
The connection point is the execution point intercepted by the AOP framework.The connection point can be method call, abnormal throwing or field modification.
5. Introduction:
Introduce new methods and attributes to allow the existing class.By introducing, developers can combine the horizontal cutting function with the existing category.
6. Weaving (weaving):
The process of weaving is the process of applying the cut surface to the target object and creating a new agent object.Spring Aop provides two types of weaving methods: weaving and runtime when compiling.
Example of writing Spring AOP code:
1. Create the cut surface:
public class LoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
public void logBefore(JoinPoint joinPoint){
System.out.println("Before Method Execution: " + joinPoint.getSignature().getName());
}
}
2. Configure Spring Aop:
Declarize a cut surface and corresponding notification in the Spring configuration file.
<aop:config>
<aop:aspect ref="loggingAspect">
<aop:pointcut expression="execution(* com.example.service.*.*(..))" id="serviceMethods"/>
<aop:before method="logBefore" pointcut-ref="serviceMethods"/>
</aop:aspect>
</aop:config>
In the above example, the cut -type loggingaspect defines a pre -notification method logBeFore, and executes before the cut point expression Execution (*com.example.service.*.*(..)).
Summarize:
The Spring AOP framework provides a flexible way to achieve the separation of cross -sectional attention, so that developers can focus more on the realization of business logic.By cutting points, notifications, weaving and other core functions, Spring AOP can help us achieve more modular and maintained applications.At the same time, using Spring AOP can reduce code duplication and improve the reuse of code.