In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly talks about "what are IOC and AOP in Spring". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are IOC and AOP in Spring?"
We are in the process of using the Spring framework, in fact, in order to use IOC, dependency injection and AOP, aspect-oriented programming, these two are the soul of Spring.
The main design patterns used are factory pattern and agent pattern.
IOC is a typical factory pattern that injects instances through sessionfactory.
AOP is the embodiment of a typical agent model.
Proxy pattern is a commonly used java design pattern, which is characterized by the fact that the proxy class has the same interface as the delegate class. The proxy class is mainly responsible for preprocessing messages, filtering messages, forwarding messages to the delegate class, and post-processing messages. There is usually an association between the proxy class and the delegate class. the object of a proxy class is associated with the object of a delegate class, and the object of the proxy class itself does not really implement the service, but by calling the relevant methods of the object of the delegate class. to provide specific services.
Spring's IoC container is the core of spring, and spring AOP is an important part of the spring framework.
In traditional programming, when the caller needs the assistance of the callee, the caller usually creates an instance of the callee. However, the work of creating the callee in spring is no longer done by the caller, so the control inversion (IoC) is done; the work of creating the callee instance is usually done by the spring container and then injected into the caller, so it is also known as dependency injection (DI). Dependency injection and control inversion are the same concept.
Aspect-oriented programming (AOP) considers program structure from another point of view, and perfects object-oriented programming (OOP) by analyzing the concerns of program structure. OOP decomposes the application into layers of objects, while AOP decomposes the program into multiple aspects. Spring AOP only implements method-level join points, and in J2EE applications, it is sufficient for AOP to intercept method-level operations. In spring, in order to make IoC use robust and flexible enterprise services conveniently in the future, it is necessary to use spring AOP implementation to establish a relationship between IoC and enterprise services.
IOC: inversion of control is also called dependency injection. Take advantage of the factory model
To give the object to the container management, you only need to configure the corresponding bean in the spring configuration file, and set the relevant properties, and let the spring container generate the instance object of the class and manage the object. When the spring container starts, spring will initialize all the bean you configured in the configuration file, and then when you need to call, assign those bean that it has initialized to the class that you need to call these bean (assuming that the class name is A), the assigned method is to call A's setter method to inject, and you do not need to new these bean in A.
Note: during the interview, if there are conditions, draw a picture, so that you understand more.
AOP: aspect-oriented programming. (Aspect-Oriented Programming)
AOP can be said to be a supplement and improvement to OOP. OOP introduces the concepts of encapsulation, inheritance and polymorphism to establish an object hierarchy to simulate a collection of common behaviors. When we need to introduce public behavior for dispersed objects, OOP seems powerless. In other words, OOP allows you to define relationships from top to bottom, but it is not suitable for defining relationships from left to right. For example, the logging function. Logging code is often distributed horizontally across all object levels, having nothing to do with the core functionality of the object to which it is distributed. In OOP design, it leads to a lot of code repetition, which is not conducive to the reuse of each module.
The cross-business logic in the program (such as security, logging, transactions, etc.) is encapsulated into an aspect, and then injected into the target object (specific business logic).
The technology to implement AOP is mainly divided into two categories: one is to use dynamic proxy technology to decorate the message by intercepting the message to replace the execution of the original object behavior, and the other is to introduce a specific syntax to create "aspects" by static weaving, so that the compiler can weave the code related to "aspects" during compilation.
Simply explain, for example, if you want to add a function to print "Hello" to all classes in your biz layer, you can do it with aop ideas. You first write a class to write a class method, the method is implemented to print 'Hello', and then the Ioc class ref= "biz.*" allows each class to be injected.
What are the advantages of spring?
1. The coupling between components is reduced, and the decoupling between software layers is realized.
two。 You can use many services that are easy to provide, such as transaction management, message services, etc.
3. The container provides singleton mode support
4. The container provides AOP technology, which makes it easy to implement functions such as permission interception, runtime monitoring and so on.
5. Containers provide a large number of auxiliary classes, which can speed up the development of applications
6.spring provides integration support for mainstream application frameworks, such as hibernate,JPA,Struts.
7.spring is a low intrusive design with extremely low code pollution.
8. Independent of a variety of application servers
9.spring 's DI mechanism reduces the complexity of business object replacement
The high degree of openness of 10.Spring does not force applications to rely entirely on Spring. Developers are free to choose some or all of spring.
What is the DI mechanism?
Dependency injection (Dependecy Injection) and inversion of control (Inversion of Control) are the same concept, specifically: when a role
When another role is needed, it is usually up to the caller to create an instance of the callee in the traditional programming process. But in spring,
The job of creating the callee is no longer done by the caller, so it is called control inversion. The job of creating the callee is done by spring, and then injected into the caller
Therefore, it is also called dependency injection.
Spring manages objects in a dynamic and flexible way, two ways of injection, setting injection and constructing injection.
Set the advantages of injection: intuitive, natural
The advantage of construction injection: the order of dependencies can be determined in the constructor.
What is AOP?
Aspect-oriented programming (AOP) perfects dependency injection (DI) of spring. Aspect-oriented programming is mainly shown in two aspects in spring.
1. Providing declarative transaction management for aspect programming
2.spring supports user-defined sections
Aspect-oriented programming (aop) complements object-oriented programming (oop):
Object-oriented programming decomposes the program into various levels of objects, and aspect-oriented programming decomposes the running process of the program into various aspects.
AOP considers the structure of the program from the point of view of running the program, and extracts the aspects of the business process. Oop is a static abstraction and aop is a dynamic abstraction.
It abstracts the steps in the execution process of the application, so as to obtain the logical division between the steps.
The aop framework has two characteristics:
1. Good isolation between steps
two。 Source code independence
At this point, I believe you have a deeper understanding of "what are IOC and AOP in Spring". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.