In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the IOC of Spring". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the IOC of Spring".
1 what is IOC?
The understanding of IOC mainly stays in the concept and several ways of injection, although we know its life cycle, but the understanding of the macro point of view of the whole bean management is not deep enough.
IOC:** control inversion (Inversion of Control) container, * * is a design idea. It means handing over the object you designed to the control of the container.
1.1 what is dependency injection
I am going to use a case to show the understanding of this concept. If class a contains class b, class an is dependent on class b. If a person needs a car, it means that a person is dependent on a car.
Class User {Car car; public User () {car=new Car ();}}
In the above case, you can see that the User class contains the Car class, which means that the User class has a dependency on the Car class.
In the traditional way, a User class that wants to use Car is basically new a new object internally. But this has a big drawback, and the new approach means that User and Car are tightly coupled. It is not conducive to large-scale use. So another way can be used instead. That is, when Car is used, it can be passed directly from the outside. In this way, the coupling is greatly reduced. Let's see if the following form is much better.
Class User {Car car; public User (Car car) {this.car=car;}}
An approach like this is dependency injection, which injects dependency Car into User.
1.2 what is reversal of control
With the above concept of dependency injection, it is easier to control the inversion immediately.
Who controls who: the traditional way of User is the internal new, but now we inject the dependent object Car through dependency injection. Now that spring has appeared, it has been invented that there is a container in IOC,IOC, and these dependent objects are all managed by the container. That is, control of these dependent objects is given to the container.
How to reverse: the traditional way of User is to take the initiative to new, this way is positive. Inversion is helped by the container to create and inject dependent objects
2 several forms of dependency injection
At present, there are five main injection methods: SET injection, constructor injection, static factory, instance factory.
This paper directly uses the basic cases on the Internet to achieve. For example, UserService relies on UserDao. First define the UserDao, and then look at how to implement the injection.
Public class UserDao {public String userLogin () {return "userLogin () method";}}
Let's take a look at several implementations of dependency injection.
2.1 set injection
Step 1: XML configuration
Step 2: set injection
Public class UserService {/ / be sure to provide the setter method private UserDao userDao; public void userlogin () {String res=userDao.userLogin (); System.out.println (res);} public void setUserDao (UserDao userDao) {this.userDao = userDao;}} of the attribute.
This method is simple and easy to operate.
2.2 Constructor injection
Step 1: XML configuration
Step 2: constructor injection
Public class UserServiceV2 {private UserDao userDao; private String name; public void userlogin () {String res=userDao.userLogin (); System.out.println (res); System.out.println (name);} public UserServiceV2 (UserDao userDao,String name) {super (); this.userDao = userDao; this.name = name;}}
2.3 static factory injection
Step 1: XML configuration
Step 2: define a static factory
Public class StaticFactory {public static UserDao createuserDao () {return new UserDao ();}}
Part III: static factory injection
Public class UserService {private UserDao userDao; public void userlogin () {String res=userDao.userLogin (); System.out.println (res);} public void setUserDao (UserDao userDao) {this.userDao = userDao;}}
2.4 instance Chemical Plant
Step 1: XML configuration
Step 2: factory injection
Public class InstanceFactory {public UserDao createUserDao () {return new UserDao ();}}
These are several common ways to inject. It is commonly used in development. After knowing the concept of IOC and several implementation methods, the following mainly discusses the underlying implementation principle of IOC.
3 the underlying implementation process of IOC
There are four main steps in the core process of initialization of Spring IOC container (some processes are not expanded, such as postloader, internationalization, event broadcaster, etc.):
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
For the positioning defined by Bean, Bean may be defined in XML, or in an annotation, or in other forms. These are all located with Resource, read Resource to get BeanDefinition and register in the Bean definition registry.
The first getBean to the container triggers the creation of the Bean. When an Bean is instantiated, the Bean is instantiated based on the class information in the BeanDefinition, etc.
Put the instantiated Bean into a single-column Bean cache.
After that, getting the getBean to the container again will get it from the cache.
This picture is the core process. This process has been simplified, and the specific implementation should be designed to manage the life cycle of bean. It's scheduled for the next chapter. The core content of spring is aop and ioc. After knowing how these two are implemented, it is the core implementation of core bean management. Finally, the configuration file is introduced.
Thank you for your reading, the above is the content of "how to understand the IOC of Spring", after the study of this article, I believe you have a deeper understanding of how to understand the IOC of Spring, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.