Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Overview and creation method of AOP Module in Spring

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the overview and creation method of AOP module in Spring". In daily operation, I believe that many people have doubts about the overview and creation method of AOP module in Spring. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Overview and creation method of AOP module in Spring". Next, please follow the editor to study!

Overview

Spirng-aop module is the core module of Spring framework. Although Spring Ioc container does not rely on AOP, AOP provides a powerful and flexible solution for the implementation of Ioc.

In Spring Framework, AOP is mainly used for two purposes:

Provide some enterprise-level declarative services, typical applications such as declarative transaction management.

Allow users to implement their own aspects and use AOP to help and supplement the functions and implementation of OOP

From a functional point of view, AOP may be seen as a complement to OOP programming, providing a different way of code or system organization. The core concept in OOP is Class, while in AOP it is Aspect.

Spirng-aop module is the core module of Spring framework. Although Spring Ioc container does not rely on AOP, AOP provides a powerful and flexible solution for the implementation of Ioc.

In Spring Framework, AOP is mainly used for two purposes:

Provide some enterprise-class declarative services, typical applications such as declarative transaction management.

Allow users to implement their own aspects and use AOP to help and supplement the functions and implementation of OOP

Spring AOP is implemented by pure Java, without special compilation processing, nor does it need to control the hierarchical structure of the class loader, so it can be applied to Servlet Container and other application server.

Spring AOP currently only supports method-level switching or interception, property interception is not currently supported, if you want to intercept properties, you can consider using AspectJ language.

The use of Spring AOP is different from most other AOP frameworks. Its main purpose is not to provide a set of large and comprehensive AOP implementations, but to integrate AOP different implementations to cooperate with Spring Ioc to help solve some common problems.

It should be noted that some fine-grained advised (such as domain model) are often not well supported by Spring AOP, and AspectJ is still considered in this scenario. Even so, according to general experience, the powerful mechanism of Spring AOP can still solve the problems in most scenarios.

So what do you think of Spring AOP and AspectJ, citing the original text of the official Spring documentation:

Spring AOP will never strive to compete with AspectJ to provide a comprehensive AOP solution. We believe that both proxy-based frameworks like Spring AOP and full-blown frameworks such as AspectJ are valuable, and that they are complementary, rather than in competition. Spring seamlessly integrates Spring AOP and IoC with AspectJ, to enable all uses of AOP to be catered for within a consistent Spring-based application architecture. This integration does not affect the Spring AOP API or the AOP Alliance API: Spring AOP remains backward-compatible.

In all the module design of the Spring framework, one of the core tenets that always follow is non-intrusiveness.

So when using Spring AOP, we will not force us to introduce specific classes or interfaces into the business code, we can keep the code clean and decouple as much as possible. However, Spring also provides another option. If there is a specific scenario, you can introduce Spring AOP directly into your code. Almost all modules in the Spring framework will give you a variety of choices in the way they use them, so that users can choose a way that is more suitable for their scenario. Use AspectJ or Spring AOP, annotation or xml configuration, Depends On U.

After understanding the original intention and usage scenario of Spring AOP, let's take a look at its general implementation principle.

Most of the problems in the software world can be solved by adding a layer.

The layer mentioned here, of course, is in a broad sense, which can be a layer of abstraction or a layer of cache, which roughly means the category of isolation and decoupling.

In the world of Spring, the introduction of each module, or the integration of third-party technologies, always provides an abstraction layer, which provides users with a unified API, shielding all implementation details and differences between different implementations. Modules such as spring-cache,spring-jdbc,spring-jms and spirng-messaging provide a layer of abstraction.

The implementation of Spring AOP is based on the proxy mechanism, and the default is Jdk dynamic proxy, or you can use cglib's proxy. The difference between the two mainly lies in the difference of the object being represented. When the target object is an interface, Jdk dynamic proxy can complete the proxy, but the target object is not a class that implements the interface (as few as possible, interface-oriented programming is a good habit), you need to use cglib proxy to complete the proxy, of course, you can also force the interface to use cglib to proxy; in addition, when you need to inject or reference a specific type, if the reference is precisely the proxied object, you also need to use cglib at this time.

Functional design and implementation can be divided into two parts.

The creation of aop infrastructure can be seen as the generation of aopProxy

Handle the interception when calling the aopProxy object, that is, handle the interceptor to the target object

The creation of AOP

Generate the core class of the proxy object, ProxyFactoryBean getObjecct ()

The following figure shows the selection logic of Jdk or cglib when generating an agent:

If the specific executor of the generation agent is found, when is this operation called? anyone who has known the Spring bean life cycle should know that when bean is created, there are a series of callback interfaces for users to insert custom behavior to influence some features of bean, of which BeanPostProcessor is one of the interfaces. It has been introduced in previous articles (the ultimate weapon of playing Spring bean). Spring AOP takes advantage of this opportunity to intervene in the process of creating bean. If the bean being created is the target of our aop, create a proxy, and finally return the proxy object to Ioc.

The AbstractAutoProxyCreator class is an implementation of BeanPostProcessor that creates a proxy to look at the processor's post-processing method and finally returns the proxy returned by the createProxy () method.

Enhanced execution of AOP section

Can be understood as a call to all interceptor chains on the target object

Since there are two proxy implementations of Spring AOP, JDK dynamic proxy and cglib, the interceptor is executed in different ways. For more information, you can read the invoke method of the source code JdkDynamicAopProxy class.

The call to the target method ultimately depends on ReflectiveMethodInvocation.

Proceed processing in ReflectiveMethodInvocation deals with interceptor chains recursively.

Intercept method of CglibAopProxy

CglibMethodInvoation inherits ReflectiveMethodInvocation and uses the proceed () method above to handle the interceptor chain.

Two details to pay attention to when using Spring AOP:

1. Spring AOP does not work when calling a method within a class (self-invoke), because the internal call does not pass through the proxy object and is directly used by the target object. The solutions are:

ReFactor the code to avoid internal calls

AopContext.currentProxy ()

Or just use the AspectJ language.

2. When injecting bean, if you want to inject a specific type of bean instead of an interface, use cglib

At this point, the study on "Overview and creation of AOP modules in Spring" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report