Spring Aspects framework instance analysis (Analysis of Spring Aspects Framework Examples)
The Spring Aspects framework is an important component provided by Spring to implement AOP (facing surface programming).In this article, we will analyze several examples of the Spring Aspects framework in detail and provide a complete programming code and related configuration interpretation.
AOP is a programming paradigm, which allows developers to separate the cross-section attention point that has nothing to do with business logic (Cross-Cutting Concerns), such as log records, transaction management, security, etc.This separation can improve the reused, maintenance and testability of the code.
The Spring Aspects framework implements AOP by using ASPECTJ annotation and XML configuration.Below we will introduce three examples of using the Spring Aspects framework to better understand its working principle.
Example 1: Log Record
In this instance, we will use the log record of the Spring Aspects framework to implement the method.First of all, we need to define a cut type, and use @Aspect annotations to identify it as a cut surface.
@Aspect
public class LoggingAspect {
@Before("execution(* com.example.service.*.*(..))")
public void beforeAdvice() {
System.out.println("Going to call the method.");
}
}
In the above code, we define a notification of @Beface type, which is called before the target method execution.Note @Beface ("Execution (*com.example.service.*.*.*(..)") specifies the expression of the entry point.effect.
Then, we need to declare the cutting class in the Spring configuration file and enable the automatic proxy.
<aop:aspectj-autoproxy/>
<bean id="loggingAspect" class="com.example.aspect.LoggingAspect"/>
Through the above configuration, we have successfully applied the logging surface to our code.
Example 2: transaction management
In this instance, we will use the Spring Aspects framework to implement the transaction management of the method.First of all, we need to define a cut type, using @Aspect and @Transactions to identify it as a cut surface and specify the attributes of transactions.
@Aspect
public class TransactionAspect {
@Around("execution(* com.example.service.*.*(..))")
@Transactional
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("Before invoking method.");
Object result = pjp.proceed();
System.out.println("After invoking method.");
return result;
}
}
In the above code, we define a notification of @Aaround type, which will be called before and after the target method execution.Note @Around ("Execution (*com.example.service.*.*.*(..))") Specify the incoming point expression, it will be before and and and.After that.@Transactions specifies the attributes of transactions, and Spring will manage transactions based on the annotation.
Then, we need to declare the cutting class in the Spring configuration file and enable the automatic proxy.
<aop:aspectj-autoproxy/>
<bean id="transactionAspect" class="com.example.aspect.TransactionAspect"/>
Through the above configuration, we have successfully applied transaction management to our code.
Example 3: Security
In this instance, we will use the Spring Aspects framework to implement the security of the method.First of all, we need to define a cut type, using @Aspect and @Secured annotations to identify it as a cut surface and specify the role that needs to be performed for safety inspections.
@Aspect
public class SecurityAspect {
@Before("execution(* com.example.service.*.*(..)) && @annotation(secured)")
public void beforeAdvice(JoinPoint joinPoint, Secured secured) {
String[] roles = secured.value();
System.out.println("Checking security for roles: " + Arrays.toString(roles));
}
}
In the above code, we define a notification of @Beface type, which is called before the target method execution.Note @BeFore ("Execution (*com.example.service.*.*.*(..)) && @annotation (secured") specifies the entry point expression, it will be in all classes in the com.example.service package.All methods work, and the method of @Secured's annotation is used.Through parameter Joinpoint and Secured, we can obtain the relevant information and the attribute values of the method of method.
Then, we need to declare the cutting class in the Spring configuration file and enable the automatic proxy.
<aop:aspectj-autoproxy/>
<bean id="securityAspect" class="com.example.aspect.SecurityAspect"/>
Through the above configuration, we have successfully applied security cutting to our code.
The above is the content of the instance of the Spring Aspects framework.Through these examples, we can better understand the way and working principles of the Spring Aspects framework.I hope this article can help you understand the Spring Aspects framework.