Apache Servicemix :: Bundles :: Spring Aop framework core concept introduction
Apache Servicemix :: Bundles :: Spring Aop framework core concept introduction
Spring Aop is a powerful framework that is widely used in Apache ServiceMix.This article will introduce the core concept of Spring Aop and provide some Java code examples.
1. What is Spring AOP?
Spring AOP is a key component of the Spring framework. It provides a programming mechanism based on cutting surface, allowing developers to separate the horizontal section focus from business logic by statement.Through AOP, without modifying the original code, you can apply the universal horizontal logic to multiple classes or methods.
2. Core concept
In Spring AOP, there are several core concepts to understand:
-ASPECT: The cutting surface is a modular unit that separates cross -cut logic (such as logging, transaction management, etc.) from business logic.It consists of cutting points and notifications.
-Capum: What connection points (method calls, field access, etc.) should be used to use the logic of the cut surface.Spring AOP uses ASPECTJ cut point expression language to define the cut point.
-TVICE: The notification is the code executed at a specific connection point, which defines the logic of the cut surface of the cut surface when it is executed when the method executes, after the method execution, etc.).Notification can be pre -notification, rear notification, surround notification, abnormal notification and final notice.
-Joinpoint: The connection point indicates that the point that can be inserted into the cut surface in the application, such as method calling or abnormally throwing.
-Troduction: Introduce the introduction of new interfaces and implementation to allow existing classes.By introducing, you can add new functions to the class without modifying the existing code.
-Wats (weaving): Weaving is the process of applying the cut surface to the target object and creating a new proxy object.It is possible to knitting in when compiling, when loading or running.
3. Java code example
Below is a simple Java code example, demonstrating how to use Spring AOP:
// Define a cut -off class
@Aspect
@Component
public class LoggingAspect {
// Define a cut point
@Pointcut("execution(* com.example.service.*.*(..))")
private void serviceMethods() {}
// Pre -notification
@Before("serviceMethods()")
public void beforeAdvice(JoinPoint joinPoint) {
System.out.println("Method Invoked: " + joinPoint.getSignature().getName());
}
// Rear notification
@AfterReturning(pointcut = "serviceMethods()", returning = "result")
public void afterReturningAdvice(JoinPoint joinPoint, Object result) {
System.out.println("Method Returned: " + joinPoint.getSignature().getName());
System.out.println("Result: " + result.toString());
}
// Abnormal notification
@AfterThrowing(pointcut = "serviceMethods()", throwing = "ex")
public void afterThrowingAdvice(JoinPoint joinPoint, Exception ex) {
System.out.println("Method Threw Exception: " + joinPoint.getSignature().getName());
System.out.println("Exception: " + ex.getMessage());
}
}
// Define a service class
@Component
public class MyService {
public String hello(String name) {
System.out.println("Hello " + name);
return "Welcome, " + name;
}
public void error() {
throw new RuntimeException("Something went wrong!");
}
}
// The main application class
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(Application.class, args);
MyService service = context.getBean(MyService.class);
service.hello("Alice");
service.error();
}
}
In the above example, Loggingaspect is a cut -off class that defines three notifications (front notice, rear notification and abnormal notification), and defines a cut point through @PointCut annotations.In the Application class, we obtained the MyService instance through the ApplicationContext and called two methods to trigger the logic of the cut surface.
Through the Spring AOP, we can easily separate the horizontal sectaries such as log records from business logic to improve the maintenance and reuse of code.
It is hoped that this article will help understand the core concept of Spring Aop in Apache Servicemix.