Principles of Spring IOC and AOP

Spring is a very popular Java development framework, in which IoC and AOP are the core concepts of Spring. In this blog, I will detail the principles of Spring IoC and AOP and how they are implemented in the Spring framework.

What is Spring IoC?

IoC (Inversion of Control) is an inversion of control, which is a software design pattern. In the traditional programming model, programmers usually instantiate objects manually and then use these objects in the program. However, in the IoC pattern, the creation and management of objects are handed over to the container, and programmers only need to define the dependencies of the objects. The advantage of this mode is that it reduces the programmer's workload and makes the code easier to maintain and expand.
In Spring, an IoC container is an object that is responsible for managing all objects in the application. These objects are called beans, and they usually represent a component in the application, such as DAO (Data Access Object), Service, Controller, and so on. The IoC container will be responsible for creating these bean objects and injecting other bean objects they need according to the dependencies between them.
Spring's IoC container implements the BeanFactory interface, which is an abstract factory class that provides methods for creating and managing bean objects. Spring also provides a higher-level interface ApplicationContext, which is a sub-interface of the BeanFactory interface and provides more functions, such as internationalization, event delivery, and so on.
Principles of Spring IoC
The implementation of the Spring IoC container is based on reflection and configuration files. In Spring, we need to define bean objects and their dependencies through configuration files, and then when the program is running, the IoC container will create and manage these bean objects according to these configuration files.
Configuration files are usually in the form of XML or annotations. In the XML configuration file, we need to define information such as the bean's type, ID, and attributes. For example:

<bean id="userDao" class="com.example.UserDaoImpl">
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="userService" class="com.example.UserServiceImpl">
    <property name="userDao" ref="userDao"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/test"/>
    <property name="username" value="root"/>
    <property name="password" value="123456"/>
</bean>

In this configuration file, we define three bean objects: userDao, userService and dataSource. There is a dependency between userDao and userService, and userService depends on userDao. dataSource, as an independent bean object, is depended on by userDao.
When the IoC container starts, it will parse this configuration file and create corresponding bean objects. For each bean object, the IoC container will create an object instance and set its properties according to the information in the configuration file. For example, in the configuration file above, the IoC container will first create the dataSource object and set its properties. It then creates the userDao object and injects the dataSource object as a property of userDao. Finally, it creates the userService object and injects the userDao object as a property of userService.
This configuration file-based approach allows programmers to flexibly define bean objects and their dependencies. If you need to modify the configuration of the bean object, you only need to modify the configuration file without modifying the code. At the same time, this method can also allow programmers to understand the dependencies between bean objects more clearly, which is convenient for debugging and maintenance.
**

What is Spring AOP?

**
AOP (Aspect-Oriented Programming) is aspect-oriented programming, which is a programming paradigm. In the traditional programming mode, the program's business logic and system-level cross-cutting concerns (such as logs, transactions, security, etc.) are mixed together, which is difficult to maintain and expand. And AOP was born to solve this problem. AOP allows us to abstract these cross-cutting concerns separately and dynamically weave them into business logic at runtime.
In Spring, the implementation of AOP is based on the concept of dynamic proxy and aspect (Aspect). An aspect is a class that contains methods (called advices) that can be executed at a specific point in the program (called a join point). Typically, a join point is the execution point of a method. When the program executes to this method, the notification of the aspect will be called, so as to realize the function of cross-cutting concerns.
The principle of Spring AOP
The realization of Spring AOP is based on two technologies of JDK dynamic proxy and CGLIB dynamic proxy. When the target object implements the interface, Spring will use the JDK dynamic proxy to generate the proxy object. When the target object does not implement the interface, Spring will use CGLIB dynamic proxy to generate the proxy object.
In Spring AOP, we need to define aspects and pointcuts. Aspects contain advice methods that will be executed at specific points in the program. A pointcut is an expression that defines which join points are intercepted by the aspect. Typically, a pointcut is a regular expression that matches method names, parameters, return values, etc. in the target object.
For example, in the following code, we define an aspect LoggingAspect, which contains a notification method beforeMethod, which will be executed before all methods in UserDao are executed.

public class LoggingAspect {
    
    
 
    public void beforeMethod(JoinPoint joinPoint) {
    
    
 
        System.out.println("before method: " + joinPoint.getSignature().getName());
 
    }
}
}

In this example, we use a JoinPoint object to represent the join point. This object contains the method's signature, parameters and other information, which can be used in the notification method.
In order to weave aspects into target objects, we need to use Spring AOP configuration files. In this configuration file, we need to define facets and pointcuts. For example, the following configuration file defines an aspect LoggingAspect, which intercepts all methods in UserDao, and executes the beforeMethod method before the method is executed.

<aop:config>
    <aop:aspect id="loggingAspect" ref="loggingAspectBean">
        <aop:pointcut id="userDaoPointcut" expression="execution(* com.example.UserDao.*(..))" />
        <aop:before pointcut-ref="userDaoPointcut" method="beforeMethod" />
    </aop:aspect>
</aop:config>

In this configuration file, we use the expression attribute to define the pointcut, which means to intercept all methods in UserDao. Then we use the before tag to specify the notification method beforeMethod. In this tag, we use the pointcut-ref attribute to refer to the pointcut we just defined.
When the program starts, Spring AOP will parse this configuration file and generate proxy objects based on aspects and points. When the method in UserDao is called, the proxy object will judge whether the method matches the pointcut, and if it matches, it will call the notification method of the aspect. In the notification method, we can implement the functions of cross-cutting concerns, such as recording logs, checking permissions, handling exceptions, and so on.

Summarize

Spring IoC and AOP are the core features of the Spring framework. They allow programmers to manage dependencies between objects more flexibly, and at the same time abstract cross-cutting concerns separately and dynamically weave them into into the business logic. In this article, we introduce the basic principles of Spring IoC and AOP, and illustrate their use through example code. I hope this article will help you understand the Spring framework

Guess you like

Origin blog.csdn.net/weixin_43866043/article/details/130219696