In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to understand the Java Spring control inversion container". In the daily operation, I believe many people have doubts about how to understand the Java Spring control inversion container. The editor 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 "how to understand the Java Spring control inversion container". Next, please follow the editor to study!
Catalogue
What is a container?
Non-invasive container
IOC control inversion
Derivation of IOC theory
The disadvantages of traditional application development
"injection" mechanism
Summary
IOC essence
DI (dependency injection)
Summary
The IoC container is the core of Spring, which can also be called the Spring container. Spring manages the instantiation and initialization of objects through the IoC container, as well as the entire lifecycle of objects from creation to destruction.
The objects used in Spring are managed by the IoC container, so there is no need for us to manually create objects using the new operator. An object managed by an IoC container is called a Spring Bean,Spring Bean, which is a Java object, and is no different from an object created using the new operator.
Spring obtains which objects need to be instantiated by reading the information in the XML or Java annotations.
Spring provides two different types of IoC containers, namely BeanFactory and ApplicationContext containers.
What is a container?
A container is a software environment that provides necessary support for the operation of a particular component. For example, Tomcat is a Servlet container that provides a running environment for Servlet to run. Software such as Docker is also a container that provides the necessary Linux environment to run a specific Linux process.
In general, using containers to run components provides many underlying services in addition to providing a component runtime environment. For example, the underlying Servlet container implements TCP connections, parsing HTTP protocols and other very complex services, if there is no container to provide these services, we will not be able to write components such as Servlet with simple code and powerful functions. The most important function of the EJB container provided by the early JavaEE server is through declarative transaction services, so that developers of EJB components do not have to write lengthy transaction code, so transaction processing is greatly simplified.
Non-invasive container
Spring's IoC container is designed to be highly scalable and non-intrusive. The so-called non-intrusive means that the components of the application do not need to implement a specific interface of Spring, or that the components simply do not know that they are running in the container of Spring. This non-invasive design has the following benefits:
1. Application components can be run in the IoC container of Spring, or you can write your own code to assemble your own configuration
two。 When testing, it does not rely on the Spring container, but can be tested separately, which greatly improves the development efficiency.
IOC control inversion
The container provided by Spring is also known as the IoC container. What is IoC?
Ioc-Inversion of Control, that is, "inversion of control", is not a technology, but a concept, an idea. It refers to handing over the object call right which is traditionally directly controlled by the program code to the container to realize the object assembly and management through the container. The inversion of control is the transfer of control over the object, from the program code itself to the external container. The assembly and management of objects are realized through containers. To put it colloquially, give the creation of the object to spring, and if we need the new object, spring will create it for us and then use it for us.
Then inevitably we need to create a container and need a description to let the container know the relationship between the object and the object that needs to be created. The most specific manifestation of this description is our configurable file. The essence of IoC is how to manage objects. Traditionally, we use new to create objects, but in the process of enterprise application development, a large number of object creation in the program maintenance is easy to waste resources, and is not conducive to the expansion of the program.
It can be realized in a variety of ways. At present, the more popular way of implementation is dependency injection. It is widely used.
Dependency: the classA class contains an instance of classB, and calls the method of classB in classA to complete the function, that is, classA is dependent on classB.
IOC Theory deduces the disadvantages of traditional Application Development
Before we understand IoC, let's take a look at how common Java components work together.
Let's write a piece of code in our original way.
1. Write a UserDao interface first
2. Then write the implementation class of Dao
Public class UserDaoOneImpl implements UserDao {@ Override public void getUser () {System.out.println ("One gets user data");}}
3. Then write the interface of UserService
4. Finally, write the implementation class of Service
5. Test it.
6. Return to the UserDao interface
Add an implementation class to Userdao.
Public class UserDaoMyTwoImpl implements UserDao {@ Override public void getUser () {System.out.println ("Two gets user data");}}
7. We need to modify the corresponding implementation in the service implementation class
Public class UserServiceImpl implements UserService {private UserDao userDao = new UserDaoTwo (); @ Override public void getUser () {userDao.getUser ();}}
Hypothetically, we add another implementation class for Userdao.
Public class UserDaoThreeImpl implements UserDao {@ Override public void getUser () {System.out.println ("Three gets user data");}}
So if we want to use Three, we need to modify the corresponding implementation in the service implementation class. Assuming that our demand is very large, this approach is not applicable at all, even anti-human, right? every change requires a lot of code changes. The coupling of this design is so high that it affects the whole body.
"injection" mechanism
Inject an object into the application, the object on which the application depends
Dependency injection can be achieved through the set () method. But dependency injection can also be implemented through constructors. Spring's IoC container supports both attribute injection and constructor injection, and allows mixed use.
We can use it where we need it, not to implement it, but to set aside an interface, using set, we can modify it in the code.
@ Testpublic void test () {UserServiceImpl service = new UserServiceImpl (); service.setUserDao (new UserDaoTwoImpl ()); service.getUser (); / / now we want to add Three to implement service.setUserDao (new UserDaoThreeImpl ()); service.getUser ();}
In the past, everything was controlled and created by the program, but now we control the creation of the object and give the initiative to the caller. The program does not have to worry about how to create, how to achieve. It is only responsible for providing an interface.
This idea essentially solves the problem. We programmers no longer manage the creation of objects, but pay more attention to the implementation of the business. The coupling is greatly reduced. This is the prototype of IOC!
Summary
Traditional programming, as shown in the figure, takes the initiative to create related objects and then combine them:
There is nothing that cannot be solved by adding one layer.
When you have a container for IoC/DI, you no longer take the initiative to create these objects in the client class
IOC essence
IoC is the core content of the Spring framework, using a variety of ways to achieve IoC perfectly, you can use XML configuration, you can also use annotations, the new version of Spring can also implement IoC with zero configuration.
The Spring container first reads the configuration file during initialization, creates and organizes objects according to the configuration file or metadata and stores them in the container, and then takes out the needed objects from the Ioc container when the program uses it.
Inversion of control is a way to produce or acquire specific objects through description (XML or annotations) and through third parties. It is IoC container that implements control inversion in Spring, which is implemented by dependency injection (Dependency Injection,DI).
DI (dependency injection)
One of the key points of IoC is to dynamically provide an object with other objects it needs while the system is running. This is achieved through DI (Dependency Injection). For example, object A needs to operate the database. In the past, we always had to write code in A to get a Connection object. With spring, we only need to tell spring,A that we need a Connection. As for how and when the Connection is constructed, A does not need to know. When the system is running, spring will make a Connection at the right time, and then inject it into A like an injection, thus completing the control of the relationship between the various objects. A relies on Connection to function properly, and this Connection is injected into A by spring, hence the name of dependency injection. So how does DI work? An important feature after Java 1.3is reflection, which allows programs to dynamically generate objects, execute object methods, and change object properties while running. Spring is injected through reflection.
At this point, the study on "how to understand the Java Spring control inversion container" 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.
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.