My new colleague asked me, what is spring AOP aspect oriented programming? After my first wave of operation, he said to invite me to dinner!

Posted by webmazter on Thu, 27 Jan 2022 12:29:23 +0100

Spring AOP aspect oriented programming

AOP (aspect oriented programming), one of the three core ideas in Spring:

In the software industry, AOP is the abbreviation of Aspect Oriented Programming, which means: Aspect Oriented Programming, a technology to realize the unified maintenance of program functions through precompile and runtime dynamic agent. AOP is the continuation of OOP, a hot spot in software development, an important content in Spring framework, and a derivative paradigm of functional programming. AOP can isolate each part of business logic, reduce the coupling between each part of business logic, improve the reusability of program, and improve the efficiency of development. It is a technology to realize the unified maintenance of program functions through precompiled mode and runtime dynamic agent.

(please go deeper to Baidu)

Spring 2.0 has a great improvement on AOP. First, the configuration of AOP XML is simpler, spring 2 0 introduces a new pattern, supports the definition of aspects developed from conventional Java objects, makes full use of AspectJ pointcut language, and provides a complete type of Advice (that is, there is no redundant conversion and Object [] parameter operation). In addition, proud of the development of Annotation, spring 2 0 provides support for @ AspectJ aspects, which can be shared in AspectJ and Spring AOP. All you need is simple configuration.

AOP mechanism? [data acquisition]

Using AOP still needs to modify all methods, but the process of modifying this method is completed by Spring

AOP notification type:

Pre notification, keyword before. This refers to notification before a method is executed.

Post notification, keyword after. It refers to notification after a method is executed.

Surround notification, keyword around. Value is notified before and after a method is executed.

Notify after the exception is thrown, throw. After a method is executed, an exception is thrown for notification.

Operation code record:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
    <!-- definition bean -->
    <bean id="stuService" class="com.lxit.aop.service.StudentService" />
    <!-- AOP to configure -->
    <!-- add to Aspect of bean -->
    <bean id="logAspect" class="com.lxit.aop.aspect.LogAspect" />
    <aop:config>
        <!-- Define a pointcut -->
        <aop:pointcut id="servicepointcut" 
        expression="execution(* com.lxit.aop.service.*.*(..))" />
        <!-- definition aspect,Reference generated aspectBean And specify pointcut and method-->
        <aop:aspect id="aspect1" ref="logAspect">
            <aop:after pointcut-ref="servicepointcut" method="logAdd" />
        </aop:aspect>
    </aop:config>
</beans>
public static void main(String[] args) {
ApplicationContext ac = 
    new ClassPathXmlApplicationContext("spring.xml");
StudentService service = (StudentService) ac.getBean("stuService");
service.add();
service.getStudent();
}

Exception throwing enhancement:

The feature of exception throwing enhancement is to weave enhanced processing when the target method throws an exception,
However, exception handling generally requires obtaining exception parameters.
Add an aspect for exception handling in the configuration file.
Use < AOP: after throwing for exception weaving.

public class ExceptionAspect {
public void exceptionLog(Exception e){
    System.out.println("If an exception occurs, write to the log." + e.getMessage());
}
}
<bean id="exceptionAspect" class="com.lxit.aop.aspect.ExceptionAspect" />
<aop:config>
<!-- Define a pointcut -->
<aop:pointcut id="servicepointcut" 
    expression="execution(* com.lxit.aop.service.*.*(..))" />
<!-- definition aspect,Reference generated aspectBean And specify pointcut and method-->
<aop:aspect id="aspect2" ref="exceptionAspect">
    <!-- Indicates that the program is weaved only after an exception occurs -->
    <aop:after-throwing method="exceptionLog" 
         pointcut-ref="servicepointcut" throwing="e"/>
</aop:aspect>
</aop:config>

Surround enhancement:

Surround enhancement can be woven into the enhancement process before and after the target method
Surround enhancement is the most powerful enhancement processing. Spring gives it all the control of the target method
In surround enhancement processing, you can obtain or modify the parameters and return values of the target method, handle exceptions, and even determine whether the target method is executed

public class AroundLogger {
    public Object aroundLogger(ProceedingJoinPoint jp) throws Throwable { ... } 
}
<bean id="theLogger" class="aop. AroundLogger"></bean>
<aop:config>
    <aop:pointcut id="pointcut" expression="execution(* biz.IUserBiz.*(..))" />
    <aop:aspect ref="theLogger">
        <aop:around  method="aroundLogger"  pointcut-ref="pointcut" />
    </aop:aspect>
</aop:config>

Differences in weaving methods in the fifth middle school:


< AOP: before... >: woven before the target method call.
As long as the before method is executed, the target method will always be called, but before can throw an exception to prevent the target method from executing. Before cannot access the return value of the target method.

< AOP: after... >: woven after the target method call.
After cannot organize the execution of the target method, and after cannot access the return value of the target method.

<aop:after-throwing..>: Weaves when an exception is thrown. If throwing is specified, an exception parameter must be specified. The enhanced method must have the same name as this parameter, and the type must be greater than the exception type.

< AOP: after returning... >: weaves after the target method is successfully executed.
After returning: cannot prevent the execution of the target method. You can access the return value of the target method, but cannot modify it.

< AOP: around... >: woven before and after the target method call. Its processing method must contain a ProceedingJoinPoint parameter.
aop:around: you can organize the execution of the target method, access the return value of the target method, and modify the return value.
The above enhancers can specify args to specify parameters

A good effect can be seen in the integration of struts 2, hibernate and Spring.

The serivce layer is defined as a cut-off point. A series of operations can be started before operations, such as writing logs, transactions and so on. This is a typical case.

AOP usage scenarios [data acquisition]

AOP is used to encapsulate crosscutting concerns, which can be used in the following scenarios:

Authentication permissions Caching Context passing content passing Error handling Lazy loading

Debugging debugging logging, tracing, profiling and monitoring record tracking optimization calibration Performance optimization

Persistence persistence Resource pooling resource pool Synchronization Transactions

Thanks for reading, attention + Sanlian is the biggest support!

Topics: Python Java Programming Spring AOP