SpringFramework Aop: cutting surface programming box in the Java class library
SpringFramework Aop: cutting surface programming framework in the Java class library
SpringFramework is one of the most commonly used open source frameworks in Java development. Its strong support and rich functions enable developers to build applications more efficiently.SpringFramework provides many modules and class libraries, one of which is AOP (Aspect-Oriented Programming) module for cutting-oriented) modules.
Overview:
AOP is a programming paradigm, which allows us to separate the cross -sectional focus (such as logging, transaction management, security, etc.) from the main business logic, thereby improving the reuse and maintenance of the code.AOP focuses on how to apply these horizontal sectors to our applications, rather than achieving the logic of the horizontal section focusing point itself.
SpringFramework's AOP module provides a mature and easy -to -use cut -off programming framework, which helps us to easily integrate horizontal focusing points into the application.
Core idea:
1. ASPECT: A cut surface is a module consisting of the entry point and notification.The entry point defines when the notification should be applied, and the notification defines the code executed at the entry point.
2. PointCut: The entry point is an expression to match the connection point in the application (such as method calling or abnormally throwing).By the entry point, where can we designate notifications.
3. ADVICE: Notification is the code executed at the entry point.SpringFramework provides five types of notifications: BeFore Advices, After Advice, After Throwing Advice, AFTER RETURNINGE, and Around Advi CE).
4. Join Point: The connection point is the execution point that may match the entry point in the application.In Java, the connection point can be the method call, the abnormality of the method, the execution of the method, and so on.
5. Introduction: Introduction is a way to add new methods or attributes to existing classes without modifying existing codes.
Example:
Below is an example code that uses SpringFramework Aop, which shows how to implement the logging function through the cut surface:
1. Create a cut -off class:
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
public void beforeMethod() {
System.out.println ("Performing Pre -Notice: Record Log");
}
}
2. Create a service interface:
public interface UserService {
void createUser(String username);
}
3. Create a class to implement the service interface:
@Service
public class UserServiceImpl implements UserService {
public void createUser(String username) {
System.out.println ("Create user:" + username);
}
}
4. Configure AOP in the configuration file of the application:
<aop:aspectj-autoproxy/>
<context:component-scan base-package="com.example.aspect, com.example.service"/>
In the above sample code, we created a cut type called Loggingaspect, which defined a front notice with @Beface annotation. This notification will be executed before performing any method in the com.example.service package.The logic of the notice is to simply output a log.
Then, we created an UserService interface and an UserServiceIMPL class to simulate a simple user service.In the UserServiceImpl class, we did not add any logging code, but to implement the logo function through the cut surface of AOP.
Finally, in the application configuration file, we enabled SpringFramework's AOP and component scanning, and specified the package name of the cutting and service class.
Summarize:
SpringFramework's AOP module is a powerful and easy -to -use cut -off programming framework. It can help us easily integrate horizontal attention points into the application.Using SpringFramework AOP, we can separate the horizontal sectaries such as logging, transaction management, security, etc. from the main business logic, thereby improving the reassembly and maintenance of the code.