Application research of the technical principle of the AOP Utilities framework in the Java class library
Application research of the technical principle of the AOP Utilities framework in the Java class library
Summary: AOP (facing surface programming) is a technology commonly used in software development. It can be separated from the main business logic from the main business logic to improve the maintenance of code and available code from the main business logic.Reuse.The AOP Utilities framework is a AOP implementation based on Java language. This article will focus on discussing the technical principles of the AOP Utilities framework and application research in the Java class library.
1 Introduction
In software development, Cross-Cutting Concerns refers to those functions that do not belong to the main business logic, but are frequently used in software, such as log records, performance monitoring, security verification, etc.Traditional object -oriented programming scattered these functions in business logic code, resulting in high code repetitive and poor maintenance.The emergence of AOP (facing surface programming) technique solves this problem. It is stripped from the main business logic from the main business logic from the main business logic, which improves the maintenance of the code and replication.
2. Technical principle of AOP Utilities framework
The AOP Utilities framework is an open source AOP framework that is implemented based on the Java language.Its core principle is through dynamic proxy technology, which weaves horizontal sectaries into the method of target objects at runtime.The specific implementation steps are as follows:
2.1 Creation Notice (Advice)
The notice is the code fragment that is executed in the horizontal section.The AOP Utilities framework supports common notification types, including BeFore Advices, After Advice, Around Advice, etc.Developers can create appropriate types of notifications as needed and implement corresponding code logic.
The following is an example code of the front notice:
public class BeforeAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("Before method execution");
// Call the target method
Object result = invocation.proceed();
return result;
}
}
2.2 Create cutting points (PointCut)
What methods are specified?In the AOP Utilities framework, the cut point is defined using the expression language of ASPECTJ.Developers can use the syntax definition points provided by ASPECTJ, such as matching according to class names and method names.
Here are a sample code of cutting point:
public class SamplePointcut extends StaticMethodMatcherPointcut {
@Override
public boolean matches(Method method, Class<?> targetClass) {
// Matching the method of satisfying conditions
return method.getName().startsWith("do");
}
}
2.3 Create a weavers (weaver)
The weavers are the core components in the AOP Utilities framework, which is responsible for weaving into the method of weaving into the cut point matching.The weavers use ProxyFactory provided by Spring Aop to create dynamic proxy objects, and use cut points and notifications to specify the weaving rules.
The following is a sample code for a weavers:
public class SampleWeaver {
public void weave() {
// Create the target object
SampleService target = new SampleServiceImpl();
// Create notice
Advice advice = new BeforeAdvice();
// Create cut points
Pointcut pointcut = new SamplePointcut();
// Create a weaving inner
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setTarget(target);
proxyFactory.addAdvice(advice);
proxyFactory.addPointcut(pointcut);
// Get the proxy object
SampleService proxy = (SampleService) proxyFactory.getProxy();
// The method of calling the proxy object
proxy.doSomething();
}
}
3. Application research in the Java class library
The AOP Utilities framework can be widely used in the Java library, providing decoupled and enhancement of cross -sectional attention points.
3.1 log record
Generally, log records are a frequent cross -section concern.By adding a logging notification before and after the cut point, it can easily implement the logging function without modifying the business logic code.
The following is an example code that applies AOP to implement logs in the Java library:
public class LoggingAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
String methodName = invocation.getMethod().getName();
Logger.log("Before " + methodName);
// Call the target method
Object result = invocation.proceed();
Logger.log("After " + methodName);
return result;
}
}
3.2 transaction management
Affairs management is a key horizontal section concern.By adding transaction management notices before and after cutting points, transaction management of methods can be achieved to ensure data consistency and integrity.
The following is an example code that applies AOP in the Java library to implement transaction management:
public class TransactionAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
TransactionManager.beginTransaction();
try {
// Call the target method
Object result = invocation.proceed();
TransactionManager.commit();
return result;
} catch (Exception e) {
TransactionManager.rollback();
throw e;
}
}
}
in conclusion:
The AOP Utilities framework realizes the AOP application in the Java library through dynamic proxy technology.Developers can use this framework to decide the horizontal sectaries from the business logic, and apply it to the scenarios such as log records and transaction management to improve the maintenance and replication of code.
references:
1. AspectJ - The Language of AOP. (https://www.eclipse.org/aspectj/)
2. Spring AOP. (https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop)