Get an insight into spring's IOC

Source: Internet
Author: User
The IOC is an important part of the learning spring, and it is also consistent with the implementation of AOP.
So let's go into the next spring's IOC;
What the IOC really is.
Ioc-inversion of control, that is, "controlling inversion"; it is not a technique, he is a change of mind; In our application, in Class A, how to implement the B-related method; The direct method is to create an object of Class B directly in the Class A. This makes the B class associated with Class A, which forms a coupling that is not easy to transplant and extend. The IOC, on the contrary, is not artificially adding class B objects to Class A, but rather a class B object that a passive acceptance container adds to it; there is no degree of coupling between them.


How does the IOC know that there is a dependency between them?
The IOC determines whether there is a dependency between them and is judged by reading the configuration XML file.
What kind of XML configuration file.
<beans>
<!--add sessionfactory bane, note that this class is provided by spring-->
<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate5.LocalSessionFactoryBean" scope= "prototype" >
<!--inject Hibernate configuration file path, preceded by classpath:-->
<property name= "configlocation" value= "Classpath:hibernate.cfg.xml"/>
</bean>


<!--inject sessionfactory into Indexdao-->
<!--indexdao=new ssh.dao.indexdaoimpl-->
<bean id= "Indexdao" class= "Ssh.dao.IndexDaoImpl" scope= "prototype" >
<property name= "Sessionfactory" ref= "Sessionfactory"/>
</bean>


<!--Indexservice = new Ssh.service.IndexServiceImpl ()-->
<bean id= "Indexservice" class= "Ssh.service.IndexServiceImpl" scope= "prototype" >
<property name= "id" ref= "Indexdao"/>//injected the top Indexdao into the indexservice.
</bean>


<!--inject Indexservice to indexaction-->
Bean id= "indexaction" class= "Ssh.action.IndexAction" scope= "prototype" >
<property name= "is" ref= "Indexservice"/>//inject the indexservice into the indexaction.
</bean>
</beans>


This is the configured dependency. That is how to inject into the process. Now you should know how the IOC judges the dependencies between classes and how they are injected.


We are in deep understanding
When spring reads these XML configuration files, how is it implemented in the application components?
Then we must, in turn, dissect the IOC container;
I borrow other LZ words:
The IOC container is a container that has dependency injection capabilities, and the IOC container is responsible for instantiating, locating, configuring objects in the application, and building dependencies between those objects. Applications are assembled by the IOC container without the need to directly new objects in the code. Beanfactory is the actual representative of the IOC container in spring.
How does the Spring IOC container know which objects it manages? This requires a configuration file where the Spring IOC container instantiates and assembles individual objects in the application by reading the configuration metadata in the configuration file (that is, the data above <bean>). Typically, configuration metadata is based on an XML configuration file, and spring is fully decoupled from the configuration file, and can be configured with any other possible means, such as annotations, Java-based files, and configuration based on a property file.
What's the name of the spring IOC container-managed object?
IOC container-managed objects that make up your application we'll call it bean, the bean is the object that is initialized, assembled, and managed by the spring container, and beyond that, the bean is no different from the other objects in the application. So how does IOC determine how to instantiate beans, manage dependencies between beans, and manage beans? This requires the configuration of metadata, represented in spring by beandefinition, which configures metadata to specify how beans are instantiated, how to assemble beans, and so on (see http://jinnianshilongnian.iteye.com/blog/ 1413851 detailed explanation and simple sample)
When you have finished reading the contents of the link above, you will know how spring IOC reads metadata from XML files to parse meta data.


And then we're going to go into it. How are the <bean> tags in the configuration generated? What are the relationships between <bean> and <bean>? I am not important here to explain it, if interested.
can go to LZ where to see: http://jinnianshilongnian.iteye.com/blog/1413857


Through the above explanation, we have a more detailed concept.


What is control reversal: When creating a Relationship object in an object, the IOC container helps instantiate the object creation and makes the program passively receive injection dependent objects, not by the program to actively create the acquisition;


What is control forward: When creating a Relationship object in an object, we actively control it in the object to get the dependent object directly


IOC control reversal process: Spring's IOC container parses the related <bean> meta data in XML configuration file by Beanfactory related APIs, and determines which classes in the program need to inject dependent objects through the parsed data , the IOC container instantiates the dependency class that they need to inject, and is defined as the process of injecting the created object dependency into the specified Application object, within the class that is required.


Let's talk about dependency injection (DI)
Dependencies in traditional application design refer to "Relationships between classes"


LZ's post, there are examples, can accelerate your understanding faster:
http://jinnianshilongnian.iteye.com/blog/1415277















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.